1class Admin::PetitionsController < Admin::AdminController |
|
2 before_action :redirect_to_show_page, only: [:index], if: :petition_id? |
|
3 before_action :fetch_petitions, only: [:index] |
|
4 before_action :fetch_petition, only: [:show, :resend] |
|
6 rescue_from ActiveRecord::RecordNotFound do |
|
7 redirect_to admin_root_url, alert: "Sorry, we couldn't find petition #{params[:id]}" |
|
8 end |
|
10 def index |
|
11 respond_to do |format| |
|
12 format.html
|
|
13 format.csv { render_csv }
|
|
14 end |
|
15 end |
|
17 def show |
|
18 respond_to do |format| |
|
19 format.html
|
|
20 end |
|
21 end |
|
23 def resend |
|
24 GatherSponsorsForPetitionEmailJob.perform_later(@petition, Site.feedback_email) |
|
25 redirect_to admin_petition_url(@petition), notice: :email_resent_to_creator |
|
26 end |
|
28 protected
|
|
30 def petition_id? |
|
31 /^\d+$/ =~ params[:q].to_s |
|
32 end |
|
34 def redirect_to_show_page |
|
35 redirect_to admin_petition_url(params[:q].to_i) |
|
36 end |
|
38 def scope |
|
39 if params[:match] == "none" |
|
40 Petition.untagged |
|
41 elsif params[:tags].present? |
|
42 if params[:match] == "all" |
|
43 Petition.tagged_with_all(params[:tags]) |
|
44 else |
|
45 Petition.tagged_with_any(params[:tags]) |
|
46 end |
|
47 else |
|
48 Petition.all |
|
49 end |
|
50 end |
|
52 def fetch_petitions |
|
53 @petitions = scope.search(params) |
|
54 end |
|
56 def fetch_petition |
|
57 @petition = Petition.find(params[:id]) |
|
58 end |
|
60 def render_csv |
|
61 set_file_headers
|
|
62 set_streaming_headers
|
|
64 #setting the body to an enumerator, rails will iterate this enumerator |
|
65 self.response_body = PetitionsCSVPresenter.new(@petitions).render |
|
66 end |
|
68 def set_file_headers |
|
69 headers["Content-Type"] = "text/csv" |
|
70 headers["Content-Disposition"] = "attachment; filename=#{csv_filename}" |
|
71 end |
|
73 def set_streaming_headers |
|
74 #nginx doc: Setting this to "no" will allow unbuffered responses suitable for Comet and HTTP streaming applications |
|
75 headers['X-Accel-Buffering'] = 'no' |
|
76 headers["Cache-Control"] ||= "no-cache" |
|
77 headers.delete("Content-Length") |
|
78 end |
|
80 def csv_filename |
|
81 "#{@petitions.scope.to_s.dasherize}-petitions-#{Time.current.to_s(:number)}.csv" |
|
82 end |
|
83end |