提交 e1ad3ed7 编写于 作者: R Rafael Mendonça França

Merge pull request #15846 from sgrif/sg-attributes-before-type-cast

Move `attributes_before_type_cast` to `AttributeSet`

Conflicts:
	activerecord/lib/active_record/attribute_set.rb
	activerecord/test/cases/attribute_set_test.rb
......@@ -57,7 +57,7 @@ def read_attribute_before_type_cast(attr_name)
# 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
......
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 to_hash
attributes.each_with_object({}) { |(k, v), h| h[k] = v.value }
end
......
......@@ -61,5 +61,12 @@ class AttributeSetTest < ActiveRecord::TestCase
assert_equal({ foo: 1, bar: 2.2 }, attributes.to_hash)
assert_equal({ foo: 1, bar: 2.2 }, attributes.to_h)
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册