From a4cc88d0851343ac16e2294c06c5a4101189c410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 20 Jan 2014 21:57:47 -0200 Subject: Extract all attribute changed work to its own method This will make easier to hook on this feature to customize the behavior --- activerecord/lib/active_record/attribute_methods/dirty.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 19e81abba5..f7065d183f 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -43,6 +43,13 @@ module ActiveRecord def write_attribute(attr, value) attr = attr.to_s + save_changed_attribute(attr, value) + + # Carry on. + super(attr, value) + end + + def save_changed_attribute(attr, value) # The attribute already has an unsaved change. if attribute_changed?(attr) old = changed_attributes[attr] @@ -51,9 +58,6 @@ module ActiveRecord old = clone_attribute_value(:read_attribute, attr) changed_attributes[attr] = old if _field_changed?(attr, old, value) end - - # Carry on. - super(attr, value) end def update_record(*) -- cgit v1.2.3 From a57a2bcf4a2c29519d553277e4439790ca443cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 20 Jan 2014 21:59:20 -0200 Subject: Make enum feature work with dirty methods To make this possible we have to override the save_changed_attribute hook. --- activerecord/lib/active_record/enum.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 3deb2d65f8..06e87cf854 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -63,6 +63,12 @@ module ActiveRecord # # Conversation.where("status <> ?", Conversation.statuses[:archived]) module Enum + DEFINED_ENUMS = [] # :nodoc: + + def enum_attribute?(attr_name) # :nodoc: + DEFINED_ENUMS.include?(attr_name.to_sym) + end + def enum(definitions) klass = self definitions.each do |name, values| @@ -70,6 +76,8 @@ module ActiveRecord enum_values = ActiveSupport::HashWithIndifferentAccess.new name = name.to_sym + DEFINED_ENUMS.unshift name + # def self.statuses statuses end klass.singleton_class.send(:define_method, name.to_s.pluralize) { enum_values } @@ -114,7 +122,16 @@ module ActiveRecord private def _enum_methods_module @_enum_methods_module ||= begin - mod = Module.new + mod = Module.new do + def save_changed_attribute(attr_name, value) + if self.class.enum_attribute?(attr_name) + old = clone_attribute_value(:read_attribute, attr_name) + changed_attributes[attr_name] = self.class.public_send(attr_name.pluralize).key old + else + super + end + end + end include mod mod end -- cgit v1.2.3 From a0520fceff9148ebfbb2e09745ba1416bceef2bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 20 Jan 2014 23:10:48 -0200 Subject: Add more tests for the dirty feature for enums --- .../lib/active_record/attribute_methods/dirty.rb | 1 - activerecord/lib/active_record/enum.rb | 26 ++++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index f7065d183f..68168bb729 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -45,7 +45,6 @@ module ActiveRecord save_changed_attribute(attr, value) - # Carry on. super(attr, value) end diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 06e87cf854..0990a4a871 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -123,14 +123,26 @@ module ActiveRecord def _enum_methods_module @_enum_methods_module ||= begin mod = Module.new do - def save_changed_attribute(attr_name, value) - if self.class.enum_attribute?(attr_name) - old = clone_attribute_value(:read_attribute, attr_name) - changed_attributes[attr_name] = self.class.public_send(attr_name.pluralize).key old - else - super + private + def save_changed_attribute(attr_name, value) + if self.class.enum_attribute?(attr_name) + if attribute_changed?(attr_name) + old = changed_attributes[attr_name] + + if self.class.public_send(attr_name.pluralize)[old] == value + changed_attributes.delete(attr_name) + end + else + old = clone_attribute_value(:read_attribute, attr_name) + + if old != value + changed_attributes[attr_name] = self.class.public_send(attr_name.pluralize).key old + end + end + else + super + end end - end end include mod mod -- cgit v1.2.3 From 5620e62f3f1920e70199a870a0a3c4feaedb9f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 21 Jan 2014 20:32:52 -0200 Subject: Store the enum values in the DEFINED_ENUM constant This will make simpler to compare if the values changed in the save_changed_attribute method. --- activerecord/lib/active_record/enum.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 0990a4a871..77a5fe9ae0 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -63,10 +63,10 @@ module ActiveRecord # # Conversation.where("status <> ?", Conversation.statuses[:archived]) module Enum - DEFINED_ENUMS = [] # :nodoc: + DEFINED_ENUMS = {} # :nodoc: - def enum_attribute?(attr_name) # :nodoc: - DEFINED_ENUMS.include?(attr_name.to_sym) + def enum_mapping_for(attr_name) # :nodoc: + DEFINED_ENUMS[attr_name.to_sym] end def enum(definitions) @@ -76,8 +76,6 @@ module ActiveRecord enum_values = ActiveSupport::HashWithIndifferentAccess.new name = name.to_sym - DEFINED_ENUMS.unshift name - # def self.statuses statuses end klass.singleton_class.send(:define_method, name.to_s.pluralize) { enum_values } @@ -115,6 +113,8 @@ module ActiveRecord # def active!() update! status: :active end define_method("#{value}!") { update! name => value } end + + DEFINED_ENUMS[name] = enum_values end end end @@ -125,18 +125,18 @@ module ActiveRecord mod = Module.new do private def save_changed_attribute(attr_name, value) - if self.class.enum_attribute?(attr_name) + if (mapping = self.class.enum_mapping_for(attr_name)) if attribute_changed?(attr_name) old = changed_attributes[attr_name] - if self.class.public_send(attr_name.pluralize)[old] == value + if mapping[old] == value changed_attributes.delete(attr_name) end else old = clone_attribute_value(:read_attribute, attr_name) if old != value - changed_attributes[attr_name] = self.class.public_send(attr_name.pluralize).key old + changed_attributes[attr_name] = mapping.key old end end else -- cgit v1.2.3 From 55f6c8c908fea2609cbc8503f8d87460fd1b16b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 21 Jan 2014 20:35:21 -0200 Subject: Use string as keys --- activerecord/lib/active_record/enum.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 77a5fe9ae0..53dde5e564 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -66,7 +66,7 @@ module ActiveRecord DEFINED_ENUMS = {} # :nodoc: def enum_mapping_for(attr_name) # :nodoc: - DEFINED_ENUMS[attr_name.to_sym] + DEFINED_ENUMS[attr_name.to_s] end def enum(definitions) @@ -114,7 +114,7 @@ module ActiveRecord define_method("#{value}!") { update! name => value } end - DEFINED_ENUMS[name] = enum_values + DEFINED_ENUMS[name.to_s] = enum_values end end end -- cgit v1.2.3