Skip to content

Loading…

Don't mark the store as changed if an attribute isn't changed. #6934

Merged
merged 1 commit into from

2 participants

@kennyj

If an attribute isn't modified, I think we shouldn't mark the store as changed.

@rafaelfranca rafaelfranca commented on an outdated diff
activerecord/lib/active_record/store.rb
@@ -58,8 +58,10 @@ def store_accessor(store_attribute, *keys)
keys.each do |key|
define_method("#{key}=") do |value|
initialize_store_attribute(store_attribute)
- send(store_attribute)[key] = value
- send :"#{store_attribute}_will_change!"
+ if value != send(store_attribute)[key]
@rafaelfranca Ruby on Rails member

I think we should cache the send(store_attribute)

@kennyj
kennyj added a note

Thanks @rafaelfranca .
I updated it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@rafaelfranca rafaelfranca merged commit 55456ad into rails:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 10 additions and 2 deletions.
  1. +5 −2 activerecord/lib/active_record/store.rb
  2. +5 −0 activerecord/test/cases/store_test.rb
View
7 activerecord/lib/active_record/store.rb
@@ -58,8 +58,11 @@ def store_accessor(store_attribute, *keys)
keys.each do |key|
define_method("#{key}=") do |value|
initialize_store_attribute(store_attribute)
- send(store_attribute)[key] = value
- send :"#{store_attribute}_will_change!"
+ attribute = send(store_attribute)
+ if value != attribute[key]
+ attribute[key] = value
+ send :"#{store_attribute}_will_change!"
+ end
end
define_method(key) do
View
5 activerecord/test/cases/store_test.rb
@@ -34,6 +34,11 @@ class StoreTest < ActiveRecord::TestCase
assert @john.settings_changed?
end
+ test "updating the store won't mark it as changed if an attribute isn't changed" do
+ @john.color = @john.color
+ assert !@john.settings_changed?
+ end
+
test "object initialization with not nullable column" do
assert_equal true, @john.remember_login
end
Something went wrong with that request. Please try again.