Skip to content

Commit 15d82fe

Browse files
authored
Merge pull request #600 from alphagov/use-new-archived-petition-models
Use new archived petition models
2 parents 4c4b2cd + 35f9ff5 commit 15d82fe

28 files changed

Lines changed: 400 additions & 514 deletions

app/helpers/archived_petition_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ArchivedPetitionHelper
22
def archived_threshold(petition)
3-
if petition.threshold_for_response_reached? || petition.response?
3+
if petition.threshold_for_response_reached? || petition.government_response?
44
petition.threshold_for_debate
55
else
66
petition.threshold_for_response

app/helpers/petition_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def petition_list_header?
3737
petition_list_header.present?
3838
end
3939

40+
def reveal_government_response?
41+
params[:reveal_response] == "yes"
42+
end
43+
4044
private
4145

4246
def render_petition_hidden_details(stage_manager, form)

app/lib/deprecated_attributes.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'active_support/deprecation'
2+
3+
module DeprecatedAttributes
4+
extend ActiveSupport::Concern
5+
6+
class_methods do
7+
def deprecate_attribute(*names)
8+
names.each do |name|
9+
define_method(:"#{name}") do
10+
callstack = caller_locations(0).map(&:to_s).reject{ |path| path =~ /deprecated_attributes\.rb/ }
11+
ActiveSupport::Deprecation.warn("#{self.class.name}##{name} is deprecated and will be removed", callstack)
12+
super()
13+
end
14+
15+
define_method(:"#{name}=") do |value|
16+
callstack = caller_locations(0).map(&:to_s).reject{ |path| path =~ /deprecated_attributes\.rb/ }
17+
ActiveSupport::Deprecation.warn("#{self.class.name}##{name}= is deprecated and will be removed", callstack)
18+
super(value)
19+
end
20+
21+
define_method(:"#{name}?") do
22+
callstack = caller_locations(0).map(&:to_s).reject{ |path| path =~ /deprecated_attributes\.rb/ }
23+
ActiveSupport::Deprecation.warn("#{self.class.name}##{name}? is deprecated and will be removed", callstack)
24+
super()
25+
end
26+
end
27+
end
28+
end
29+
end

app/models/archived/petition.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Archived
44
class Petition < ActiveRecord::Base
5+
include DeprecatedAttributes
6+
57
OPEN_STATE = 'open'
68
CLOSED_STATE = 'closed'
79
HIDDEN_STATE = 'hidden'
@@ -21,12 +23,13 @@ class Petition < ActiveRecord::Base
2123
has_many :signatures
2224
has_many :sponsors, -> { where(sponsor: true) }, class_name: "Signature"
2325

24-
validates :title, presence: true, length: { maximum: 150 }
25-
validates :description, presence: true, length: { maximum: 1000 }
26+
validates :action, presence: true, length: { maximum: 150 }
27+
validates :background, length: { maximum: 300 }, allow_blank: true
28+
validates :additional_details, length: { maximum: 1000 }, allow_blank: true
2629
validates :state, presence: true, inclusion: STATES
2730
validates :closed_at, presence: true, unless: :rejected?
2831

29-
extend Searchable(:title, :description)
32+
extend Searchable(:action, :background, :additional_details)
3033
include Browseable
3134

3235
filter :parliament
@@ -36,13 +39,16 @@ class Petition < ActiveRecord::Base
3639
facet :open, -> { for_state(OPEN_STATE).by_most_signatures }
3740
facet :closed, -> { for_state(CLOSED_STATE).by_most_signatures }
3841
facet :rejected, -> { for_state(REJECTED_STATE).by_most_signatures }
42+
facet :with_response, -> { with_response.by_most_signatures }
3943
facet :by_most_signatures, -> { by_most_signatures }
4044
facet :by_created_at, -> { by_created_at }
4145

4246
default_scope { preload(:parliament) }
4347

4448
delegate :threshold_for_response, :threshold_for_debate, to: :parliament
4549

50+
deprecate_attribute :title, :description, :response, :reason_for_rejection
51+
4652
class << self
4753
def for_state(state)
4854
where(state: state)
@@ -55,14 +61,10 @@ def by_created_at
5561
def by_most_signatures
5662
reorder(signature_count: :desc)
5763
end
58-
end
59-
60-
def action
61-
super || title
62-
end
6364

64-
def action?
65-
super || title?
65+
def with_response
66+
where.not(government_response_at: nil)
67+
end
6668
end
6769

6870
def open?
@@ -91,6 +93,10 @@ def closed_early_due_to_election?
9193
closed_at == parliament.dissolution_at
9294
end
9395

96+
def government_response?
97+
government_response_at && government_response
98+
end
99+
94100
def threshold_for_debate_reached?
95101
signature_count >= parliament.threshold_for_debate
96102
end

app/views/application/_social_meta.html.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<% if archived_petition_page? %>
88
<%= open_graph_tag 'url', archived_petition_url(@petition) %>
99
<%= open_graph_tag 'type', 'article' %>
10-
<%= open_graph_tag 'title', :archived_title, petition: @petition.title %>
11-
<%= open_graph_tag 'description', @petition.description %>
10+
<%= open_graph_tag 'title', :archived_title, petition: @petition.action %>
11+
<%= open_graph_tag 'description', @petition.background? ? @petition.background : @petition.additional_details %>
1212
<% elsif defined?(@petition) %>
1313
<%= open_graph_tag 'url', petition_url(@petition) %>
1414
<%= open_graph_tag 'type', 'article' %>
@@ -24,8 +24,8 @@
2424
<%= twitter_card_tag 'site', '@hocpetitions' %>
2525
<%= twitter_card_tag 'image', 'os-social/opengraph-image.png' %>
2626
<% if archived_petition_page? %>
27-
<%= twitter_card_tag 'title', :title, petition: @petition.title %>
28-
<%= twitter_card_tag 'description', @petition.description %>
27+
<%= twitter_card_tag 'title', :title, petition: @petition.action %>
28+
<%= twitter_card_tag 'description', @petition.background? ? @petition.background : @petition.additional_details %>
2929
<% elsif defined?(@petition) %>
3030
<%= twitter_card_tag 'title', :title, petition: @petition.action %>
3131
<%= twitter_card_tag 'description', @petition.background %>

app/views/archived/petitions/_petition.json.jbuilder

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,33 @@ json.links do
66
end if defined?(is_collection)
77

88
json.attributes do
9-
json.title petition.title
10-
json.description petition.description
9+
json.action petition.action
10+
json.background petition.background
11+
json.additional_details petition.additional_details
1112
json.state petition.state
1213
json.signature_count petition.signature_count
1314
json.opened_at api_date_format(petition.opened_at)
1415
json.closed_at api_date_format(petition.closed_at)
16+
json.government_response_at api_date_format(petition.government_response_at)
17+
json.rejected_at api_date_format(petition.rejected_at)
1518
json.created_at api_date_format(petition.created_at)
1619
json.updated_at api_date_format(petition.updated_at)
1720

18-
if petition.rejected?
21+
if rejection = petition.rejection
1922
json.rejection do
20-
json.details petition.reason_for_rejection
23+
json.code rejection.code
24+
json.details rejection.details
2125
end
2226
else
2327
json.rejection nil
2428
end
2529

26-
if petition.response?
30+
if response = petition.government_response
2731
json.government_response do
28-
json.details petition.response
32+
json.summary response.summary
33+
json.details response.details
34+
json.created_at api_date_format(response.created_at)
35+
json.updated_at api_date_format(response.updated_at)
2936
end
3037
else
3138
json.government_response nil

app/views/archived/petitions/index.csv.ruby

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ csv_builder = lambda do |csv|
33

44
@petitions.find_each do |petition|
55
csv << [
6-
petition.title,
6+
petition.action,
77
archived_petition_url(petition),
88
petition.state,
99
petition.signature_count

app/views/archived/petitions/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<ol>
4949
<% @petitions.each do |petition| %>
5050
<li class="petition-item petition">
51-
<h3><%= link_to petition.title, archived_petition_path(petition), :class => 'text_link' %></h3>
51+
<h3><%= link_to petition.action, archived_petition_path(petition), :class => 'text_link' %></h3>
5252
<% if petition.rejected? %><p>Rejected</p><% end %>
5353
<p><%= signature_count(:default, petition.signature_count) unless petition.rejected? %></p>
5454
</li>

app/views/archived/petitions/show.html.erb

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,35 @@
1515
<% else %>
1616
<span class="heading-secondary">Petition</span>
1717
<% end %>
18-
<%= @petition.title %>
18+
<%= @petition.action %>
1919
</h1>
2020

21-
<div><%= auto_link(simple_format(h(@petition.description)), html: { rel: 'nofollow' }) %></div>
21+
<% if @petition.background? %>
22+
<div><%= auto_link(simple_format(h(@petition.background)), html: { rel: 'nofollow' }) %></div>
23+
<% if @petition.additional_details? %>
24+
<details>
25+
<summary><span class="summary">More details</span></summary>
26+
<div><%= auto_link(simple_format(h(@petition.additional_details)), html: { rel: 'nofollow' }) %></div>
27+
</details>
28+
<% end %>
29+
<% else %>
30+
<% if @petition.additional_details? %>
31+
<details open="open">
32+
<summary><span class="summary">More details</span></summary>
33+
<div><%= auto_link(simple_format(h(@petition.additional_details)), html: { rel: 'nofollow' }) %></div>
34+
</details>
35+
<% end %>
36+
<% end %>
2237

2338
<% if @petition.rejected? %>
2439
<p class="flash-notice">This petition was rejected</p>
2540

2641
<h2>Why was this petition rejected?</h2>
27-
<%= auto_link(simple_format(h(@petition.reason_for_rejection)), html: { rel: 'nofollow' }) %>
28-
29-
<p>We only reject petitions that don’t meet the <a href="https://petition.parliament.uk/help#standards">petition standards</a>.</p>
42+
<%= rejection_description(@petition.rejection.code) %>
43+
<% if @petition.rejection.details? %>
44+
<p><%= auto_link(simple_format(h(@petition.rejection.details))) %></p>
45+
<% end %>
46+
<p>We only reject petitions that don’t meet the <%= link_to 'petition standards', help_url(anchor: 'standards') %>.</p>
3047
<% else %>
3148
<% if @petition.closed_early_due_to_election? %>
3249
<p class="flash-notice">
@@ -54,13 +71,34 @@
5471
</div>
5572

5673
<div class="about-petitions">
57-
<% if @petition.response? %>
74+
<% if government_response = @petition.government_response? %>
5875
<section class="about-item about-item-count-response" id="response-threshold" aria-labelledby="response-threshold-heading">
5976
<h2 id="response-threshold-heading">Government responded</h2>
60-
<%= auto_link(simple_format(h(@petition.response), class: 'response'), html: { rel: 'nofollow' }) %>
77+
<% if government_response.summary? %>
78+
<blockquote class="pull-quote">
79+
<%= auto_link(simple_format(h(government_response.summary)), html: { rel: 'nofollow' } ) %>
80+
</blockquote>
81+
82+
<% if government_response.details? %>
83+
<% if reveal_government_response? %>
84+
<%= auto_link(simple_format(h(government_response.details)), html: { rel: 'nofollow' }) %>
85+
<% else %>
86+
<details>
87+
<summary><span class="summary">Read the response in full</span></summary>
88+
<div class="panel-indent panel-no-border">
89+
<%= auto_link(simple_format(h(government_response.details)), html: { rel: 'nofollow' }) %>
90+
</div>
91+
</details>
92+
<% end %>
93+
<% end %>
94+
95+
<% else %>
96+
<%= auto_link(simple_format(h(government_response.details), class: 'response'), html: { rel: 'nofollow' }) %>
97+
<% end %>
6198
</section>
6299
<% end %>
63100
</div>
101+
64102
<% end %>
65103

66104
<ul class="petition-meta">

bin/create_test_csv_data.rb

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)