From e8375a662bc47647f4bd39779ada018cc634fe97 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Wed, 4 Jun 2014 06:26:03 -0600 Subject: Use null column for association key types --- .../lib/active_record/attribute_methods.rb | 40 ++++++++++++---------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'activerecord/lib/active_record/attribute_methods.rb') diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index e56a4cc805..dccbd48592 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -18,6 +18,8 @@ module ActiveRecord include TimeZoneConversion include Dirty include Serialization + + delegate :column_for_attribute, to: :class end AttrNames = Module.new { @@ -192,6 +194,25 @@ module ActiveRecord [] end end + + # Returns the column object for the named attribute. Returns +nil+ if the + # named attribute not exists. + # + # class Person < ActiveRecord::Base + # end + # + # person = Person.new + # person.column_for_attribute(:name) # the result depends on the ConnectionAdapter + # # => # + # + # person.column_for_attribute(:nothing) + # # => #, ...> + def column_for_attribute(name) + name = name.to_s + columns_hash.fetch(name) do + ConnectionAdapters::Column.new(name, nil, Type::Value.new) + end + end end # If we haven't generated any methods yet, generate them, then @@ -339,25 +360,6 @@ module ActiveRecord !value.nil? && !(value.respond_to?(:empty?) && value.empty?) end - # Returns the column object for the named attribute. Returns +nil+ if the - # named attribute not exists. - # - # class Person < ActiveRecord::Base - # end - # - # person = Person.new - # person.column_for_attribute(:name) # the result depends on the ConnectionAdapter - # # => # - # - # person.column_for_attribute(:nothing) - # # => #, ...> - def column_for_attribute(name) - name = name.to_s - self.class.columns_hash.fetch(name) do - ConnectionAdapters::Column.new(name, nil, Type::Value.new) - end - end - # Returns the value of the attribute identified by attr_name after it has been typecast (for example, # "2004-12-12" in a date column is cast to a date object, like Date.new(2004, 12, 12)). It raises # ActiveModel::MissingAttributeError if the identified attribute is missing. -- cgit v1.2.3