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

Move attributes to the AttributeSet object.

This commit is contained in:
Sean Griffin 2014-06-21 13:09:48 -06:00
parent 0e9a705966
commit 3e422e201d
3 changed files with 14 additions and 3 deletions

View file

@ -271,9 +271,7 @@ module ActiveRecord
# person.attributes
# # => {"id"=>3, "created_at"=>Sun, 21 Oct 2012 04:53:04, "updated_at"=>Sun, 21 Oct 2012 04:53:04, "name"=>"Francesco", "age"=>22}
def attributes
attribute_names.each_with_object({}) { |name, attrs|
attrs[name] = read_attribute(name)
}
@attributes.to_hash
end
# Returns an <tt>#inspect</tt>-like string for the value of the

View file

@ -10,6 +10,11 @@ module ActiveRecord
attributes.update(other.attributes)
end
def to_hash
attributes.each_with_object({}) { |(k, v), h| h[k] = v.value }
end
alias_method :to_h, :to_hash
def freeze
@attributes.freeze
super

View file

@ -45,5 +45,13 @@ module ActiveRecord
assert clone.frozen?
assert_not attributes.frozen?
end
test "to_hash returns a hash of the type cast values" do
builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new)
attributes = builder.build_from_database(foo: '1.1', bar: '2.2')
assert_equal({ foo: 1, bar: 2.2 }, attributes.to_hash)
assert_equal({ foo: 1, bar: 2.2 }, attributes.to_h)
end
end
end