From 9d1f6edbe0aa6f95f55464136e39fb4e2e4643f2 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 27 Apr 2012 20:44:08 -0300 Subject: Return earlier if attribute already changed in *_will_change! methods Avoid obtaining the value and duplicating it when possible. --- activemodel/lib/active_model/dirty.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index d327913824..7f7fb90d87 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -151,13 +151,15 @@ module ActiveModel # Handle *_will_change! for +method_missing+. def attribute_will_change!(attr) + return if attribute_changed?(attr) + begin value = __send__(attr) value = value.duplicable? ? value.clone : value rescue TypeError, NoMethodError end - changed_attributes[attr] = value unless changed_attributes.include?(attr) + changed_attributes[attr] = value end # Handle reset_*! for +method_missing+. -- cgit v1.2.3