Permalink
Browse files

SettingObject: Don't save if there are no changes

  • Loading branch information...
1 parent fcd687c commit af8a8855f6daa064588e7f20c6becf219bc3c743 @ledermann committed Mar 4, 2013
Showing with 13 additions and 0 deletions.
  1. +6 −0 lib/app/models/setting_object.rb
  2. +7 −0 spec/queries_spec.rb
@@ -36,5 +36,11 @@ def method_missing(method_name, *args, &block)
def target_class
target_type.constantize
end
+
+ def update(*)
+ # Patch ActiveRecord to save serialized attributes only if they are changed
+ # https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/attribute_methods/dirty.rb#L70
+ super(changed) if changed?
+ end
end
end
View
@@ -97,5 +97,12 @@
user.settings(:dashboard).update_attributes! :foo => 'bar'
}.to perform_queries(1)
end
+
+ it "should not touch database if there are no changes made" do
+ expect {
+ user.settings(:dashboard).update_attributes :theme => 'pink'
+ user.settings(:calendar).update_attributes :scope => 'all'
+ }.to perform_queries(0)
+ end
end
end

0 comments on commit af8a885

Please sign in to comment.