<commit_before>class SupportingPagesController < PublicFacingController
  before_filter :find_policy
  before_filter :find_supporting_page, only: [:show]
  before_filter :set_analytics_format, only: [:show]

  def index
    if @policy.supporting_pages.empty?
      render text: "Not found", status: :not_found
    else
      redirect_to policy_supporting_page_path(@policy.document, @policy.supporting_pages.first)
    end
  end

  def show
    @document = @policy
    @recently_changed_documents = Edition.published.related_to(@policy).in_reverse_chronological_order
    set_slimmer_organisations_header(@supporting_page.edition.organisations)
  end

  private

  def find_policy
    unless @policy = Policy.published_as(params[:policy_id])
      render text: "Not found", status: :not_found
    end
  end

  def find_supporting_page
    @supporting_page = @policy.supporting_pages.find(params[:id])
  end

  def analytics_format
    :policy
  end
end
<commit_msg>Make view and preview of supporting pages work<commit_after>class SupportingPagesController < PublicFacingController
  include PermissionsChecker

  before_filter :find_policy
  before_filter :find_supporting_page, only: [:show]
  before_filter :set_analytics_format, only: [:show]

  def index
    if @policy.supporting_pages.empty?
      render_not_found
    else
      redirect_to policy_supporting_page_path(@policy.document, @policy.supporting_pages.first)
    end
  end

  def show
    @document = @policy
    @recently_changed_documents = Edition.published.related_to(@policy).in_reverse_chronological_order
    set_slimmer_organisations_header(@policy.organisations)
  end

private
  def render_not_found
    render text: "Not found", status: :not_found
  end

  def find_policy
    if should_preview?
      @policy = Document.at_slug('Policy', params[:policy_id]).try(:latest_edition)
      # Fall back to the non-preview behaviour if user isn't allowed to preview
      @policy = nil unless can_preview?(@policy)
    end

    @policy ||= Policy.published_as(params[:policy_id]) or render_not_found
  end

  def find_supporting_page
    if should_preview?
      @supporting_page = Document.at_slug('SupportingPage', params[:id]).try(:latest_edition)
      render_not_found unless can_preview?(@supporting_page)
    else
      @supporting_page = Document.at_slug('SupportingPage', params[:id]).try(:published_edition)
      render_not_found unless @policy.supporting_pages.include?(@supporting_page)
    end
  end

  def analytics_format
    :policy
  end

  def should_preview?
    params[:preview]
  end

  def can_preview?(record)
    can?(:see, record)
  end
end

{'fraction_non_alphanumeric': 0.08181818181818182, 'fraction_numerical': 0.0, 'mean_word_length': 4.490644490644491, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5387064', 'n_tokens_mistral': 828, 'n_tokens_neox': 806, 'n_words': 174}