diff options
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/customstates.rb | 62 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 36 |
2 files changed, 98 insertions, 0 deletions
diff --git a/spec/models/customstates.rb b/spec/models/customstates.rb new file mode 100644 index 000000000..de8d04ffb --- /dev/null +++ b/spec/models/customstates.rb @@ -0,0 +1,62 @@ +module InfoRequestCustomStates + # Mixin methods for InfoRequest + def theme_display_status(status) + if status == 'deadline_extended' + _("Deadline extended.") + elsif status == 'wrong_response' + _("Wrong Response.") + else + raise _("unknown status ") + status + end + end + + def theme_extra_states + return ['deadline_extended', + 'wrong_response'] + end + + + def theme_calculate_status + return 'waiting_classification' if self.awaiting_description + waiting_response = self.described_state == "waiting_response" || self.described_state == "deadline_extended" + return self.described_state unless waiting_response + if self.described_state == 'deadline_extended' + return 'deadline_extended' if + Time.now.strftime("%Y-%m-%d") < self.date_deadline_extended.strftime("%Y-%m-%d") + return 'waiting_response_very_overdue' if + Time.now.strftime("%Y-%m-%d") > Holiday.due_date_from(self.date_deadline_extended, 15).strftime("%Y-%m-%d") + return 'waiting_response_overdue' + end + return 'waiting_response_very_overdue' if + Time.now.strftime("%Y-%m-%d") > self.date_very_overdue_after.strftime("%Y-%m-%d") + return 'waiting_response_overdue' if + Time.now.strftime("%Y-%m-%d") > self.date_response_required_by.strftime("%Y-%m-%d") + return 'waiting_response' + end + + def date_deadline_extended + # XXX shouldn't this be 15 days after the date the status was + # changed to "deadline extended"? Or perhaps 15 days ater the + # initial request due date? + return Holiday.due_date_from(self.date_response_required_by, 15) + end + +end + +module RequestControllerCustomStates + + def theme_describe_state(info_request) + # called after the core describe_state code. It should + # end by raising an error if the status is unknown + if info_request.calculate_status == 'deadline_extended' + flash[:notice] = _("Authority has requested extension of the deadline.") + redirect_to unhappy_url(info_request) + elsif info_request.calculate_status == 'wrong_response' + flash[:notice] = _("Oh no! Sorry to hear that your request was wrong. Here is what to do now.") + redirect_to unhappy_url(info_request) + else + raise "unknown calculate_status " + info_request.calculate_status + end + end + +end diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 96a4f5bc8..91b1b0876 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -159,6 +159,42 @@ describe InfoRequest do end end + + describe "when using a plugin and calculating the status" do + + fixtures :info_requests + + before do + InfoRequest.send(:require, File.expand_path(File.dirname(__FILE__) + '/customstates')) + @ir = info_requests(:naughty_chicken_request) + @ir.load_custom_states + end + + it "rejects invalid states" do + lambda {@ir.set_described_state("foo")}.should raise_error(ActiveRecord::RecordInvalid) + end + + it "accepts core states" do + @ir.set_described_state("requires_admin") + end + + it "accepts extended states" do + # this time would normally be "overdue" + Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01)) + @ir.set_described_state("deadline_extended") + @ir.display_status.should == 'Deadline extended.' + @ir.date_deadline_extended + end + + it "is not overdue if it's had the deadline extended" do + when_overdue = Time.utc(2007, 11, 10, 00, 01) + 16.days + Time.stub!(:now).and_return(when_overdue) + @ir.calculate_status.should == 'waiting_response_overdue' + end + + end + + describe "when calculating the status for a school" do fixtures :info_requests, :info_request_events, :holidays, :public_bodies, :public_body_translations |