mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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 in043ce35b18
. The whole class is still `#:nodoc:` so I don't think this will have any effect for now. [class_method]:cc834db1d0/activerecord/lib/active_record/attribute_methods.rb (L154-L160)
[instance_method]:cc834db1d0/activerecord/lib/active_record/attribute_methods.rb (L299-L301)
This commit is contained in:
parent
1b2efe5a11
commit
e7a28c3990
2 changed files with 58 additions and 0 deletions
|
@ -26,6 +26,21 @@ module ActiveModel
|
|||
define_attribute_method(name)
|
||||
end
|
||||
|
||||
# Returns an array of attribute names as strings
|
||||
#
|
||||
# class Person
|
||||
# include ActiveModel::Attributes
|
||||
#
|
||||
# attribute :name, :string
|
||||
# attribute :age, :integer
|
||||
# end
|
||||
#
|
||||
# Person.attribute_names
|
||||
# # => ["name", "age"]
|
||||
def attribute_names
|
||||
attribute_types.keys
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def define_method_attribute=(name)
|
||||
|
@ -65,10 +80,39 @@ module ActiveModel
|
|||
super
|
||||
end
|
||||
|
||||
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
|
||||
#
|
||||
# class Person
|
||||
# include ActiveModel::Model
|
||||
# include ActiveModel::Attributes
|
||||
#
|
||||
# attribute :name, :string
|
||||
# attribute :age, :integer
|
||||
# end
|
||||
#
|
||||
# person = Person.new(name: 'Francesco', age: 22)
|
||||
# person.attributes
|
||||
# # => {"name"=>"Francesco", "age"=>22}
|
||||
def attributes
|
||||
@attributes.to_hash
|
||||
end
|
||||
|
||||
# Returns an array of attribute names as strings
|
||||
#
|
||||
# class Person
|
||||
# include ActiveModel::Attributes
|
||||
#
|
||||
# attribute :name, :string
|
||||
# attribute :age, :integer
|
||||
# end
|
||||
#
|
||||
# person = Person.new
|
||||
# person.attribute_names
|
||||
# # => ["name", "age"]
|
||||
def attribute_names
|
||||
@attributes.keys
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def write_attribute(attr_name, value)
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue