Skip to content

Commit

Permalink
Merge pull request #661 from alphagov/add-gov-responded-date
Browse files Browse the repository at this point in the history
Display date of government response on home page and petition page
  • Loading branch information
pixeltrix authored Jun 24, 2018
2 parents 822ba89 + 77456ce commit 2dd7ac2
Show file tree
Hide file tree
Showing 27 changed files with 116 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def fetch_government_response
end

def government_response_params
params.require(:archived_government_response).permit(:summary, :details)
params.require(:archived_government_response).permit(:responded_on, :summary, :details)
end

def send_email_to_petitioners?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/government_response_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def fetch_government_response
end

def government_response_params
params.require(:government_response).permit(:summary, :details)
params.require(:government_response).permit(:responded_on, :summary, :details)
end

def send_email_to_petitioners?
Expand Down
1 change: 1 addition & 0 deletions app/jobs/archive_petition_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def perform(petition)

if government_response = petition.government_response
p.build_government_response do |r|
r.responded_on = government_response.responded_on
r.summary = government_response.summary
r.details = government_response.details
r.created_at = government_response.created_at
Expand Down
15 changes: 15 additions & 0 deletions app/models/archived/government_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ class GovernmentResponse < ActiveRecord::Base
validates :petition, presence: true
validates :summary, presence: true, length: { maximum: 500 }
validates :details, length: { maximum: 10000 }, allow_blank: true
validates :responded_on, presence: true

after_create do
petition.touch(:government_response_at) unless petition.government_response_at?
end

def responded_on
super || default_responded_on
end

private

def default_responded_on
if petition && petition.government_response_at
petition.government_response_at.to_date
elsif created_at
created_at.to_date
end
end
end
end
4 changes: 2 additions & 2 deletions app/models/archived/petition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def not_responded
end

def with_response
where.not(government_response_at: nil)
where.not(government_response_at: nil).preload(:government_response)
end

def response_threshold_reached
Expand Down Expand Up @@ -148,7 +148,7 @@ def debateable
end

def debated
where(debate_state: 'debated')
where(debate_state: 'debated').preload(:debate_outcome)
end

def not_debated
Expand Down
17 changes: 17 additions & 0 deletions app/models/government_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ class GovernmentResponse < ActiveRecord::Base
validates :petition, presence: true
validates :summary, presence: true, length: { maximum: 200 }
validates :details, length: { maximum: 6000 }, allow_blank: true
validates :responded_on, presence: true

after_create do
petition.touch(:government_response_at) unless petition.government_response_at?
end

def responded_on
super || default_responded_on
end

private

def default_responded_on
if petition && petition.government_response_at
petition.government_response_at.to_date
elsif created_at
created_at.to_date
elsif new_record?
Date.current
end
end
end
4 changes: 2 additions & 2 deletions app/models/petition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def debateable
end

def debated
where(debate_state: 'debated')
where(debate_state: 'debated').preload(:debate_outcome)
end

def for_state(state)
Expand Down Expand Up @@ -275,7 +275,7 @@ def with_debated_outcome
end

def with_response
where.not(government_response_at: nil)
where.not(government_response_at: nil).preload(:government_response)
end

def trending(since = 1.hour.ago, limit = 3)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<h2 class="petition-action-heading">Government response</h2>
<%= form_for @government_response, :url => admin_archived_petition_government_response_path(petition), method: :put do |f| -%>
<%= form_row :for => [f.object, :responded_on] do %>
<%= f.label :responded_on, class: 'form-label' %>
<%= error_messages_for_field f.object, :responded_on %>
<%= f.date_field :responded_on, tabindex: increment, class: 'form-control' %>
<% end %>

<%= form_row :for => [f.object, :summary] do %>
<%= f.label :summary, 'Summary quote', class: 'form-label' %>
<%= error_messages_for_field f.object, :summary %>
Expand All @@ -10,7 +16,8 @@
<%= form_row :for => [f.object, :details] do %>
<%= f.label :details, 'Response in full', class: 'form-label' %>
<%= error_messages_for_field f.object, :details %>
<%= f.text_area :details, rows: 8, cols: 70, tabindex: increment, class: 'form-control' %>
<%= f.text_area :details, rows: 8, cols: 70, tabindex: increment, data: { max_length: 10000 }, class: 'form-control' %>
<p class="character-count">10000 characters max</p>
<% end %>

<%= email_petitioners_with_count_submit_button(f, petition) %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<h2 class="petition-action-heading">Government response</h2>
<%= form_for @government_response, :url => admin_petition_government_response_path(petition), method: :put do |f| -%>
<%= form_row :for => [f.object, :responded_on] do %>
<%= f.label :responded_on, class: 'form-label' %>
<%= error_messages_for_field f.object, :responded_on %>
<%= f.date_field :responded_on, tabindex: increment, class: 'form-control', disabled: @petition.editing_disabled? %>
<% end %>

<%= form_row :for => [f.object, :summary] do %>
<%= f.label :summary, 'Summary quote', class: 'form-label' %>
<%= error_messages_for_field f.object, :summary %>
Expand All @@ -10,7 +16,8 @@
<%= form_row :for => [f.object, :details] do %>
<%= f.label :details, 'Response in full', class: 'form-label' %>
<%= error_messages_for_field f.object, :details %>
<%= f.text_area :details, rows: 8, cols: 70, tabindex: increment, class: 'form-control', disabled: @petition.editing_disabled? %>
<%= f.text_area :details, rows: 8, cols: 70, tabindex: increment, data: { max_length: 10000 }, class: 'form-control', disabled: @petition.editing_disabled? %>
<p class="character-count">10000 characters max</p>
<% end %>

<%= email_petitioners_with_count_submit_button(f, petition, disabled: @petition.editing_disabled?) %>
Expand All @@ -24,4 +31,4 @@

<%= javascript_include_tag 'character-counter' %>

<%= render 'edit_lock' %>
<%= render 'edit_lock' %>
1 change: 1 addition & 0 deletions app/views/archived/petitions/_petition.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ json.attributes do

if response = petition.government_response
json.government_response do
json.responded_on api_date_format(response.responded_on)
json.summary response.summary
json.details response.details
json.created_at api_date_format(response.created_at)
Expand Down
1 change: 1 addition & 0 deletions app/views/archived/petitions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<% if government_response = @petition.government_response? %>
<section class="about-item about-item-count-response" id="response-threshold" aria-labelledby="response-threshold-heading">
<h2 id="response-threshold-heading">Government responded</h2>
<p class="secondary">This response was given on <%= short_date_format government_response.responded_on %></p>
<% if government_response.summary? %>
<blockquote class="pull-quote">
<%= auto_link(simple_format(h(government_response.summary)), html: { rel: 'nofollow' } ) %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/pages/home/_responded_petitions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<% actioned[:with_response][:list].each do |petition| %>
<li class="petition-item">
<h3><%= link_to petition.action, petition_path(petition, reveal_response: "yes", anchor: 'response-threshold'), class: "threshold-petition-title" %></h3>
<p class="intro">The government responded</p>
<p class="intro">The government responded on <%= short_date_format(petition.government_response_at) %></p>
<blockquote class="pull-quote"><%= simple_format(petition.government_response.summary) %></blockquote>
<p><%= link_to "Read the response in full", petition_path(petition, reveal_response: "yes", anchor: 'response-threshold') %></p>
</li>
Expand Down
1 change: 1 addition & 0 deletions app/views/petitions/_petition.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ json.attributes do

if response = petition.government_response
json.government_response do
json.responded_on api_date_format(response.responded_on)
json.summary response.summary
json.details response.details
json.created_at api_date_format(response.created_at)
Expand Down
1 change: 1 addition & 0 deletions app/views/petitions/_response_threshold.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<%# Has a government response #%>
<% if government_response = petition.government_response? -%>
<h2 id="response-threshold-heading">Government responded</h2>
<p class="secondary">This response was given on <%= short_date_format government_response.responded_on %></p>
<blockquote class="pull-quote">
<%= auto_link(simple_format(h(government_response.summary)), html: { rel: 'nofollow' } ) %>
</blockquote>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h3><%= link_to petition.action, petition_path(petition, reveal_response: "yes", anchor: 'response-threshold') %></h3>
<p>Government responded – <%= short_date_format(petition.government_response_at) %></p>
<p>Government responded – <%= short_date_format(petition.government_response.responded_on) %></p>
<p><%= petition.government_response.summary %></p>
<p><%= signature_count(:default, petition.signature_count) %></p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddRespondedOnToGovernmentResponses < ActiveRecord::Migration
def change
change_table :archived_government_responses do |t|
t.date :responded_on
end

change_table :government_responses do |t|
t.date :responded_on
end
end
end
8 changes: 6 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ CREATE TABLE archived_government_responses (
summary character varying(500) NOT NULL,
details text,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
updated_at timestamp without time zone NOT NULL,
responded_on date
);


Expand Down Expand Up @@ -635,7 +636,8 @@ CREATE TABLE government_responses (
summary character varying(500) NOT NULL,
details text,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
updated_at timestamp without time zone NOT NULL,
responded_on date
);


Expand Down Expand Up @@ -2668,3 +2670,5 @@ INSERT INTO schema_migrations (version) VALUES ('20180510122656');

INSERT INTO schema_migrations (version) VALUES ('20180510131346');

INSERT INTO schema_migrations (version) VALUES ('20180623131406');

Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
describe 'PATCH /update' do
let(:government_response_attributes) do
{
responded_on: Date.civil(2018, 6, 23),
summary: 'The government agrees',
details: 'Your petition is brilliant and we will do our utmost to make it law.'
}
Expand Down Expand Up @@ -118,6 +119,7 @@ def do_patch(overrides = {})
it 'stores the supplied government response in the db' do
do_patch
petition.reload
expect(government_response.responded_on).to eq government_response_attributes[:responded_on]
expect(government_response.summary).to eq government_response_attributes[:summary]
expect(government_response.details).to eq government_response_attributes[:details]
end
Expand Down Expand Up @@ -218,6 +220,7 @@ def do_patch(overrides = {})

describe 'using no params to add a government response' do
before do
government_response_attributes[:responded_on] = nil
government_response_attributes[:summary] = nil
government_response_attributes[:details] = nil
end
Expand Down Expand Up @@ -296,6 +299,7 @@ def do_patch(overrides = {})
it 'stores the supplied response on the petition in the db' do
do_patch
petition.reload
expect(government_response.responded_on).to eq government_response_attributes[:responded_on]
expect(government_response.summary).to eq government_response_attributes[:summary]
expect(government_response.details).to eq government_response_attributes[:details]
end
Expand Down Expand Up @@ -359,6 +363,7 @@ def do_patch(overrides = {})
it 'stores the supplied government response in the db' do
do_patch
petition.reload
expect(government_response.responded_on).to eq government_response_attributes[:responded_on]
expect(government_response.summary).to eq government_response_attributes[:summary]
expect(government_response.details).to eq government_response_attributes[:details]
end
Expand Down Expand Up @@ -429,6 +434,7 @@ def do_patch(overrides = {})

describe 'using no params to add a government response' do
before do
government_response_attributes[:responded_on] = nil
government_response_attributes[:summary] = nil
government_response_attributes[:details] = nil
end
Expand Down Expand Up @@ -507,6 +513,7 @@ def do_patch(overrides = {})
it 'stores the supplied response on the petition in the db' do
do_patch
petition.reload
expect(government_response.responded_on).to eq government_response_attributes[:responded_on]
expect(government_response.summary).to eq government_response_attributes[:summary]
expect(government_response.details).to eq government_response_attributes[:details]
end
Expand Down Expand Up @@ -545,7 +552,7 @@ def do_patch(overrides = {})

context "when two moderators update the response for the first time simultaneously" do
let(:government_response) do
FactoryBot.build(:archived_government_response, summary: "", details: "", petition: petition)
FactoryBot.build(:archived_government_response, responded_on: "", summary: "", details: "", petition: petition)
end

before do
Expand All @@ -559,6 +566,7 @@ def do_patch(overrides = {})
expect(petition.government_response).to be_nil

response_attributes = {
responded_on: Date.civil(2018, 6, 23),
summary: "summmary 1",
details: "details 1"
}
Expand All @@ -570,6 +578,7 @@ def do_patch(overrides = {})
allow(petition).to receive(:build_government_response).and_return(government_response)

response_attributes = {
responded_on: Date.civil(2018, 6, 23),
summary: "summmary 2",
details: "details 2"
}
Expand Down
Loading

0 comments on commit 2dd7ac2

Please sign in to comment.