1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move attributes_before_type_cast to AttributeSet

This commit is contained in:
Sean Griffin 2014-06-21 10:52:19 -06:00
parent 0e9a705966
commit 3ea9a88d80
3 changed files with 13 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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