aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/info_request_spec.rb
diff options
context:
space:
mode:
authorMark Longair <mhl@pobox.com>2013-08-19 10:34:05 +0100
committerMark Longair <mhl@pobox.com>2013-08-20 12:11:45 +0100
commite5855f7fd2657574c5af99890c63d530ca3bb5d0 (patch)
tree338b64fd532af727027ada82b7a4d2042855e305 /spec/models/info_request_spec.rb
parent32625f08b9c7e7bb65bd24099e64d7d17be5e45e (diff)
Improve calculation of PublicBody statistics columns
On PublicBody, we don't need to update info_requests_count because that's already done with :counter_cache. On the other hand, info_requests_successful_count and info_requests_not_held_count can't be updated easily with counter_cache (since they need conditions to be attached). Instead we update them in post_save and post_destroy, as suggested here: http://blog.douglasfshearer.com/post/17495285851/custom-counter-cache-with-conditions This also adds tests to ensure that the after_(save|destroy) callbacks are called and that they modify the counts correctly.
Diffstat (limited to 'spec/models/info_request_spec.rb')
-rw-r--r--spec/models/info_request_spec.rb66
1 files changed, 66 insertions, 0 deletions
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index 3451e018f..3f2e02189 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -857,4 +857,70 @@ describe InfoRequest do
end
end
end
+
+ describe 'when saving an info_request' do
+
+ before do
+ @info_request = InfoRequest.new(:external_url => 'http://www.example.com',
+ :external_user_name => 'Example User',
+ :title => 'Some request or other',
+ :public_body => public_bodies(:geraldine_public_body))
+ end
+
+ it "should call purge_in_cache and update_counter_cache" do
+ @info_request.should_receive(:purge_in_cache)
+ # Twice - once for save, once for destroy:
+ @info_request.should_receive(:update_counter_cache).twice
+ @info_request.save!
+ @info_request.destroy
+ end
+
+ end
+
+ describe 'when destroying an info_request' do
+
+ before do
+ @info_request = InfoRequest.new(:external_url => 'http://www.example.com',
+ :external_user_name => 'Example User',
+ :title => 'Some request or other',
+ :public_body => public_bodies(:geraldine_public_body))
+ end
+
+ it "should call update_counter_cache" do
+ @info_request.save!
+ @info_request.should_receive(:update_counter_cache)
+ @info_request.destroy
+ end
+
+ end
+
+ describe 'when changing a described_state' do
+
+ it "should change the counts on its PublicBody" do
+ pb = public_bodies(:geraldine_public_body)
+ old_successful_count = pb.info_requests_successful_count
+ old_not_held_count = pb.info_requests_not_held_count
+ ir = InfoRequest.new(:external_url => 'http://www.example.com',
+ :external_user_name => 'Example User',
+ :title => 'Some request or other',
+ :described_state => 'partially_successful',
+ :public_body => pb)
+ ir.save!
+ pb.info_requests_successful_count.should == (old_successful_count + 1)
+ ir.described_state = 'not_held'
+ ir.save!
+ pb.info_requests_successful_count.should == old_successful_count
+ pb.info_requests_not_held_count.should == (old_not_held_count + 1)
+ ir.described_state = 'successful'
+ ir.save!
+ pb.info_requests_successful_count.should == (old_successful_count + 1)
+ pb.info_requests_not_held_count.should == old_not_held_count
+ ir.destroy
+ pb.info_requests_successful_count.should == old_successful_count
+ pb.info_requests_successful_count.should == old_not_held_count
+ end
+
+ end
+
+
end