Changeset 2850
- Timestamp:
- 11/02/05 17:30:59 (3 years ago)
- Files:
-
- trunk/activerecord/lib/active_record/base.rb (modified) (4 diffs)
- trunk/activerecord/test/base_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/base.rb
r2837 r2850 1 1 require 'yaml' 2 require 'set' 2 3 require 'active_record/deprecated_finders' 3 4 … … 762 763 end 763 764 764 765 765 # Contains the names of the generated reader methods. 766 766 def read_methods 767 @read_methods ||= {}768 end 769 767 @read_methods ||= Set.new 768 end 769 770 770 # Resets all the cached information about columns, which will cause them to be reloaded on the next request. 771 771 def reset_column_information 772 read_methods.each _key {|name| undef_method(name)}772 read_methods.each { |name| undef_method(name) } 773 773 @column_names = @columns = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = nil 774 774 end … … 1135 1135 end 1136 1136 1137 # Every Active Record class must use "id" as their primary ID. This getter overwrites the native1138 # id method, which isn't being used in this context.1137 # A model instance's primary key is always available as model.id 1138 # whether you name it the default 'id' or set it to something else. 1139 1139 def id 1140 1140 attr_name = self.class.primary_key 1141 column = column_for_attribute(attr_name) 1142 raise ActiveRecordError, "No such primary key column #{attr_name} for table #{self.class.table_name}" if column.nil? 1141 column = column_for_attribute(attr_name) 1143 1142 define_read_method(:id, attr_name, column) if self.class.generate_read_methods 1144 (value = @attributes[attr_name]) && column.type_cast(value)1143 read_attribute(attr_name) 1145 1144 end 1146 1145 … … 1474 1473 end 1475 1474 1476 # Define a column type specific reader method.1475 # Define an attribute reader method. Cope with nil column. 1477 1476 def define_read_method(symbol, attr_name, column) 1478 cast_code = column.type_cast_code('v') 1477 cast_code = column.type_cast_code('v') if column 1479 1478 access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']" 1480 1479 1481 1480 unless attr_name.to_s == self.class.primary_key.to_s 1482 1481 access_code = access_code.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ") 1482 self.class.read_methods << attr_name 1483 1483 end 1484 1484 1485 1485 self.class.class_eval("def #{symbol}; #{access_code}; end") 1486 self.class.read_methods[attr_name] = true unless symbol == :id1487 1486 end 1488 1487 trunk/activerecord/test/base_test.rb
r2817 r2850 1080 1080 def assert_readers(model, exceptions) 1081 1081 expected_readers = model.column_names - (model.serialized_attributes.keys + exceptions + ['id']) 1082 assert_equal expected_readers.sort, model.read_methods. keys.sort1082 assert_equal expected_readers.sort, model.read_methods.to_a.sort 1083 1083 end 1084 1084 end