1require 'csv'
 
2
 
3class PetitionsController < ApplicationController
 
4  before_action :redirect_to_valid_state, only: [:index]
 
5  before_action :do_not_cache, except: [:index, :show]
 
6  before_action :set_cors_headers, only: [:index, :show, :count], if: :json_request?
 
7
 
8  before_action :redirect_to_home_page_if_dissolved, only: [:new, :check, :check_results, :create]
 
 9  before_action :redirect_to_home_page_unless_opened, only: [:index, :new, :check, :check_results, :create]
 
10  before_action :redirect_to_archived_petition_if_archived, only: [:show]
 
 
12  before_action :retrieve_petitions, only: [:index]
 
13  before_action :retrieve_petition, only: [:show, :count, :gathering_support, :moderation_info]
 
14  before_action :build_petition_creator, only: [:check, :check_results, :new, :create]
 
 
16  before_action :redirect_to_stopped_page, if: :stopped?, only: [:moderation_info, :show]
 
17  before_action :redirect_to_gathering_support_url, if: :collecting_sponsors?, only: [:moderation_info, :show]
 
18  before_action :redirect_to_moderation_info_url, if: :in_moderation?, only: [:gathering_support, :show]
 
19  before_action :redirect_to_petition_url, if: :moderated?, only: [:gathering_support, :moderation_info]
 
 
21  after_action :set_content_disposition, if: :csv_request?, only: [:index]
 
  • FeatureEnvy - refers to 'format' more than self (maybe move it to another class?) » reek
  • Complexity 2 » saikuro
23  def index
 
24    respond_to do |format|
 
25      format.html
 
26      format.json
 
27      format.csv
 
28    end
 
29  end
 
  • FeatureEnvy - refers to 'format' more than self (maybe move it to another class?) » reek
  • Complexity 2 » saikuro
31  def show
 
32    respond_to do |format|
 
33      format.html
 
34      format.json
 
35    end
 
36  end
 
  • Complexity 2 » saikuro
38  def count
 
39    respond_to do |format|
 
40      format.json
 
41    end
 
42  end
 
  • Complexity 2 » saikuro
44  def check
 
45    respond_to do |format|
 
46      format.html
 
47    end
 
48  end
 
  • Complexity 2 » saikuro
50  def check_results
 
51    respond_to do |format|
 
52      format.html
 
53    end
 
54  end
 
  • Complexity 2 » saikuro
56  def new
 
57    respond_to do |format|
 
58      format.html
 
59    end
 
60  end
 
  • Complexity 4 » saikuro
62  def create
 
63    if @new_petition.save
 
64      redirect_to thank_you_petition_url(@new_petition)
 
65    else
 
66      respond_to do |format|
 
67        format.html { render :new }
 
68      end
 
69    end
 
70  end
 
  • Complexity 2 » saikuro
72  def gathering_support
 
73    respond_to do |format|
 
74      format.html
 
75    end
 
76  end
 
  • Complexity 2 » saikuro
78  def moderation_info
 
79    respond_to do |format|
 
80      format.html
 
81    end
 
82  end
 
  • Complexity 2 » saikuro
84  def thank_you
 
85    respond_to do |format|
 
86      format.html
 
87    end
 
88  end
 
 
90  protected
 
  • Complexity 1 » saikuro
92  def petition_id
 
93    params[:id].to_i
 
94  end
 
  • Complexity 2 » saikuro
96  def redirect_to_home_page_if_dissolved
 
97    redirect_to home_url if Parliament.dissolved?
 
98  end
 
 99
  • Complexity 2 » saikuro
100  def redirect_to_home_page_unless_opened
 
101    redirect_to home_url unless Parliament.opened?
 
102  end
 
  • Complexity 2 » saikuro
104  def request_format
 
105    request.format.json? ? :json : nil
 
106  end
 
  • Complexity 3 » saikuro
108  def redirect_to_archived_petition_if_archived
  • Found = in conditional. It should probably be an == » roodi
109    if petition = Archived::Petition.find_by(id: petition_id)
 
110      redirect_to archived_petition_url(petition_id, format: request_format) if petition.parliament.archived?
 
111    end
 
112  end
 
  • Complexity 1 » saikuro
114  def retrieve_petitions
 
115    @petitions = Petition.visible.search(params)
 
116  end
 
  • Complexity 1 » saikuro
118  def retrieve_petition
 
119    @petition = Petition.show.find(petition_id)
 
120  end
 
  • Complexity 1 » saikuro
122  def build_petition_creator
 
123    @new_petition = PetitionCreator.new(params, request)
 
124  end
 
  • Complexity 2 » saikuro
126  def redirect_to_valid_state
 
127    if state_present? && !valid_state?
 
128      redirect_to petitions_url(search_params(state: :all))
 
129    end
 
130  end
 
  • Complexity 1 » saikuro
132  def state_present?
 
133    params[:state].present?
 
134  end
 
  • Complexity 1 » saikuro
136  def valid_state?
 
137    public_petition_facets.include?(params[:state].to_sym)
 
138  end
 
  • Complexity 1 » saikuro
140  def search_params(overrides = {})
 
141    params.permit(:page, :q, :state).merge(overrides)
 
142  end
 
  • Complexity 1 » saikuro
144  def collecting_sponsors?
 
145    @petition.collecting_sponsors?
 
146  end
 
  • Complexity 1 » saikuro
148  def redirect_to_gathering_support_url
 
149    redirect_to gathering_support_petition_url(@petition)
 
150  end
 
  • Complexity 1 » saikuro
152  def in_moderation?
 
153    @petition.in_moderation?
 
154  end
 
  • Complexity 1 » saikuro
156  def redirect_to_moderation_info_url
 
157    redirect_to moderation_info_petition_url(@petition)
 
158  end
 
  • Complexity 1 » saikuro
160  def moderated?
 
161    @petition.moderated?
 
162  end
 
  • Complexity 1 » saikuro
164  def stopped?
 
165    @petition.stopped?
 
166  end
 
  • Complexity 1 » saikuro
168  def redirect_to_stopped_page
 
169    redirect_to home_url
 
170  end
 
  • Complexity 1 » saikuro
172  def redirect_to_petition_url
 
173    redirect_to petition_url(@petition)
 
174  end
 
  • Complexity 1 » saikuro
176  def csv_filename
 
177    "#{@petitions.scope}-petitions.csv"
 
178  end
 
  • Complexity 1 » saikuro
180  def set_content_disposition
 
181    response.headers['Content-Disposition'] = "attachment; filename=#{csv_filename}"
 
182  end
 
183end