1require 'csv'  | 
         |
            3class Archived::PetitionsController < ApplicationController  | 
         |
            4 before_action :redirect_to_valid_state, only: [:index]  | 
         |
            5 before_action :fetch_parliament, only: [:index]  | 
         |
            6 before_action :fetch_petitions, only: [:index]  | 
         |
            7 before_action :fetch_petition, only: [:show]  | 
         |
            9 before_action :set_cors_headers, only: [:index, :show], if: :json_request?  | 
         |
            10 after_action :set_content_disposition, if: :csv_request?, only: [:index]  | 
         |
            12 helper_method :archived_petition_facets  | 
         |
            14 def index  | 
         |
            15 respond_to do |format|  | 
         |
            16      format.html
 | 
         |
            17      format.json
 | 
         |
            18      format.csv
 | 
         |
            19 end  | 
         |
            20 end  | 
         |
            22 def show  | 
         |
            23 respond_to do |format|  | 
         |
            24      format.html
 | 
         |
            25      format.json
 | 
         |
            26 end  | 
         |
            27 end  | 
         |
            29  private
 | 
         |
            31 def parliament_id  | 
         |
            32 params[:parliament].to_i  | 
         |
            33 end  | 
         |
            35 def petition_id  | 
         |
            36 params[:id].to_i  | 
         |
            37 end  | 
         |
            39 def fetch_parliament  | 
         |
            40 if params.key?(:parliament)  | 
         |
            41 @parliament = Parliament.archived.find(parliament_id)  | 
         |
            42 else  | 
         |
            43 @parliament = Parliament.archived.first  | 
         |
            44 end  | 
         |
            45 end  | 
         |
            47 def fetch_petitions  | 
         |
            48 @petitions = @parliament.petitions.search(params)  | 
         |
            49 end  | 
         |
            51 def fetch_petition  | 
         |
            52 @petition = Archived::Petition.visible.find(petition_id)  | 
         |
            53 @parliament = @petition.parliament  | 
         |
            55 unless @parliament.archived?  | 
         |
            56      redirect_to petition_url(petition_id)
 | 
         |
            57 end  | 
         |
            58 end  | 
         |
            60 def csv_filename  | 
         |
            61 "#{@petitions.scope}-petitions-#{@parliament.period}.csv"  | 
         |
            62 end  | 
         |
            64 def redirect_to_valid_state  | 
         |
            65 if state_present? && !valid_state?  | 
         |
            66 redirect_to archived_petitions_url(search_params(state: :all))  | 
         |
            67 end  | 
         |
            68 end  | 
         |
            70 def state_present?  | 
         |
            71 params[:state].present?  | 
         |
            72 end  | 
         |
            74 def valid_state?  | 
         |
            75 archived_petition_facets.include?(params[:state].to_sym)  | 
         |
            76 end  | 
         |
            78 def search_params(overrides = {})  | 
         |
            79 params.permit(:page, :parliament, :q, :state).merge(overrides)  | 
         |
            80 end  | 
         |
            82 def archived_petition_facets  | 
         |
            83 I18n.t :archived, scope: :"petitions.facets", default: []  | 
         |
            84 end  | 
         |
            86 def set_content_disposition  | 
         |
            87 response.headers['Content-Disposition'] = "attachment; filename=#{csv_filename}"  | 
         |
            88 end  | 
         |
            89end  |