diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-22 13:55:59 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-22 13:55:59 +0200 |
commit | ebf552bd2c0ac97c0b2a9943819368a352ce636c (patch) | |
tree | a79284295d22166e52122b124eb0ca3d224bd44d | |
parent | 1e122ff71dc65637bfbb0abf6e530af8af140345 (diff) | |
parent | 3e422e201de47c6158d2f60679604e51d2539dfb (diff) | |
download | rails-ebf552bd2c0ac97c0b2a9943819368a352ce636c.tar.gz rails-ebf552bd2c0ac97c0b2a9943819368a352ce636c.tar.bz2 rails-ebf552bd2c0ac97c0b2a9943819368a352ce636c.zip |
Merge pull request #15849 from sgrif/sg-move-attributes
Move `attributes` to the `AttributeSet` object.
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_set.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/attribute_set_test.rb | 8 |
3 files changed, 14 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 268cec6160..267377cec6 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -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 diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb index 102ef17e16..39f4ffe628 100644 --- a/activerecord/lib/active_record/attribute_set.rb +++ b/activerecord/lib/active_record/attribute_set.rb @@ -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 diff --git a/activerecord/test/cases/attribute_set_test.rb b/activerecord/test/cases/attribute_set_test.rb index 091f7e396a..e63d278650 100644 --- a/activerecord/test/cases/attribute_set_test.rb +++ b/activerecord/test/cases/attribute_set_test.rb @@ -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 |