Skip to content

Commit

Permalink
Merge pull request #15 from rtomayko/noop-blob-column-writes
Browse files Browse the repository at this point in the history
Don't write blob column if no attributes changed
  • Loading branch information
technoweenie committed Dec 3, 2013
2 parents c33b1d8 + 405569b commit a76c8de
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/serializable_attributes/schema.rb
Expand Up @@ -147,7 +147,9 @@ def reload(options = nil)

@model.before_save do |r|
schema = r.class.send("#{data_field}_schema")
r.send("#{blob_field}=", schema.encode(r.send(data_field)))
if r.send(blob_field).nil? || !r.send(changed_ivar).empty?
r.send("#{blob_field}=", schema.encode(r.send(data_field)))
end
end
end

Expand Down
1 change: 1 addition & 0 deletions test/serialized_attributes_test.rb
Expand Up @@ -111,6 +111,7 @@ def @record.title=(v)
test "ignores data with extra keys" do
@record.raw_data = self.class.format.encode(self.class.raw_hash.merge(:foo => :bar))
assert_not_nil @record.title # no undefined foo= error
@record.title = "changed" # force a change / rewrite
assert_equal false, @record.save # extra before_save cancels the operation
assert_equal self.class.raw_hash.merge(:active => 1).stringify_keys.keys.sort, self.class.format.decode(@record.raw_data).keys.sort
end
Expand Down

0 comments on commit a76c8de

Please sign in to comment.