diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-07-28 23:52:53 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-07-30 17:54:00 -0500 |
commit | 94dabf9b4bf14de665cb17570209078f16920e54 (patch) | |
tree | 467bcebef4282d94631fe77fc857cdbf999ed438 /activerecord/lib | |
parent | c2b075bed084a59a674469d09db016aaa3365c2e (diff) | |
download | rails-94dabf9b4bf14de665cb17570209078f16920e54.tar.gz rails-94dabf9b4bf14de665cb17570209078f16920e54.tar.bz2 rails-94dabf9b4bf14de665cb17570209078f16920e54.zip |
Generate methods for all suffixes
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 15 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/read.rb | 11 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 4 |
3 files changed, 12 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index dc42f05635..9a5d719179 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -65,8 +65,7 @@ module ActiveRecord def define_attribute_methods return if generated_methods? columns_hash.keys.each do |name| - # TODO: Generate for all defined suffixes - ["", "=", "?"].each do |suffix| + attribute_method_suffixes.each do |suffix| method_name = "#{name}#{suffix}" unless instance_method_already_implemented?(method_name) generate_method = "define_attribute_method#{suffix}" @@ -81,14 +80,10 @@ module ActiveRecord end # Checks whether the method is defined in the model or any of its subclasses - # that also derive from Active Record. Raises DangerousAttributeError if the - # method is defined by Active Record though. + # that also derive from Active Record. def instance_method_already_implemented?(method_name) method_name = method_name.to_s - return true if method_name =~ /^id(=$|\?$|$)/ # TODO: Check against all defined suffixes - @_defined_class_methods ||= ancestors.first(ancestors.index(ActiveRecord::Base)).sum([]) { |m| m.public_instance_methods(false) | m.private_instance_methods(false) | m.protected_instance_methods(false) }.map {|m| m.to_s }.to_set - @@_defined_activerecord_methods ||= (ActiveRecord::Base.public_instance_methods(false) | ActiveRecord::Base.private_instance_methods(false) | ActiveRecord::Base.protected_instance_methods(false)).map{|m| m.to_s }.to_set - raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name) + @_defined_class_methods ||= ancestors.first(ancestors.index(ActiveRecord::Base)).sum([]) { |m| m.public_instance_methods(false) | m.private_instance_methods(false) | m.protected_instance_methods(false) }.map(&:to_s).to_set @_defined_class_methods.include?(method_name) end @@ -124,9 +119,7 @@ module ActiveRecord # Evaluate the definition for an attribute related method def evaluate_attribute_method(attr_name, method_definition, method_name) - unless method_name.to_s == primary_key.to_s - generated_methods << method_name - end + generated_methods << method_name begin class_eval(method_definition, __FILE__, __LINE__) diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 9f948536fc..dda700d04c 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -3,9 +3,9 @@ module ActiveRecord module Read extend ActiveSupport::Concern - # included do - # attribute_method_suffix "" - # end + included do + attribute_method_suffix "" + end module ClassMethods protected @@ -74,6 +74,11 @@ module ActiveRecord "#{attr_name} was supposed to be a #{self.class.serialized_attributes[attr_name]}, but was a #{unserialized_object.class.to_s}" end end + + private + def attribute(attribute_name) + read_attribute(attribute_name) + end end end end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f3cb905754..b64c76b558 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -144,10 +144,6 @@ module ActiveRecord #:nodoc: class Rollback < ActiveRecordError end - # Raised when attribute has a name reserved by Active Record (when attribute has name of one of Active Record instance methods). - class DangerousAttributeError < ActiveRecordError - end - # Raised when you've tried to access a column which wasn't loaded by your finder. # Typically this is because <tt>:select</tt> has been specified. class MissingAttributeError < NoMethodError |