1require 'csv' |
|
3class PetitionCSVPresenter |
|
4 include DateTimeHelper |
|
5 include Rails.application.routes.url_helpers |
|
7 def self.fields |
|
8 urls + attributes + timestamps + [:notes] |
|
9 end |
|
|
11 def initialize(petition) |
12 @petition = petition |
|
13 end |
|
|
15 def to_csv |
16 CSV::Row.new(self.class.fields, values).to_s |
|
17 end |
|
19 attr_reader :petition |
|
21 private
|
|
23 def self.urls |
|
24 [:public_url, :admin_url] |
|
25 end |
|
27 def self.attributes |
|
28 [:id, |
|
29 :action, :background, :additional_details, :state, |
|
30 :creator_name, :creator_email, :signature_count, :rejection_code, :rejection_details, |
|
31 :government_response_summary, :government_response_details, :debate_date, |
|
32 :debate_transcript_url, :debate_video_url, :debate_pack_url, :debate_overview |
|
33 ]
|
|
34 end |
|
36 def self.timestamps |
|
37 [
|
|
38 :created_at, :updated_at, :open_at, :closed_at, :government_response_at, :scheduled_debate_date, |
|
39 :response_threshold_reached_at, :debate_threshold_reached_at, :rejected_at, :debate_outcome_at, |
|
40 :moderation_threshold_reached_at, :government_response_created_at, :government_response_updated_at |
|
41 ]
|
|
42 end |
|
|
44 def values |
45 self.class.fields.map {|field| send field } |
|
46 end |
|
|
48 def public_url |
49 petition_url id
|
|
50 end |
|
|
52 def admin_url |
53 admin_petition_url id
|
|
54 end |
|
|
56 def notes |
57 petition.note.details if petition.note |
|
58 end |
|
60 delegate *attributes, to: :petition |
|
62 timestamps.each do |timestamp| |
|
63 define_method timestamp do |
|
64 api_date_format petition.send timestamp
|
|
65 end |
|
66 end |
|
67end |