diff options
author | Louise Crow <louise.crow@gmail.com> | 2013-09-05 15:39:19 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2013-09-05 15:40:35 +0100 |
commit | 2f1cca7df58c93ef7844831af892a716b9b17f48 (patch) | |
tree | 2c5fa964a729a38fb338f3105adf6d6ec3f6a97e | |
parent | 6518bfde0d974092777522c8dd883fd498b2972c (diff) |
Don't dirty every attribute in checking for whitespace.
Check to see if the stripped version is different before setting it on
the record. If we don't do this, the subsequent call to write_attribute
in Globalize3 which uses attribute_will_change! means we're storing
versions when there hasn't really been any change.
-rw-r--r-- | spec/models/public_body_spec.rb | 46 | ||||
-rw-r--r-- | vendor/plugins/strip_attributes/lib/strip_attributes.rb | 9 |
2 files changed, 37 insertions, 18 deletions
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 |