aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-07-30 12:33:39 -0500
committerJoshua Peek <josh@joshpeek.com>2009-07-30 17:54:01 -0500
commit1841fd54e6b76e4e1af3c8e4ef11ce2df3e3234e (patch)
tree42ed001e3748302006c71e71f06ca9fad33c45e8 /activerecord/lib/active_record/attribute_methods
parent3e58f8e19117346ce459b7531379525d3143608a (diff)
downloadrails-1841fd54e6b76e4e1af3c8e4ef11ce2df3e3234e.tar.gz
rails-1841fd54e6b76e4e1af3c8e4ef11ce2df3e3234e.tar.bz2
rails-1841fd54e6b76e4e1af3c8e4ef11ce2df3e3234e.zip
Move id attribute methods into their related concern
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r--activerecord/lib/active_record/attribute_methods/before_type_cast.rb12
-rw-r--r--activerecord/lib/active_record/attribute_methods/read.rb11
-rw-r--r--activerecord/lib/active_record/attribute_methods/write.rb5
3 files changed, 28 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/before_type_cast.rb b/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
index 65845c4d9a..8815f07df6 100644
--- a/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
+++ b/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
@@ -11,6 +11,18 @@ module ActiveRecord
@attributes[attr_name]
end
+ # Returns a hash of attributes before typecasting and deserialization.
+ def attributes_before_type_cast
+ self.attribute_names.inject({}) do |attrs, name|
+ attrs[name] = read_attribute_before_type_cast(name)
+ attrs
+ end
+ end
+
+ def id_before_type_cast #:nodoc:
+ read_attribute_before_type_cast(self.class.primary_key)
+ end
+
private
# Handle *_before_type_cast for method_missing.
def attribute_before_type_cast(attribute_name)
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb
index 5285fda868..7e92e0bd68 100644
--- a/activerecord/lib/active_record/attribute_methods/read.rb
+++ b/activerecord/lib/active_record/attribute_methods/read.rb
@@ -77,6 +77,17 @@ module ActiveRecord
end
end
+ # A model instance's primary key is always available as model.id
+ # whether you name it the default 'id' or set it to something else.
+ def id
+ attr_name = self.class.primary_key
+ column = column_for_attribute(attr_name)
+
+ self.class.send(:define_read_method, :id, attr_name, column)
+ # now that the method exists, call it
+ self.send attr_name.to_sym
+ end
+
# Returns true if the attribute is of a text column and marked for serialization.
def unserializable_attribute?(attr_name, column)
column.text? && self.class.serialized_attributes[attr_name]
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb
index aab816899c..140057c186 100644
--- a/activerecord/lib/active_record/attribute_methods/write.rb
+++ b/activerecord/lib/active_record/attribute_methods/write.rb
@@ -26,6 +26,11 @@ module ActiveRecord
end
end
+ # Sets the primary ID.
+ def id=(value)
+ write_attribute(self.class.primary_key, value)
+ end
+
private
# Handle *= for method_missing.
def attribute=(attribute_name, value)