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:
parent
0e9a705966
commit
3ea9a88d80
3 changed files with 13 additions and 2 deletions
|
@ -59,7 +59,7 @@ module ActiveRecord
|
||||||
# task.attributes_before_type_cast
|
# task.attributes_before_type_cast
|
||||||
# # => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>"2012-10-21", "created_at"=>nil, "updated_at"=>nil}
|
# # => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>"2012-10-21", "created_at"=>nil, "updated_at"=>nil}
|
||||||
def attributes_before_type_cast
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
class AttributeSet # :nodoc:
|
class AttributeSet # :nodoc:
|
||||||
delegate :[], :[]=, :fetch, :include?, :keys, :each_with_object, to: :attributes
|
delegate :[], :[]=, :fetch, :include?, :keys, to: :attributes
|
||||||
|
|
||||||
def initialize(attributes)
|
def initialize(attributes)
|
||||||
@attributes = attributes
|
@attributes = attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def values_before_type_cast
|
||||||
|
attributes.each_with_object({}) { |(k, v), h| h[k] = v.value_before_type_cast }
|
||||||
|
end
|
||||||
|
|
||||||
def update(other)
|
def update(other)
|
||||||
attributes.update(other.attributes)
|
attributes.update(other.attributes)
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,5 +45,12 @@ module ActiveRecord
|
||||||
assert clone.frozen?
|
assert clone.frozen?
|
||||||
assert_not attributes.frozen?
|
assert_not attributes.frozen?
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue