aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-21 10:52:19 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-21 10:52:19 -0600
commit3ea9a88d803d7fd1bb0de649f7237fca0200efe3 (patch)
tree627f55bf4c86c087e5695acc0f405160ffb35edb
parent0e9a7059664eb9dee8e3afae60d26ff858d0d84b (diff)
downloadrails-3ea9a88d803d7fd1bb0de649f7237fca0200efe3.tar.gz
rails-3ea9a88d803d7fd1bb0de649f7237fca0200efe3.tar.bz2
rails-3ea9a88d803d7fd1bb0de649f7237fca0200efe3.zip
Move `attributes_before_type_cast` to `AttributeSet`
-rw-r--r--activerecord/lib/active_record/attribute_methods/before_type_cast.rb2
-rw-r--r--activerecord/lib/active_record/attribute_set.rb6
-rw-r--r--activerecord/test/cases/attribute_set_test.rb7
3 files changed, 13 insertions, 2 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 d057f0941a..8ca9379bb6 100644
--- a/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
+++ b/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
@@ -59,7 +59,7 @@ module ActiveRecord
# task.attributes_before_type_cast
# # => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>"2012-10-21", "created_at"=>nil, "updated_at"=>nil}
def attributes_before_type_cast
- @attributes.each_with_object({}) { |(k, v), h| h[k] = v.value_before_type_cast }
+ @attributes.values_before_type_cast
end
private
diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb
index 102ef17e16..f545fa8a9a 100644
--- a/activerecord/lib/active_record/attribute_set.rb
+++ b/activerecord/lib/active_record/attribute_set.rb
@@ -1,11 +1,15 @@
module ActiveRecord
class AttributeSet # :nodoc:
- delegate :[], :[]=, :fetch, :include?, :keys, :each_with_object, to: :attributes
+ delegate :[], :[]=, :fetch, :include?, :keys, to: :attributes
def initialize(attributes)
@attributes = attributes
end
+ def values_before_type_cast
+ attributes.each_with_object({}) { |(k, v), h| h[k] = v.value_before_type_cast }
+ end
+
def update(other)
attributes.update(other.attributes)
end
diff --git a/activerecord/test/cases/attribute_set_test.rb b/activerecord/test/cases/attribute_set_test.rb
index 091f7e396a..dd4143af2d 100644
--- a/activerecord/test/cases/attribute_set_test.rb
+++ b/activerecord/test/cases/attribute_set_test.rb
@@ -45,5 +45,12 @@ module ActiveRecord
assert clone.frozen?
assert_not attributes.frozen?
end
+
+ test "values_before_type_cast" do
+ builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Integer.new)
+ attributes = builder.build_from_database(foo: '1.1', bar: '2.2')
+
+ assert_equal({ foo: '1.1', bar: '2.2' }, attributes.values_before_type_cast)
+ end
end
end