Permalink
Browse files

Merge pull request #15849 from sgrif/sg-move-attributes

Move `attributes` to the `AttributeSet` object.
  • Loading branch information...
2 parents 1e122ff + 3e422e2 commit ebf552bd2c0ac97c0b2a9943819368a352ce636c @senny senny committed Jun 22, 2014
View
4 activerecord/lib/active_record/attribute_methods.rb
@@ -271,9 +271,7 @@ def attribute_names
# 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
5 activerecord/lib/active_record/attribute_set.rb
@@ -10,6 +10,11 @@ def update(other)
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
8 activerecord/test/cases/attribute_set_test.rb
@@ -45,5 +45,13 @@ class AttributeSetTest < ActiveRecord::TestCase
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

0 comments on commit ebf552b

Please sign in to comment.