1require 'csv' |
|
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? |
|
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] |
|
|
23 def index |
24 respond_to do |format| |
|
25 format.html
|
|
26 format.json
|
|
27 format.csv
|
|
28 end |
|
29 end |
|
|
31 def show |
32 respond_to do |format| |
|
33 format.html
|
|
34 format.json
|
|
35 end |
|
36 end |
|
|
38 def count |
39 respond_to do |format| |
|
40 format.json
|
|
41 end |
|
42 end |
|
|
44 def check |
45 respond_to do |format| |
|
46 format.html
|
|
47 end |
|
48 end |
|
|
50 def check_results |
51 respond_to do |format| |
|
52 format.html
|
|
53 end |
|
54 end |
|
|
56 def new |
57 respond_to do |format| |
|
58 format.html
|
|
59 end |
|
60 end |
|
|
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 |
|
|
72 def gathering_support |
73 respond_to do |format| |
|
74 format.html
|
|
75 end |
|
76 end |
|
|
78 def moderation_info |
79 respond_to do |format| |
|
80 format.html
|
|
81 end |
|
82 end |
|
|
84 def thank_you |
85 respond_to do |format| |
|
86 format.html
|
|
87 end |
|
88 end |
|
90 protected
|
|
|
92 def petition_id |
93 params[:id].to_i |
|
94 end |
|
|
96 def redirect_to_home_page_if_dissolved |
97 redirect_to home_url if Parliament.dissolved? |
|
98 end |
|
|
100 def redirect_to_home_page_unless_opened |
101 redirect_to home_url unless Parliament.opened? |
|
102 end |
|
|
104 def request_format |
105 request.format.json? ? :json : nil |
|
106 end |
|
|
108 def redirect_to_archived_petition_if_archived |
|
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 |
|
|
114 def retrieve_petitions |
115 @petitions = Petition.visible.search(params) |
|
116 end |
|
|
118 def retrieve_petition |
119 @petition = Petition.show.find(petition_id) |
|
120 end |
|
|
122 def build_petition_creator |
123 @new_petition = PetitionCreator.new(params, request) |
|
124 end |
|
|
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 |
|
|
132 def state_present? |
133 params[:state].present? |
|
134 end |
|
|
136 def valid_state? |
137 public_petition_facets.include?(params[:state].to_sym) |
|
138 end |
|
|
140 def search_params(overrides = {}) |
141 params.permit(:page, :q, :state).merge(overrides) |
|
142 end |
|
|
144 def collecting_sponsors? |
145 @petition.collecting_sponsors? |
|
146 end |
|
|
148 def redirect_to_gathering_support_url |
149 redirect_to gathering_support_petition_url(@petition) |
|
150 end |
|
|
152 def in_moderation? |
153 @petition.in_moderation? |
|
154 end |
|
|
156 def redirect_to_moderation_info_url |
157 redirect_to moderation_info_petition_url(@petition) |
|
158 end |
|
|
160 def moderated? |
161 @petition.moderated? |
|
162 end |
|
|
164 def stopped? |
165 @petition.stopped? |
|
166 end |
|
|
168 def redirect_to_stopped_page |
169 redirect_to home_url
|
|
170 end |
|
|
172 def redirect_to_petition_url |
173 redirect_to petition_url(@petition) |
|
174 end |
|
|
176 def csv_filename |
177 "#{@petitions.scope}-petitions.csv" |
|
178 end |
|
|
180 def set_content_disposition |
181 response.headers['Content-Disposition'] = "attachment; filename=#{csv_filename}" |
|
182 end |
|
183end |