From e7a28c39908768acc1b8c836ab53af546c58c0c0 Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Mon, 22 Apr 2019 19:48:17 -0400 Subject: Add attribute_names to ActiveModel::Attributes This adds `.attribute_names` and `#attribute_names` to `ActiveModel::Attributes` along the same lines as the corresponding methods in `ActiveRecord::AttributeMethods` (see [`.attribute_names`][class_method] and [`#attribute_names`][instance_method]. While I was here I also added documentation for '#attributes', which I added in 043ce35b186. The whole class is still `#:nodoc:` so I don't think this will have any effect for now. [class_method]: https://github.com/rails/rails/blob/cc834db1d0815744cfa173813c05d928e008e167/activerecord/lib/active_record/attribute_methods.rb#L154-L160 [instance_method]: https://github.com/rails/rails/blob/cc834db1d0815744cfa173813c05d928e008e167/activerecord/lib/active_record/attribute_methods.rb#L299-L301 --- activemodel/test/cases/attributes_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/attributes_test.rb b/activemodel/test/cases/attributes_test.rb index 5483fb101d..af0ddcb92f 100644 --- a/activemodel/test/cases/attributes_test.rb +++ b/activemodel/test/cases/attributes_test.rb @@ -67,6 +67,20 @@ module ActiveModel assert_equal expected_attributes, data.attributes end + test "reading attribute names" do + names = [ + "integer_field", + "string_field", + "decimal_field", + "string_with_default", + "date_field", + "boolean_field" + ] + + assert_equal names, ModelForAttributesTest.attribute_names + assert_equal names, ModelForAttributesTest.new.attribute_names + end + test "nonexistent attribute" do assert_raise ActiveModel::UnknownAttributeError do ModelForAttributesTest.new(nonexistent: "nonexistent") -- cgit v1.2.3