diff options
-rw-r--r-- | app/controllers/admin_general_controller.rb | 4 | ||||
-rw-r--r-- | app/controllers/admin_request_controller.rb | 12 | ||||
-rw-r--r-- | app/models/info_request.rb | 22 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 64 |
4 files changed, 89 insertions, 13 deletions
diff --git a/app/controllers/admin_general_controller.rb b/app/controllers/admin_general_controller.rb index 3d2bf74df..cbb381e21 100644 --- a/app/controllers/admin_general_controller.rb +++ b/app/controllers/admin_general_controller.rb @@ -4,7 +4,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: admin_general_controller.rb,v 1.3 2009-03-10 12:04:55 tony Exp $ +# $Id: admin_general_controller.rb,v 1.4 2009-04-08 10:45:33 louise Exp $ class AdminGeneralController < AdminController def index @@ -19,7 +19,7 @@ class AdminGeneralController < AdminController @requires_admin_requests = InfoRequest.find(:all, :select => '*, ' + InfoRequest.last_event_time_clause + ' as last_event_time', :conditions => ["described_state = 'requires_admin'"], :order => "last_event_time") @error_message_requests = InfoRequest.find(:all, :select => '*, ' + InfoRequest.last_event_time_clause + ' as last_event_time', :conditions => ["described_state = 'error_message'"], :order => "last_event_time") @blank_contacts = PublicBody.find(:all, :conditions => ["request_email = ''"], :order => "updated_at") - @ten_days_old_unclassified = InfoRequest.find(:all, :select => '*, ' + InfoRequest.last_event_time_clause + ' as last_event_time', :conditions => [ "awaiting_description = ? and " + InfoRequest.last_event_time_clause + " < ? and prominence != 'backpage'", true, Time.now() - 10.days ], :order => "last_event_time", :limit => 50) + @ten_days_old_unclassified = InfoRequest.find_old_unclassified(limit=50) @holding_pen_messages = InfoRequest.holding_pen_request.incoming_messages end diff --git a/app/controllers/admin_request_controller.rb b/app/controllers/admin_request_controller.rb index 18a577443..15faacac2 100644 --- a/app/controllers/admin_request_controller.rb +++ b/app/controllers/admin_request_controller.rb @@ -4,7 +4,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: admin_request_controller.rb,v 1.32 2009-04-08 04:51:37 francis Exp $ +# $Id: admin_request_controller.rb,v 1.33 2009-04-08 10:45:33 louise Exp $ class AdminRequestController < AdminController def index @@ -19,15 +19,7 @@ class AdminRequestController < AdminController end def list_old_unclassified - @info_requests = InfoRequest.find( - :all, - :select => '*, ' + InfoRequest.last_event_time_clause + ' as last_event_time', - :conditions => [ - "awaiting_description = ? and " + InfoRequest.last_event_time_clause + " < ? and prominence != 'backpage'", - true, Time.now() - 10.days - ], - :order => "last_event_time" - ) + @info_requests = InfoRequest.find_old_unclassified end def show diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 0dec0528a..7ea355a18 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -23,7 +23,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: info_request.rb,v 1.183 2009-04-08 07:31:07 francis Exp $ +# $Id: info_request.rb,v 1.184 2009-04-08 10:45:34 louise Exp $ require 'digest/sha1' require File.join(File.dirname(__FILE__),'../../vendor/plugins/acts_as_xapian/lib/acts_as_xapian') @@ -73,6 +73,8 @@ class InfoRequest < ActiveRecord::Base 'foi', # Freedom of Information Act 'eir', # Environmental Information Regulations ] + + OLD_AGE_IN_DAYS = 10.days def after_initialize if self.described_state.nil? @@ -673,6 +675,24 @@ public def InfoRequest.last_event_time_clause '(select created_at from info_request_events where info_request_events.info_request_id = info_requests.id order by created_at desc limit 1)' end + + def InfoRequest.find_old_unclassified(limit=nil) + params = {:select => "*, #{last_event_time_clause} as last_event_time", + :conditions => ["awaiting_description = ? and #{last_event_time_clause} < ? and prominence != 'backpage'", + true, Time.now() - OLD_AGE_IN_DAYS], + :order => "last_event_time"} + params[:limit] = limit if limit + find(:all, params) + end + + def is_old_unclassified? + return false if !awaiting_description + return false if prominence == 'backpage' + last_event = get_last_event + return false unless last_event + return false if last_event.created_at >= Time.now - OLD_AGE_IN_DAYS + return true + end # List of incoming messages to followup, by unique email def who_can_followup_to diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 3147965c4..cbcf7eecc 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -184,4 +184,68 @@ describe InfoRequest do end + describe 'when asked for old unclassified requests' do + + before do + Time.stub!(:now).and_return(Time.utc(2007, 11, 12, 23, 59)) + end + + it 'should ask for requests using any limit param supplied' do + InfoRequest.should_receive(:find).with(:all, {:select => anything, + :order => anything, + :conditions=> anything, + :limit => 5}) + InfoRequest.find_old_unclassified(limit=5) + end + + it 'should not limit the number of requests returned by default' do + InfoRequest.should_not_receive(:find).with(:all, {:select => anything, + :order => anything, + :conditions=> anything, + :limit => anything}) + InfoRequest.find_old_unclassified + end + + it 'should ask the database for requests that are awaiting description, older than 10 days old and not backpaged' do + InfoRequest.should_receive(:find).with(:all, + {:select=>"*, (select created_at from info_request_events where info_request_events.info_request_id = info_requests.id order by created_at desc limit 1) as last_event_time", + :order=>"last_event_time", + :conditions=>["awaiting_description = ? and (select created_at from info_request_events where info_request_events.info_request_id = info_requests.id order by created_at desc limit 1) < ? and prominence != 'backpage'", + true, Time.now - 10.days]}) + InfoRequest.find_old_unclassified + end + + end + + describe 'when an instance is asked if it is old and unclassified' do + + before do + Time.stub!(:now).and_return(Time.utc(2007, 11, 12, 23, 59)) + @mock_event = mock_model(InfoRequestEvent, :created_at => Time.now - 11.days) + @info_request = InfoRequest.new(:prominence => 'normal', + :awaiting_description => true, + :info_request_events => [@mock_event]) + end + + it 'should return false if it is not awaiting description' do + @info_request.stub!(:awaiting_description).and_return(false) + @info_request.is_old_unclassified?.should be_false + end + + it 'should return false if it\'s last event occurred less than 10 days ago' do + @mock_event.stub!(:created_at).and_return(Time.now - 9.days) + @info_request.is_old_unclassified?.should be_false + end + + it 'should return false if it\'s backpaged' do + @info_request.stub!(:prominence).and_return('backpage') + @info_request.is_old_unclassified?.should be_false + end + + it 'should return true if it is awaiting description, hasn\'t had an event in 10 days and is not backpaged' do + @info_request.is_old_unclassified?.should be_true + end + + end + end |