diff options
-rw-r--r-- | app/controllers/admin_request_controller.rb | 8 | ||||
-rw-r--r-- | app/controllers/api_controller.rb | 2 | ||||
-rw-r--r-- | app/models/info_request.rb | 44 | ||||
-rw-r--r-- | lib/tasks/temp.rake | 30 | ||||
-rw-r--r-- | spec/controllers/api_controller_spec.rb | 3 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 53 |
6 files changed, 126 insertions, 14 deletions
diff --git a/app/controllers/admin_request_controller.rb b/app/controllers/admin_request_controller.rb index 66989ea93..40ccfb98c 100644 --- a/app/controllers/admin_request_controller.rb +++ b/app/controllers/admin_request_controller.rb @@ -62,9 +62,6 @@ class AdminRequestController < AdminController @info_request.title = params[:info_request][:title] @info_request.prominence = params[:info_request][:prominence] - if @info_request.described_state != params[:info_request][:described_state] - @info_request.set_described_state(params[:info_request][:described_state]) - end @info_request.awaiting_description = params[:info_request][:awaiting_description] == "true" ? true : false @info_request.allow_new_responses_from = params[:info_request][:allow_new_responses_from] @info_request.handle_rejected_responses = params[:info_request][:handle_rejected_responses] @@ -77,13 +74,16 @@ class AdminRequestController < AdminController { :editor => admin_current_user(), :old_title => old_title, :title => @info_request.title, :old_prominence => old_prominence, :prominence => @info_request.prominence, - :old_described_state => old_described_state, :described_state => @info_request.described_state, + :old_described_state => old_described_state, :described_state => params[:info_request][:described_state], :old_awaiting_description => old_awaiting_description, :awaiting_description => @info_request.awaiting_description, :old_allow_new_responses_from => old_allow_new_responses_from, :allow_new_responses_from => @info_request.allow_new_responses_from, :old_handle_rejected_responses => old_handle_rejected_responses, :handle_rejected_responses => @info_request.handle_rejected_responses, :old_tag_string => old_tag_string, :tag_string => @info_request.tag_string, :old_comments_allowed => old_comments_allowed, :comments_allowed => @info_request.comments_allowed }) + if @info_request.described_state != params[:info_request][:described_state] + @info_request.set_described_state(params[:info_request][:described_state]) + end # expire cached files expire_for_request(@info_request) flash[:notice] = 'Request successfully updated.' diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 49b226e4b..e7bea67ef 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -63,6 +63,8 @@ class ApiController < ApplicationController :smtp_message_id => nil ) + request.set_described_state('waiting_response') + # Return the URL and ID number. render :json => { 'url' => make_url("request", request.url_title), diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 8f15a4ea4..9bce2ca88 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -563,12 +563,15 @@ public end # change status, including for last event for later historical purposes + # described_state should always indicate the current state of the request, as described + # by the request owner (or, in some other cases an admin or other user) def set_described_state(new_state, set_by = nil, message = "") old_described_state = described_state ActiveRecord::Base.transaction do self.awaiting_description = false last_event = self.info_request_events.last last_event.described_state = new_state + self.described_state = new_state last_event.save! self.save! @@ -588,11 +591,14 @@ public end end - # Work out what the situation of the request is. In addition to values of - # self.described_state, can take these two values: + # Work out what state to display for the request on the site. In addition to values of + # self.described_state, can take these values: # waiting_classification # waiting_response_overdue # waiting_response_very_overdue + # (this method adds an assessment of overdueness with respect to the current time to 'waiting_response' + # states, and will return 'waiting_classification' instead of the described_state if the + # awaiting_description flag is set on the request). def calculate_status(cached_value_ok=false) if cached_value_ok && @cached_calculated_status return @cached_calculated_status @@ -611,10 +617,22 @@ public return 'waiting_response' end + + # 'described_state' can be populated on any info_request_event but is only + # ever used in the process populating calculated_state on the + # info_request_event (if it represents a response, outgoing message, edit + # or status update), or previous response or outgoing message events for + # the same request. + # Fill in any missing event states for first response before a description # was made. i.e. We take the last described state in between two responses # (inclusive of earlier), and set it as calculated value for the earlier - # response. + # response. Also set the calculated state for any initial outgoing message, + # follow up, edit or status_update to the described state of that event. + + # Note that the calculated state of the latest info_request_event will + # be used in latest_status based searches and should match the described_state + # of the info_request. def calculate_event_states curr_state = nil for event in self.info_request_events.reverse @@ -636,7 +654,7 @@ public event.save! end curr_state = nil - elsif !curr_state.nil? && (event.event_type == 'followup_sent' || event.event_type == 'sent' || event.event_type == "status_update") + elsif !curr_state.nil? && (event.event_type == 'followup_sent' || event.event_type == 'sent') && !event.described_state.nil? && (event.described_state == 'waiting_response' || event.described_state == 'internal_review') # Followups can set the status to waiting response / internal # review. Initial requests ('sent') set the status to waiting response. @@ -648,10 +666,22 @@ public event.save! end - # And we don't want to propogate it to the response itself, + # And we don't want to propagate it to the response itself, # as that might already be set to waiting_clarification / a # success status, which we want to know about. curr_state = nil + elsif !curr_state.nil? && (['edit', 'status_update'].include? event.event_type) + # A status update or edit event should get the same calculated state as described state + # so that the described state is always indexed (and will be the latest_status + # for the request immediately after it has been described, regardless of what + # other request events precede it). This means that request should be correctly included + # in status searches for that status. These events allow the described state to propagate in + # case there is a preceding response that the described state should be applied to. + if event.calculated_state != event.described_state + event.calculated_state = event.described_state + event.last_described_at = Time.now() + event.save! + end end end end @@ -1107,10 +1137,10 @@ public begin if self.described_state.nil? self.described_state = 'waiting_response' - end + end rescue ActiveModel::MissingAttributeError # this should only happen on Model.exists?() call. It can be safely ignored. - # See http://www.tatvartha.com/2011/03/activerecordmissingattributeerror-missing-attribute-a-bug-or-a-features/ + # See http://www.tatvartha.com/2011/03/activerecordmissingattributeerror-missing-attribute-a-bug-or-a-features/ end # FOI or EIR? if !self.public_body.nil? && self.public_body.eir_only? diff --git a/lib/tasks/temp.rake b/lib/tasks/temp.rake index fcabb23de..5dba4fb7a 100644 --- a/lib/tasks/temp.rake +++ b/lib/tasks/temp.rake @@ -1,5 +1,35 @@ namespace :temp do + desc "Fix the history of requests where the described state doesn't match the latest status value + used by search, by adding an edit event that will correct the latest status" + task :fix_bad_request_states => :environment do + dryrun = ENV['DRYRUN'] != '0' + if dryrun + puts "This is a dryrun" + end + + InfoRequest.find_each() do |info_request| + next if info_request.url_title == 'holding_pen' + last_info_request_event = info_request.info_request_events[-1] + if last_info_request_event.latest_status != info_request.described_state + puts "#{info_request.id} #{info_request.url_title} #{last_info_request_event.latest_status} #{info_request.described_state}" + params = { :script => 'rake temp:fix_bad_request_states', + :user_id => nil, + :old_described_state => info_request.described_state, + :described_state => info_request.described_state + } + if ! dryrun + info_request.info_request_events.create!(:last_described_at => last_info_request_event.described_at + 1.second, + :event_type => 'status_update', + :described_state => info_request.described_state, + :calculated_state => info_request.described_state, + :params => params) + end + end + + end + end + def disable_duplicate_account(user, count, dryrun) dupe_email = "duplicateemail#{count}@example.com" puts "Updating #{user.email} to #{dupe_email} for user #{user.id}" diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index 66b8e33f0..8e9d17fbe 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -83,6 +83,9 @@ describe ApiController, "when using the API" do new_request.last_event_forming_initial_request.outgoing_message.body.should == request_data["body"].strip new_request.public_body_id.should == public_bodies(:geraldine_public_body).id + new_request.info_request_events.size.should == 1 + new_request.info_request_events[0].event_type.should == 'sent' + new_request.info_request_events[0].calculated_state.should == 'waiting_response' end def _create_request diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index c9ee57c74..3451e018f 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -670,7 +670,7 @@ describe InfoRequest do events[0].calculated_state.should == "waiting_response" events[1].event_type.should == "response" events[1].described_state.should be_nil - events[1].calculated_state.should be_nil + events[1].calculated_state.should == 'waiting_response' events[2].event_type.should == "status_update" events[2].described_state.should == "waiting_response" events[2].calculated_state.should == "waiting_response" @@ -698,7 +698,7 @@ describe InfoRequest do events[0].calculated_state.should == "waiting_response" events[1].event_type.should == "response" events[1].described_state.should be_nil - events[1].calculated_state.should be_nil + events[1].calculated_state.should == 'waiting_response' events[2].event_type.should == "status_update" events[2].described_state.should == "waiting_response" events[2].calculated_state.should == "waiting_response" @@ -731,7 +731,7 @@ describe InfoRequest do events[0].calculated_state.should == "waiting_response" events[1].event_type.should == "response" events[1].described_state.should be_nil - events[1].calculated_state.should be_nil + events[1].calculated_state.should == 'waiting_response' events[2].event_type.should == "status_update" events[2].described_state.should == "waiting_response" events[2].calculated_state.should == "waiting_response" @@ -807,6 +807,53 @@ describe InfoRequest do events[1].described_state.should == "successful" events[1].calculated_state.should == "successful" end + + it "should have sensible event states" do + # An initial request is sent + request.log_event('sent', {}) + request.set_described_state('waiting_response') + + # A response is received + request.awaiting_description = true + request.log_event("response", {}) + + # The user marks the request as successful + request.log_event("status_update", {}) + request.set_described_state("successful") + + events = request.info_request_events + events.count.should == 3 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + events[1].event_type.should == "response" + events[1].described_state.should be_nil + events[1].calculated_state.should == "successful" + events[2].event_type.should == "status_update" + events[2].described_state.should == "successful" + events[2].calculated_state.should == "successful" + end + end + + context "another series of events on a request", :focus => true do + it "should have sensible event states" do + # An initial request is sent + request.log_event('sent', {}) + request.set_described_state('waiting_response') + # An admin sets the status of the request to 'gone postal' using + # the admin interface + request.log_event("edit", {}) + request.set_described_state("gone_postal") + + events = request.info_request_events + events.count.should == 2 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + events[1].event_type.should == "edit" + events[1].described_state.should == "gone_postal" + events[1].calculated_state.should == "gone_postal" + end end end end |