aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-22 16:25:40 -0600
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-26 06:45:57 -0300
commit14b1208dd313f643f72aa2d92874198342e9fe14 (patch)
tree82af9d0a1dbcaafb4061c50ffa6d1a595d339e95 /activerecord/lib/active_record
parent2571c3f415ac0c6eacedbc199e31b1c77e7bacc4 (diff)
downloadrails-14b1208dd313f643f72aa2d92874198342e9fe14.tar.gz
rails-14b1208dd313f643f72aa2d92874198342e9fe14.tar.bz2
rails-14b1208dd313f643f72aa2d92874198342e9fe14.zip
Encapsulate the creation of `Attribute` objects
This will make it less painful to add additional properties, which should persist across writes, such as `name`. Conflicts: activerecord/lib/active_record/attribute_set.rb
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/attribute.rb28
-rw-r--r--activerecord/lib/active_record/attribute_methods/write.rb5
-rw-r--r--activerecord/lib/active_record/attribute_set.rb10
-rw-r--r--activerecord/lib/active_record/attribute_set/builder.rb2
-rw-r--r--activerecord/lib/active_record/core.rb4
5 files changed, 31 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb
index 6c0b81b3fe..13c8bc3676 100644
--- a/activerecord/lib/active_record/attribute.rb
+++ b/activerecord/lib/active_record/attribute.rb
@@ -41,6 +41,14 @@ module ActiveRecord
type.changed_in_place?(old_value, value)
end
+ def with_value_from_user(value)
+ self.class.from_user(value, type)
+ end
+
+ def with_value_from_database(value)
+ self.class.from_database(value, type)
+ end
+
def type_cast
raise NotImplementedError
end
@@ -69,18 +77,13 @@ module ActiveRecord
end
end
- class Null # :nodoc:
- class << self
- attr_reader :value, :value_before_type_cast, :value_for_database
-
- def changed_from?(*)
- false
- end
- alias changed_in_place_from? changed_from?
+ class NullAttribute < Attribute # :nodoc:
+ def initialize
+ super(nil, Type::Value.new)
+ end
- def initialized?
- true
- end
+ def value
+ nil
end
end
@@ -98,5 +101,8 @@ module ActiveRecord
false
end
end
+ private_constant :FromDatabase, :FromUser, :NullAttribute, :Uninitialized
+
+ Null = NullAttribute.new # :nodoc:
end
end
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb
index 246a2cd8ba..94fce2db60 100644
--- a/activerecord/lib/active_record/attribute_methods/write.rb
+++ b/activerecord/lib/active_record/attribute_methods/write.rb
@@ -69,16 +69,15 @@ module ActiveRecord
def write_attribute_with_type_cast(attr_name, value, should_type_cast)
attr_name = attr_name.to_s
attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key
- type = type_for_attribute(attr_name)
unless has_attribute?(attr_name) || self.class.columns_hash.key?(attr_name)
raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{attr_name}'"
end
if should_type_cast
- @attributes[attr_name] = Attribute.from_user(value, type)
+ @attributes.write_from_user(attr_name, value)
else
- @attributes[attr_name] = Attribute.from_database(value, type)
+ @attributes.write_from_database(attr_name, value)
end
value
diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb
index 68382756a4..65e15b16dd 100644
--- a/activerecord/lib/active_record/attribute_set.rb
+++ b/activerecord/lib/active_record/attribute_set.rb
@@ -2,7 +2,7 @@ require 'active_record/attribute_set/builder'
module ActiveRecord
class AttributeSet # :nodoc:
- delegate :[], :[]=, to: :attributes
+ delegate :[], to: :attributes
delegate :keys, to: :initialized_attributes
def initialize(attributes)
@@ -31,6 +31,14 @@ module ActiveRecord
end
end
+ def write_from_database(name, value)
+ attributes[name] = self[name].with_value_from_database(value)
+ end
+
+ def write_from_user(name, value)
+ attributes[name] = self[name].with_value_from_user(value)
+ end
+
def freeze
@attributes.freeze
super
diff --git a/activerecord/lib/active_record/attribute_set/builder.rb b/activerecord/lib/active_record/attribute_set/builder.rb
index d91720d5e1..f9cf9c1809 100644
--- a/activerecord/lib/active_record/attribute_set/builder.rb
+++ b/activerecord/lib/active_record/attribute_set/builder.rb
@@ -7,7 +7,7 @@ module ActiveRecord
@types = types
end
- def build_from_database(values, additional_types = {})
+ def build_from_database(values = {}, additional_types = {})
attributes = build_attributes_from_values(values, additional_types)
add_uninitialized_attributes(attributes)
AttributeSet.new(attributes)
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 10b9e27b7d..c434b0d40a 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -322,7 +322,7 @@ module ActiveRecord
def initialize_dup(other) # :nodoc:
pk = self.class.primary_key
@attributes = @attributes.dup
- @attributes[pk] = Attribute.from_database(nil, type_for_attribute(pk))
+ @attributes.write_from_database(pk, nil)
run_callbacks(:initialize) unless _initialize_callbacks.empty?
@@ -522,7 +522,7 @@ module ActiveRecord
def init_internals
pk = self.class.primary_key
unless @attributes.include?(pk)
- @attributes[pk] = Attribute.from_database(nil, type_for_attribute(pk))
+ @attributes.write_from_database(pk, nil)
end
@aggregation_cache = {}