From 8c3e46c093023f9430c9772e81d58c9ee24de229 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Mar 2010 11:26:19 -0700 Subject: clean up more warnings, remove unnecessary methods, fix eval line numbers. [#4193 state:resolved] Signed-off-by: wycats --- activemodel/lib/active_model/dirty.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 5f02929a9d..cb70cf74ee 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -116,19 +116,12 @@ module ActiveModel # person.save # person.previous_changes # => {'name' => ['bob, 'robert']} def previous_changes - previously_changed_attributes + @previously_changed end private # Map of change attr => original value. - def changed_attributes - @changed_attributes ||= {} - end - - # Map of fields that were changed when the model was saved - def previously_changed_attributes - @previously_changed || {} - end + attr_reader :changed_attributes # Handle *_changed? for +method_missing+. def attribute_changed?(attr) -- cgit v1.2.3 From df735cf5b712312a161d703f2606b145ea2d3b85 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Mar 2010 15:08:38 -0700 Subject: fisting uninitialized ivar warnings. [#4198 state:resolved] Signed-off-by: wycats --- activemodel/lib/active_model/dirty.rb | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index cb70cf74ee..d3a6bad6bb 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -91,7 +91,7 @@ module ActiveModel # person.name = 'bob' # person.changed? # => true def changed? - !changed_attributes.empty? + !@changed_attributes.empty? end # List of attributes with unsaved changes. @@ -99,7 +99,7 @@ module ActiveModel # person.name = 'bob' # person.changed # => ['name'] def changed - changed_attributes.keys + @changed_attributes.keys end # Map of changed attrs => [original value, new value]. @@ -120,22 +120,19 @@ module ActiveModel end private - # Map of change attr => original value. - attr_reader :changed_attributes - # Handle *_changed? for +method_missing+. def attribute_changed?(attr) - changed_attributes.include?(attr) + @changed_attributes.include?(attr) end # Handle *_change for +method_missing+. def attribute_change(attr) - [changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) + [@changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) end # Handle *_was for +method_missing+. def attribute_was(attr) - attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) + attribute_changed?(attr) ? @changed_attributes[attr] : __send__(attr) end # Handle *_will_change! for +method_missing+. @@ -146,12 +143,12 @@ module ActiveModel rescue TypeError, NoMethodError end - changed_attributes[attr] = value + @changed_attributes[attr] = value end # Handle reset_*! for +method_missing+. def reset_attribute!(attr) - __send__("#{attr}=", changed_attributes[attr]) if attribute_changed?(attr) + __send__("#{attr}=", @changed_attributes[attr]) if attribute_changed?(attr) end end end -- cgit v1.2.3 From b652aa81216f718f27ba257238d4c27dd54863b2 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 17 Mar 2010 13:30:17 -0700 Subject: cleaning up a bunch of method already defined warnings [#4209 state:resolved] Signed-off-by: wycats --- activemodel/lib/active_model/attribute_methods.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 588976f1d4..7f2959fcde 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -90,13 +90,20 @@ module ActiveModel # # => 'address_id' def define_attr_method(name, value=nil, &block) sing = singleton_class - sing.send :alias_method, "original_#{name}", name + sing.class_eval <<-eorb, __FILE__, __LINE__ + 1 + if method_defined?(:original_#{name}) + undef :original_#{name} + end + alias_method :original_#{name}, :#{name} + eorb if block_given? sing.send :define_method, name, &block else # use eval instead of a block to work around a memory leak in dev # mode in fcgi - sing.class_eval "def #{name}; #{value.to_s.inspect}; end" + sing.class_eval <<-eorb, __FILE__, __LINE__ + 1 + def #{name}; #{value.to_s.inspect}; end + eorb end end @@ -257,8 +264,13 @@ module ActiveModel if respond_to?(generate_method) send(generate_method, attr_name) else + method_name = matcher.method_name(attr_name) + generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__+1 - def #{matcher.method_name(attr_name)}(*args) + if method_defined?(:#{method_name}) + undef :#{method_name} + end + def #{method_name}(*args) send(:#{matcher.method_missing_target}, '#{attr_name}', *args) end STR -- cgit v1.2.3 From 4c7c4061558bb8781da0d54159e3cebcb0a8c07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 26 Mar 2010 01:12:24 +0100 Subject: Remove reference to unexistent methods and fix typo. --- activemodel/lib/active_model/validations.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index ba8648f8c9..6013cdb23b 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -112,7 +112,6 @@ module ActiveModel # end # end # - # This usage applies to +validate_on_create+ and +validate_on_update as well+. def validate(*args, &block) options = args.last if options.is_a?(Hash) && options.key?(:on) -- cgit v1.2.3 From a5d637d86c02d911b07d48bdbfbc4f23fb59d720 Mon Sep 17 00:00:00 2001 From: Jacob Atzen Date: Fri, 12 Mar 2010 10:22:29 +0100 Subject: ActiveModel::Dirty#changes should return a HashWithIndifferentAccess [#4157 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep the Rails style of inject Signed-off-by: José Valim --- activemodel/lib/active_model/dirty.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index d3a6bad6bb..2f6b30dfed 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -107,7 +107,7 @@ module ActiveModel # person.name = 'bob' # person.changes # => { 'name' => ['bill', 'bob'] } def changes - changed.inject({}) { |h, attr| h[attr] = attribute_change(attr); h } + changed.inject(HashWithIndifferentAccess.new){ |h, attr| h[attr] = attribute_change(attr); h } end # Map of attributes that were changed when the model was saved. -- cgit v1.2.3 From f1da7174cc66d70fe8e7352761873f9ff05ca8fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 27 Mar 2010 11:04:21 +0100 Subject: Fix tests added in previous commit. --- activemodel/lib/active_model/dirty.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 2f6b30dfed..57c594f159 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -86,6 +86,11 @@ module ActiveModel attribute_method_affix :prefix => 'reset_', :suffix => '!' end + def initialize(*) + @changed_attributes = {} + super + end + # Do any attributes have unsaved changes? # person.changed? # => false # person.name = 'bob' -- cgit v1.2.3 From c3cea9b59460bb9e67dafcc055f39b8e69542405 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 27 Mar 2010 11:03:36 -0700 Subject: Fix unstated dep on HWIA --- activemodel/lib/active_model/dirty.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 57c594f159..cb67ef7270 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -1,3 +1,5 @@ +require 'active_support/hash_with_indifferent_access' + module ActiveModel # ActiveModel::Dirty provides a way to track changes in your # object in the same way as ActiveRecord does. -- cgit v1.2.3 From a98db7c6ef7384d60a1c7f02d43ee601e2647eea Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 27 Mar 2010 11:50:11 -0700 Subject: Use Array.wrap uniformly --- activemodel/lib/active_model/callbacks.rb | 7 ++++--- activemodel/lib/active_model/errors.rb | 3 ++- activemodel/lib/active_model/serializers/xml.rb | 3 ++- activemodel/lib/active_model/validations.rb | 3 ++- activemodel/lib/active_model/validator.rb | 3 ++- 5 files changed, 12 insertions(+), 7 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index a7e0cf90c1..d4e98de57b 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -1,3 +1,4 @@ +require 'active_support/core_ext/array/wrap' require 'active_support/callbacks' module ActiveModel @@ -91,7 +92,7 @@ module ActiveModel options = callbacks.extract_options! options = { :terminator => "result == false", :scope => [:kind, :name] }.merge(options) - types = Array(options.delete(:only)) + types = Array.wrap(options.delete(:only)) types = [:before, :around, :after] if types.empty? callbacks.each do |callback| @@ -124,10 +125,10 @@ module ActiveModel def self.after_#{callback}(*args, &block) options = args.extract_options! options[:prepend] = true - options[:if] = Array(options[:if]) << "!halted && value != false" + options[:if] = Array.wrap(options[:if]) << "!halted && value != false" set_callback(:#{callback}, :after, *(args << options), &block) end CALLBACK end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index d8320275df..a9a54a90e0 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -1,3 +1,4 @@ +require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/string/inflections' require 'active_support/ordered_hash' @@ -206,7 +207,7 @@ module ActiveModel full_messages = [] each do |attribute, messages| - messages = Array(messages) + messages = Array.wrap(messages) next if messages.empty? if attribute == :base diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index a185204680..c226359ea7 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -1,3 +1,4 @@ +require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/class/attribute_accessors' require 'active_support/core_ext/hash/conversions' @@ -108,7 +109,7 @@ module ActiveModel end def serializable_method_attributes - Array(options[:methods]).inject([]) do |methods, name| + Array.wrap(options[:methods]).inject([]) do |methods, name| methods << MethodAttribute.new(name.to_s, @serializable) if @serializable.respond_to?(name.to_s) methods end diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 6013cdb23b..708557f4ae 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/array/extract_options' +require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/hash/keys' require 'active_model/errors' @@ -115,7 +116,7 @@ module ActiveModel def validate(*args, &block) options = args.last if options.is_a?(Hash) && options.key?(:on) - options[:if] = Array(options[:if]) + options[:if] = Array.wrap(options[:if]) options[:if] << "@_on_validate == :#{options[:on]}" end set_callback(:validate, *args, &block) diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index b61f0cb266..b7c52be3f0 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -1,3 +1,4 @@ +require 'active_support/core_ext/array/wrap' require "active_support/core_ext/module/anonymous" module ActiveModel #:nodoc: @@ -130,7 +131,7 @@ module ActiveModel #:nodoc: # +options+ reader, however the :attributes option will be removed # and instead be made available through the +attributes+ reader. def initialize(options) - @attributes = Array(options.delete(:attributes)) + @attributes = Array.wrap(options.delete(:attributes)) raise ":attributes cannot be blank" if @attributes.empty? super check_validity! -- cgit v1.2.3