aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorDaniel Colson <danieljamescolson@gmail.com>2018-02-07 16:14:45 -0500
committerDaniel Colson <danieljamescolson@gmail.com>2018-02-07 18:11:14 -0500
commit043ce35b18662e46cb5b0d9cf203f4da4eefc421 (patch)
tree8ae98f8e58c717b6a676ee88338d3dbba59ee444 /activemodel
parent5ae2ecab6d3365f6f17e3c8cb298dfeeea113774 (diff)
downloadrails-043ce35b18662e46cb5b0d9cf203f4da4eefc421.tar.gz
rails-043ce35b18662e46cb5b0d9cf203f4da4eefc421.tar.bz2
rails-043ce35b18662e46cb5b0d9cf203f4da4eefc421.zip
Add ActiveModel::Attributes#attributes
This starts to fix #31832. ActiveModel::Attributes includes ActiveModel::AttributeMethods, which requires an `#attributes` method that returns a hash with string keys.
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/attributes.rb4
-rw-r--r--activemodel/test/cases/attributes_test.rb20
2 files changed, 24 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/attributes.rb b/activemodel/lib/active_model/attributes.rb
index 046ae67ad7..4083bf827b 100644
--- a/activemodel/lib/active_model/attributes.rb
+++ b/activemodel/lib/active_model/attributes.rb
@@ -66,6 +66,10 @@ module ActiveModel
super
end
+ def attributes
+ @attributes.to_hash
+ end
+
private
def write_attribute(attr_name, value)
diff --git a/activemodel/test/cases/attributes_test.rb b/activemodel/test/cases/attributes_test.rb
index 7c1d813ce0..5483fb101d 100644
--- a/activemodel/test/cases/attributes_test.rb
+++ b/activemodel/test/cases/attributes_test.rb
@@ -47,6 +47,26 @@ module ActiveModel
assert_equal true, data.boolean_field
end
+ test "reading attributes" do
+ data = ModelForAttributesTest.new(
+ integer_field: 1.1,
+ string_field: 1.1,
+ decimal_field: 1.1,
+ boolean_field: 1.1
+ )
+
+ expected_attributes = {
+ integer_field: 1,
+ string_field: "1.1",
+ decimal_field: BigDecimal("1.1"),
+ string_with_default: "default string",
+ date_field: Date.new(2016, 1, 1),
+ boolean_field: true
+ }.stringify_keys
+
+ assert_equal expected_attributes, data.attributes
+ end
+
test "nonexistent attribute" do
assert_raise ActiveModel::UnknownAttributeError do
ModelForAttributesTest.new(nonexistent: "nonexistent")