diff --git a/app/controllers/admin/archived/government_response_controller.rb b/app/controllers/admin/archived/government_response_controller.rb index 806bb7a68..f4a593c75 100644 --- a/app/controllers/admin/archived/government_response_controller.rb +++ b/app/controllers/admin/archived/government_response_controller.rb @@ -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? diff --git a/app/controllers/admin/government_response_controller.rb b/app/controllers/admin/government_response_controller.rb index f379f0f57..531f57c0c 100644 --- a/app/controllers/admin/government_response_controller.rb +++ b/app/controllers/admin/government_response_controller.rb @@ -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? diff --git a/app/jobs/archive_petition_job.rb b/app/jobs/archive_petition_job.rb index 85724a3c9..b2b6b8bee 100644 --- a/app/jobs/archive_petition_job.rb +++ b/app/jobs/archive_petition_job.rb @@ -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 diff --git a/app/models/archived/government_response.rb b/app/models/archived/government_response.rb index c00a7db04..0e8802da0 100644 --- a/app/models/archived/government_response.rb +++ b/app/models/archived/government_response.rb @@ -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 diff --git a/app/models/archived/petition.rb b/app/models/archived/petition.rb index b90cece62..9f40aa727 100644 --- a/app/models/archived/petition.rb +++ b/app/models/archived/petition.rb @@ -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 @@ -148,7 +148,7 @@ def debateable end def debated - where(debate_state: 'debated') + where(debate_state: 'debated').preload(:debate_outcome) end def not_debated diff --git a/app/models/government_response.rb b/app/models/government_response.rb index 4f9ad81a4..cec9a3e85 100644 --- a/app/models/government_response.rb +++ b/app/models/government_response.rb @@ -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 diff --git a/app/models/petition.rb b/app/models/petition.rb index 5e118b2a9..baa2d57e3 100644 --- a/app/models/petition.rb +++ b/app/models/petition.rb @@ -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) @@ -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) diff --git a/app/views/admin/archived/government_response/_petition_action_government_response.html.erb b/app/views/admin/archived/government_response/_petition_action_government_response.html.erb index d6905e1c3..b7eff9e6d 100644 --- a/app/views/admin/archived/government_response/_petition_action_government_response.html.erb +++ b/app/views/admin/archived/government_response/_petition_action_government_response.html.erb @@ -1,5 +1,11 @@
10000 characters max
<% end %> <%= email_petitioners_with_count_submit_button(f, petition) %> diff --git a/app/views/admin/government_response/_petition_action_government_response.html.erb b/app/views/admin/government_response/_petition_action_government_response.html.erb index 4b7663694..4ce16818e 100644 --- a/app/views/admin/government_response/_petition_action_government_response.html.erb +++ b/app/views/admin/government_response/_petition_action_government_response.html.erb @@ -1,5 +1,11 @@10000 characters max
<% end %> <%= email_petitioners_with_count_submit_button(f, petition, disabled: @petition.editing_disabled?) %> @@ -24,4 +31,4 @@ <%= javascript_include_tag 'character-counter' %> -<%= render 'edit_lock' %> \ No newline at end of file +<%= render 'edit_lock' %> diff --git a/app/views/archived/petitions/_petition.json.jbuilder b/app/views/archived/petitions/_petition.json.jbuilder index ab70858dd..a6e06b1ec 100644 --- a/app/views/archived/petitions/_petition.json.jbuilder +++ b/app/views/archived/petitions/_petition.json.jbuilder @@ -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) diff --git a/app/views/archived/petitions/show.html.erb b/app/views/archived/petitions/show.html.erb index 0706bcd63..b201bd50f 100644 --- a/app/views/archived/petitions/show.html.erb +++ b/app/views/archived/petitions/show.html.erb @@ -138,6 +138,7 @@ <% if government_response = @petition.government_response? %>This response was given on <%= short_date_format government_response.responded_on %>
<% if government_response.summary? %><%= auto_link(simple_format(h(government_response.summary)), html: { rel: 'nofollow' } ) %> diff --git a/app/views/pages/home/_responded_petitions.html.erb b/app/views/pages/home/_responded_petitions.html.erb index e28070be7..f69c889a3 100644 --- a/app/views/pages/home/_responded_petitions.html.erb +++ b/app/views/pages/home/_responded_petitions.html.erb @@ -5,7 +5,7 @@ <% actioned[:with_response][:list].each do |petition| %>diff --git a/app/views/petitions/_petition.json.jbuilder b/app/views/petitions/_petition.json.jbuilder index 9deed4270..30fe342f4 100644 --- a/app/views/petitions/_petition.json.jbuilder +++ b/app/views/petitions/_petition.json.jbuilder @@ -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) diff --git a/app/views/petitions/_response_threshold.html.erb b/app/views/petitions/_response_threshold.html.erb index 1d6a5adfc..58ca23b86 100644 --- a/app/views/petitions/_response_threshold.html.erb +++ b/app/views/petitions/_response_threshold.html.erb @@ -2,6 +2,7 @@ <%# Has a government response #%> <% if government_response = petition.government_response? -%> <%= link_to petition.action, petition_path(petition, reveal_response: "yes", anchor: 'response-threshold'), class: "threshold-petition-title" %>
-The government responded
+The government responded on <%= short_date_format(petition.government_response_at) %>
<%= simple_format(petition.government_response.summary) %><%= link_to "Read the response in full", petition_path(petition, reveal_response: "yes", anchor: 'response-threshold') %>
Government responded
+This response was given on <%= short_date_format government_response.responded_on %>
<%= auto_link(simple_format(h(government_response.summary)), html: { rel: 'nofollow' } ) %>diff --git a/app/views/petitions/search/result_items/_petition_result_for_facet_with_response.html.erb b/app/views/petitions/search/result_items/_petition_result_for_facet_with_response.html.erb index 0120d05e6..903edc661 100644 --- a/app/views/petitions/search/result_items/_petition_result_for_facet_with_response.html.erb +++ b/app/views/petitions/search/result_items/_petition_result_for_facet_with_response.html.erb @@ -1,4 +1,4 @@<%= link_to petition.action, petition_path(petition, reveal_response: "yes", anchor: 'response-threshold') %>
-Government responded – <%= short_date_format(petition.government_response_at) %>
+Government responded – <%= short_date_format(petition.government_response.responded_on) %>
<%= petition.government_response.summary %>
<%= signature_count(:default, petition.signature_count) %>
diff --git a/db/migrate/20180623131406_add_responded_on_to_government_responses.rb b/db/migrate/20180623131406_add_responded_on_to_government_responses.rb new file mode 100644 index 000000000..f31d92b73 --- /dev/null +++ b/db/migrate/20180623131406_add_responded_on_to_government_responses.rb @@ -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 diff --git a/db/structure.sql b/db/structure.sql index e771134aa..9365a426d 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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 ); @@ -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 ); @@ -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'); + diff --git a/spec/controllers/admin/archived/government_response_controller_spec.rb b/spec/controllers/admin/archived/government_response_controller_spec.rb index 963cb07c0..f52f06aa2 100644 --- a/spec/controllers/admin/archived/government_response_controller_spec.rb +++ b/spec/controllers/admin/archived/government_response_controller_spec.rb @@ -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.' } @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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" } @@ -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" } diff --git a/spec/controllers/admin/government_response_controller_spec.rb b/spec/controllers/admin/government_response_controller_spec.rb index 12c5070e5..cbcc1a88f 100644 --- a/spec/controllers/admin/government_response_controller_spec.rb +++ b/spec/controllers/admin/government_response_controller_spec.rb @@ -98,6 +98,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.' } @@ -130,6 +131,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 @@ -227,6 +229,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 @@ -305,6 +308,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 @@ -381,6 +385,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 @@ -448,6 +453,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 @@ -526,6 +532,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 @@ -577,7 +584,7 @@ def do_patch(overrides = {}) context "when two moderators update the response for the first time simultaneously" do let(:government_response) do - FactoryBot.build(:government_response, summary: "", details: "", petition: petition) + FactoryBot.build(:government_response, responded_on: "", summary: "", details: "", petition: petition) end before do @@ -591,6 +598,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" } @@ -602,6 +610,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" } diff --git a/spec/factories.rb b/spec/factories.rb index 2fe3747c9..dfd15022e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -39,6 +39,7 @@ factory :archived_government_response, class: "Archived::GovernmentResponse" do association :petition, factory: :archived_petition + responded_on { 1.year.ago.to_date } details "Government Response Details" summary "Government Response Summary" end @@ -594,6 +595,7 @@ factory :government_response do association :petition, factory: :awaiting_petition + responded_on { 1.day.ago.to_date } details "Government Response Details" summary "Government Response Summary" end diff --git a/spec/jobs/archive_petition_job_spec.rb b/spec/jobs/archive_petition_job_spec.rb index 89df549a1..2ee311a38 100644 --- a/spec/jobs/archive_petition_job_spec.rb +++ b/spec/jobs/archive_petition_job_spec.rb @@ -183,6 +183,7 @@ end it "copies the government_response object" do + expect(archived_government_response.responded_on).to eq(government_response.responded_on) expect(archived_government_response.summary).to eq(government_response.summary) expect(archived_government_response.details).to eq(government_response.details) expect(archived_government_response.created_at).to be_usec_precise_with(government_response.created_at) diff --git a/spec/models/archived/government_response_spec.rb b/spec/models/archived/government_response_spec.rb index 741649d19..ac57491dc 100644 --- a/spec/models/archived/government_response_spec.rb +++ b/spec/models/archived/government_response_spec.rb @@ -9,6 +9,7 @@ it { is_expected.to have_db_column(:petition_id).of_type(:integer) } it { is_expected.to have_db_column(:summary).of_type(:string).with_options(limit: 500, null: false) } it { is_expected.to have_db_column(:details).of_type(:text) } + it { is_expected.to have_db_column(:responded_on).of_type(:date) } it { is_expected.to have_db_column(:created_at).of_type(:datetime).with_options(null: false) } it { is_expected.to have_db_column(:updated_at).of_type(:datetime).with_options(null: false) } end diff --git a/spec/models/government_response_spec.rb b/spec/models/government_response_spec.rb index f90a63f2c..8833fcda6 100644 --- a/spec/models/government_response_spec.rb +++ b/spec/models/government_response_spec.rb @@ -5,6 +5,15 @@ expect(FactoryBot.build(:government_response)).to be_valid end + describe "schema" do + it { is_expected.to have_db_column(:petition_id).of_type(:integer) } + it { is_expected.to have_db_column(:summary).of_type(:string).with_options(limit: 500, null: false) } + it { is_expected.to have_db_column(:details).of_type(:text) } + it { is_expected.to have_db_column(:responded_on).of_type(:date) } + it { is_expected.to have_db_column(:created_at).of_type(:datetime).with_options(null: false) } + it { is_expected.to have_db_column(:updated_at).of_type(:datetime).with_options(null: false) } + end + describe "associations" do it { is_expected.to belong_to(:petition).touch(true) } end diff --git a/spec/requests/archived_petition_show_spec.rb b/spec/requests/archived_petition_show_spec.rb index e34446233..648dff2a0 100644 --- a/spec/requests/archived_petition_show_spec.rb +++ b/spec/requests/archived_petition_show_spec.rb @@ -93,6 +93,7 @@ a_hash_including( "government_response_at" => a_string_matching(%r[\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\z]), "government_response" => a_hash_including( + "responded_on" => a_string_matching(%r[\A\d{4}-\d{2}-\d{2}\z]), "summary" => "Summary of what the government said", "details" => "Details of what the government said", "created_at" => a_string_matching(%r[\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\z]), diff --git a/spec/requests/archived_petitions_list_spec.rb b/spec/requests/archived_petitions_list_spec.rb index 74c673b7f..1c23a5830 100644 --- a/spec/requests/archived_petitions_list_spec.rb +++ b/spec/requests/archived_petitions_list_spec.rb @@ -182,6 +182,7 @@ a_hash_including( "attributes" => a_hash_including( "government_response" => a_hash_including( + "responded_on" => a_string_matching(%r[\A\d{4}-\d{2}-\d{2}\z]), "summary" => "Summary of what the government said", "details" => "Details of what the government said" ) diff --git a/spec/requests/petition_show_spec.rb b/spec/requests/petition_show_spec.rb index 63a5c7e36..1cd394f87 100644 --- a/spec/requests/petition_show_spec.rb +++ b/spec/requests/petition_show_spec.rb @@ -105,6 +105,7 @@ a_hash_including( "government_response_at" => a_string_matching(%r[\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\z]), "government_response" => a_hash_including( + "responded_on" => a_string_matching(%r[\A\d{4}-\d{2}-\d{2}\z]), "summary" => "Summary of what the government said", "details" => "Details of what the government said", "created_at" => a_string_matching(%r[\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\z]), diff --git a/spec/requests/petitions_list_spec.rb b/spec/requests/petitions_list_spec.rb index 8a512dfe8..2a921817b 100644 --- a/spec/requests/petitions_list_spec.rb +++ b/spec/requests/petitions_list_spec.rb @@ -206,6 +206,7 @@ a_hash_including( "attributes" => a_hash_including( "government_response" => a_hash_including( + "responded_on" => a_string_matching(%r[\A\d{4}-\d{2}-\d{2}\z]), "summary" => "Summary of what the government said", "details" => "Details of what the government said" )