diff options
-rw-r--r-- | app/models/info_request.rb | 6 | ||||
-rw-r--r-- | app/models/public_body.rb | 24 | ||||
-rw-r--r-- | lib/tasks/stats.rake | 5 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 7 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 46 | ||||
-rw-r--r-- | vendor/plugins/strip_attributes/lib/strip_attributes.rb | 9 |
7 files changed, 74 insertions, 25 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 91bd37d9f..c0b8e2fca 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -1128,12 +1128,18 @@ public after_save :update_counter_cache after_destroy :update_counter_cache def update_counter_cache + PublicBody.skip_callback(:save, :after, :purge_in_cache) self.public_body.info_requests_not_held_count = InfoRequest.where( :public_body_id => self.public_body.id, :described_state => 'not_held').count self.public_body.info_requests_successful_count = InfoRequest.where( :public_body_id => self.public_body.id, :described_state => ['successful', 'partially_successful']).count + self.public_body.without_revision do + public_body.no_xapian_reindex = true + public_body.save + end + PublicBody.set_callback(:save, :after, :purge_in_cache) end def for_admin_column diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 300fdb956..3154be632 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -40,6 +40,7 @@ class PublicBody < ActiveRecord::Base has_many :info_requests, :order => 'created_at desc' has_many :track_things, :order => 'created_at desc' has_many :censor_rules, :order => 'created_at desc' + attr_accessor :no_xapian_reindex has_tag_string before_save :set_api_key, :set_default_publication_scheme @@ -60,12 +61,23 @@ class PublicBody < ActiveRecord::Base # XXX - Don't like repeating this! def calculate_cached_fields(t) - t.first_letter = t.name.scan(/^./mu)[0].upcase unless t.name.nil? or t.name.empty? + PublicBody.set_first_letter(t) short_long_name = t.name short_long_name = t.short_name if t.short_name and !t.short_name.empty? t.url_name = MySociety::Format.simplify_url_part(short_long_name, 'body') end + # Set the first letter on a public body or translation + def PublicBody.set_first_letter(instance) + unless instance.name.nil? or instance.name.empty? + # we use a regex to ensure it works with utf-8/multi-byte + first_letter = instance.name.scan(/^./mu)[0].upcase + if first_letter != instance.first_letter + instance.first_letter = first_letter + end + end + end + def translated_versions translations end @@ -130,8 +142,7 @@ class PublicBody < ActiveRecord::Base # Set the first letter, which is used for faster queries before_save(:set_first_letter) def set_first_letter - # we use a regex to ensure it works with utf-8/multi-byte - self.first_letter = self.name.scan(/./mu)[0].upcase + PublicBody.set_first_letter(self) end # If tagged "not_apply", then FOI/EIR no longer applies to authority at all @@ -177,7 +188,11 @@ class PublicBody < ActiveRecord::Base end acts_as_versioned - self.non_versioned_columns << 'created_at' << 'updated_at' << 'first_letter' << 'api_key' << 'info_requests_count' + self.non_versioned_columns << 'created_at' << 'updated_at' << 'first_letter' << 'api_key' + self.non_versioned_columns << 'info_requests_count' << 'info_requests_successful_count' + self.non_versioned_columns << 'info_requests_not_held_count' << 'info_requests_overdue' + self.non_versioned_columns << 'info_requests_overdue_count' + class Version attr_accessor :created_at @@ -231,6 +246,7 @@ class PublicBody < ActiveRecord::Base def reindex_requested_from if self.changes.include?('url_name') for info_request in self.info_requests + for info_request_event in info_request.info_request_events info_request_event.xapian_mark_needs_index end diff --git a/lib/tasks/stats.rake b/lib/tasks/stats.rake index 2a02b1716..4eda27289 100644 --- a/lib/tasks/stats.rake +++ b/lib/tasks/stats.rake @@ -111,7 +111,10 @@ namespace :stats do end end public_body.info_requests_overdue_count = overdue_count + very_overdue_count - public_body.save! + public_body.no_xapian_reindex = true + public_body.without_revision do + public_body.save! + end end end end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 2c605a139..9c4e16c67 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -174,7 +174,7 @@ describe RequestController, "when changing things that appear on the request pag ir.save! PurgeRequest.all().first.model_id.should == ir.id end - it "should not create more than one entry for any given resourcce" do + it "should not create more than one entry for any given resource" do ir = info_requests(:fancy_dog_request) ir.prominence = 'hidden' ir.save! diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 3f2e02189..ab36a201c 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -896,8 +896,9 @@ describe InfoRequest do describe 'when changing a described_state' do - it "should change the counts on its PublicBody" do + it "should change the counts on its PublicBody without saving a new version" do pb = public_bodies(:geraldine_public_body) + old_version_count = pb.versions.count 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', @@ -909,15 +910,19 @@ describe InfoRequest do pb.info_requests_successful_count.should == (old_successful_count + 1) ir.described_state = 'not_held' ir.save! + pb.reload 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.reload pb.info_requests_successful_count.should == (old_successful_count + 1) pb.info_requests_not_held_count.should == old_not_held_count ir.destroy + pb.reload pb.info_requests_successful_count.should == old_successful_count pb.info_requests_successful_count.should == old_not_held_count + pb.versions.count.should == old_version_count end end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 90affaaaa..3578c0e9c 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -136,36 +136,32 @@ describe PublicBody, " when saving" do @public_body = PublicBody.new end + def set_default_attributes(public_body) + public_body.name = "Testing Public Body" + public_body.short_name = "TPB" + public_body.request_email = "request@localhost" + public_body.last_edit_editor = "*test*" + public_body.last_edit_comment = "This is a test" + end + it "should not be valid without setting some parameters" do @public_body.should_not be_valid end it "should not be valid with misformatted request email" do - @public_body.name = "Testing Public Body" - @public_body.short_name = "TPB" + set_default_attributes(@public_body) @public_body.request_email = "requestBOOlocalhost" - @public_body.last_edit_editor = "*test*" - @public_body.last_edit_comment = "This is a test" @public_body.should_not be_valid @public_body.should have(1).errors_on(:request_email) end it "should save" do - @public_body.name = "Testing Public Body" - @public_body.short_name = "TPB" - @public_body.request_email = "request@localhost" - @public_body.last_edit_editor = "*test*" - @public_body.last_edit_comment = "This is a test" + set_default_attributes(@public_body) @public_body.save! end it "should update first_letter" do - @public_body.name = "Testing Public Body" - @public_body.short_name = "TPB" - @public_body.request_email = "request@localhost" - @public_body.last_edit_editor = "*test*" - @public_body.last_edit_comment = "This is a test" - + set_default_attributes(@public_body) @public_body.first_letter.should be_nil @public_body.save! @public_body.first_letter.should == 'T' @@ -178,6 +174,26 @@ describe PublicBody, " when saving" do public_body.name.should == "Mark's Public Body" end + + it 'should not create a new version when nothing has changed' do + @public_body.versions.size.should == 0 + set_default_attributes(@public_body) + @public_body.save! + @public_body.versions.size.should == 1 + @public_body.save! + @public_body.versions.size.should == 1 + end + + it 'should create a new version if something has changed' do + @public_body.versions.size.should == 0 + set_default_attributes(@public_body) + @public_body.save! + @public_body.versions.size.should == 1 + @public_body.name = 'Test' + @public_body.save! + @public_body.versions.size.should == 2 + end + end describe PublicBody, "when searching" do diff --git a/vendor/plugins/strip_attributes/lib/strip_attributes.rb b/vendor/plugins/strip_attributes/lib/strip_attributes.rb index bb93aa9a7..130d10185 100644 --- a/vendor/plugins/strip_attributes/lib/strip_attributes.rb +++ b/vendor/plugins/strip_attributes/lib/strip_attributes.rb @@ -8,12 +8,15 @@ module StripAttributes attribute_names.each do |attribute_name| value = record[attribute_name] if value.respond_to?(:strip) - record[attribute_name] = (value.nil?) ? nil : value.strip + stripped = value.strip + if stripped != value + record[attribute_name] = (value.nil?) ? nil : stripped + end end end end end - + # Necessary because Rails has removed the narrowing of attributes using :only # and :except on Base#attributes def self.narrow(attribute_names, options) @@ -28,7 +31,7 @@ module StripAttributes attribute_names & only else raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})" - end + end end end end |