public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
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
dhh (author)
Sun Mar 19 22:45:34 -0800 2006
commit  1aff68d61574410fa942aa34ec261c68565e48c1
tree    bc23d1bb58cedaf97a8cf4afff59454b25129735
parent  89c0ab6b82410b826dacb53803a36b935bae36cb
...
1676
1677
1678
1679
1680
 
 
 
1681
1682
1683
1684
1685
1686
1687
...
1686
1687
1688
1689
 
1690
1691
1692
 
 
 
 
 
 
 
 
1693
1694
1695
 
 
 
 
 
 
1696
1697
1698
 
1699
1700
1701
1702
1703
1704
...
1676
1677
1678
 
 
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
...
1687
1688
1689
 
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
 
1704
1705
1706
1707
1708
1709
1710
 
 
1711
1712
1713
 
1714
1715
1716
0
@@ -1676,8 +1676,9 @@
0
       # ActiveRecord::Base.generate_read_methods is set to true.
0
       def define_read_methods
0
         self.class.columns_hash.each do |name, column|
0
- unless self.class.serialized_attributes[name] || respond_to_without_attributes?(name)
0
- define_read_method(name.to_sym, name, column)
0
+ unless self.class.serialized_attributes[name]
0
+ define_read_method(name.to_sym, name, column) unless respond_to_without_attributes?(name)
0
+ define_question_method(name) unless respond_to_without_attributes?("#{name}?")
0
           end
0
         end
0
       end
0
0
0
0
0
@@ -1686,19 +1687,30 @@
0
       def define_read_method(symbol, attr_name, column)
0
         cast_code = column.type_cast_code('v') if column
0
         access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"
0
-
0
+
0
         unless attr_name.to_s == self.class.primary_key.to_s
0
           access_code = access_code.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ")
0
           self.class.read_methods << attr_name
0
+ end
0
+
0
+ evaluate_read_method attr_name, "def #{symbol}; #{access_code}; end"
0
+ end
0
+
0
+ # Define an attribute ? method.
0
+ def define_question_method(attr_name)
0
+ unless attr_name.to_s == self.class.primary_key.to_s
0
           self.class.read_methods << "#{attr_name}?"
0
         end
0
-
0
+
0
+ evaluate_read_method attr_name, "def #{attr_name}?; query_attribute('#{attr_name}'); end"
0
+ end
0
+
0
+ # Evaluate the definition for an attribute reader or ? method
0
+ def evaluate_read_method(attr_name, method_definition)
0
         begin
0
- self.class.class_eval("def #{symbol}; #{access_code}; end")
0
- self.class.class_eval("def #{symbol}?; query_attribute('#{attr_name}'); end")
0
+ self.class.class_eval(method_definition)
0
         rescue SyntaxError => err
0
           self.class.read_methods.delete(attr_name)
0
- self.class.read_methods.delete("#{attr_name}?")
0
           if logger
0
             logger.warn "Exception occured during reader method compilation."
0
             logger.warn "Maybe #{attr_name} is not a valid Ruby identifier?"
...
266
267
268
269
 
270
271
272
273
...
1282
1283
1284
1285
 
1286
 
1287
1288
1289
...
266
267
268
 
269
270
271
272
273
...
1282
1283
1284
 
1285
1286
1287
1288
1289
1290
0
@@ -266,7 +266,7 @@
0
     if ActiveRecord::Base.generate_read_methods
0
       assert_readers(Topic, %w(type replies_count))
0
       assert_readers(Firm, %w(type))
0
- assert_readers(Client, %w(type))
0
+ assert_readers(Client, %w(type ruby_type rating?))
0
     else
0
       [Topic, Firm, Client].each {|klass| assert_equal klass.read_methods, {}}
0
     end
0
0
@@ -1282,8 +1282,9 @@
0
   
0
   private
0
     def assert_readers(model, exceptions)
0
- expected_readers = Set.new(model.column_names - (model.serialized_attributes.keys + exceptions + ['id']))
0
+ expected_readers = Set.new(model.column_names - (model.serialized_attributes.keys + s + ['id']))
0
       expected_readers += expected_readers.map { |col| "#{col}?" }
0
+ expected_readers -= exceptions
0
       assert_equal expected_readers, model.read_methods
0
     end
0
 end
...
57
58
59
 
 
 
 
 
 
 
 
 
60
61
62
...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
0
@@ -57,6 +57,15 @@
0
     end
0
     true
0
   end
0
+
0
+ # Used to test that read and question methods are not generated for these attributes
0
+ def ruby_type
0
+ read_attribute :ruby_type
0
+ end
0
+
0
+ def rating?
0
+ query_attribute :rating
0
+ end
0
 end
0
 
0
 

Comments

    No one has commented yet.