diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-20 06:45:34 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-20 06:45:34 +0000 |
commit | 1aff68d61574410fa942aa34ec261c68565e48c1 (patch) | |
tree | bc23d1bb58cedaf97a8cf4afff59454b25129735 /activerecord/lib | |
parent | 89c0ab6b82410b826dacb53803a36b935bae36cb (diff) | |
download | rails-1aff68d61574410fa942aa34ec261c68565e48c1.tar.gz rails-1aff68d61574410fa942aa34ec261c68565e48c1.tar.bz2 rails-1aff68d61574410fa942aa34ec261c68565e48c1.zip |
Fix bug introduced by changeset 3679 which caused custom attribute? methods to be overridden. Also ensure that ? methods are defined even if read method is customised. (closes #3677) [jonathan@bluewire.net.nz]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4002 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index a4505733de..facb22c0e9 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1676,8 +1676,9 @@ module ActiveRecord #:nodoc: # ActiveRecord::Base.generate_read_methods is set to true. def define_read_methods self.class.columns_hash.each do |name, column| - unless self.class.serialized_attributes[name] || respond_to_without_attributes?(name) - define_read_method(name.to_sym, name, column) + unless self.class.serialized_attributes[name] + define_read_method(name.to_sym, name, column) unless respond_to_without_attributes?(name) + define_question_method(name) unless respond_to_without_attributes?("#{name}?") end end end @@ -1686,19 +1687,30 @@ module ActiveRecord #:nodoc: def define_read_method(symbol, attr_name, column) cast_code = column.type_cast_code('v') if column access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']" - + unless attr_name.to_s == self.class.primary_key.to_s access_code = access_code.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ") self.class.read_methods << attr_name + end + + evaluate_read_method attr_name, "def #{symbol}; #{access_code}; end" + end + + # Define an attribute ? method. + def define_question_method(attr_name) + unless attr_name.to_s == self.class.primary_key.to_s self.class.read_methods << "#{attr_name}?" end - + + evaluate_read_method attr_name, "def #{attr_name}?; query_attribute('#{attr_name}'); end" + end + + # Evaluate the definition for an attribute reader or ? method + def evaluate_read_method(attr_name, method_definition) begin - self.class.class_eval("def #{symbol}; #{access_code}; end") - self.class.class_eval("def #{symbol}?; query_attribute('#{attr_name}'); end") + self.class.class_eval(method_definition) rescue SyntaxError => err self.class.read_methods.delete(attr_name) - self.class.read_methods.delete("#{attr_name}?") if logger logger.warn "Exception occured during reader method compilation." logger.warn "Maybe #{attr_name} is not a valid Ruby identifier?" |