mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
allow define_attribute_methods to pass multiple values
This commit is contained in:
parent
017632fe2b
commit
05234b358f
2 changed files with 20 additions and 13 deletions
|
@ -28,7 +28,7 @@ module ActiveModel
|
|||
# attribute_method_affix :prefix => 'reset_', :suffix => '_to_default!'
|
||||
# attribute_method_suffix '_contrived?'
|
||||
# attribute_method_prefix 'clear_'
|
||||
# define_attribute_methods ['name']
|
||||
# define_attribute_methods 'name'
|
||||
#
|
||||
# attr_accessor :name
|
||||
#
|
||||
|
@ -86,7 +86,7 @@ module ActiveModel
|
|||
# include ActiveModel::AttributeMethods
|
||||
# attr_accessor :name
|
||||
# attribute_method_prefix 'clear_'
|
||||
# define_attribute_methods [:name]
|
||||
# define_attribute_methods :name
|
||||
#
|
||||
# private
|
||||
#
|
||||
|
@ -124,7 +124,7 @@ module ActiveModel
|
|||
# include ActiveModel::AttributeMethods
|
||||
# attr_accessor :name
|
||||
# attribute_method_suffix '_short?'
|
||||
# define_attribute_methods [:name]
|
||||
# define_attribute_methods :name
|
||||
#
|
||||
# private
|
||||
#
|
||||
|
@ -162,7 +162,7 @@ module ActiveModel
|
|||
# include ActiveModel::AttributeMethods
|
||||
# attr_accessor :name
|
||||
# attribute_method_affix :prefix => 'reset_', :suffix => '_to_default!'
|
||||
# define_attribute_methods [:name]
|
||||
# define_attribute_methods :name
|
||||
#
|
||||
# private
|
||||
#
|
||||
|
@ -216,7 +216,7 @@ module ActiveModel
|
|||
# # Call to define_attribute_methods must appear after the
|
||||
# # attribute_method_prefix, attribute_method_suffix or
|
||||
# # attribute_method_affix declares.
|
||||
# define_attribute_methods [:name, :age, :address]
|
||||
# define_attribute_methods :name, :age, :address
|
||||
#
|
||||
# private
|
||||
#
|
||||
|
@ -224,8 +224,8 @@ module ActiveModel
|
|||
# ...
|
||||
# end
|
||||
# end
|
||||
def define_attribute_methods(attr_names)
|
||||
attr_names.each { |attr_name| define_attribute_method(attr_name) }
|
||||
def define_attribute_methods(*attr_names)
|
||||
attr_names.flatten.each { |attr_name| define_attribute_method(attr_name) }
|
||||
end
|
||||
|
||||
def define_attribute_method(attr_name)
|
||||
|
|
|
@ -10,7 +10,7 @@ class ModelWithAttributes
|
|||
end
|
||||
|
||||
def attributes
|
||||
{ :foo => 'value of foo' }
|
||||
{ :foo => 'value of foo', :baz => 'value of baz' }
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -127,29 +127,36 @@ class AttributeMethodsTest < ActiveModel::TestCase
|
|||
assert_equal "value of a?b", ModelWithWeirdNamesAttributes.new.send('a?b')
|
||||
end
|
||||
|
||||
test '#define_attribute_methods works passing multiple arguments' do
|
||||
ModelWithAttributes.define_attribute_methods(:foo, :baz)
|
||||
|
||||
assert_equal "value of foo", ModelWithAttributes.new.foo
|
||||
assert_equal "value of baz", ModelWithAttributes.new.baz
|
||||
end
|
||||
|
||||
test '#define_attribute_methods generates attribute methods' do
|
||||
ModelWithAttributes.define_attribute_methods([:foo])
|
||||
ModelWithAttributes.define_attribute_methods(:foo)
|
||||
|
||||
assert_respond_to ModelWithAttributes.new, :foo
|
||||
assert_equal "value of foo", ModelWithAttributes.new.foo
|
||||
end
|
||||
|
||||
test '#define_attribute_methods generates attribute methods with spaces in their names' do
|
||||
ModelWithAttributesWithSpaces.define_attribute_methods([:'foo bar'])
|
||||
ModelWithAttributesWithSpaces.define_attribute_methods(:'foo bar')
|
||||
|
||||
assert_respond_to ModelWithAttributesWithSpaces.new, :'foo bar'
|
||||
assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.send(:'foo bar')
|
||||
end
|
||||
|
||||
test '#alias_attribute works with attributes with spaces in their names' do
|
||||
ModelWithAttributesWithSpaces.define_attribute_methods([:'foo bar'])
|
||||
ModelWithAttributesWithSpaces.define_attribute_methods(:'foo bar')
|
||||
ModelWithAttributesWithSpaces.alias_attribute(:'foo_bar', :'foo bar')
|
||||
|
||||
assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.foo_bar
|
||||
end
|
||||
|
||||
test '#undefine_attribute_methods removes attribute methods' do
|
||||
ModelWithAttributes.define_attribute_methods([:foo])
|
||||
ModelWithAttributes.define_attribute_methods(:foo)
|
||||
ModelWithAttributes.undefine_attribute_methods
|
||||
|
||||
assert !ModelWithAttributes.new.respond_to?(:foo)
|
||||
|
@ -170,7 +177,7 @@ class AttributeMethodsTest < ActiveModel::TestCase
|
|||
assert_deprecated { klass.attribute_method_suffix '' }
|
||||
assert_deprecated { klass.attribute_method_prefix '' }
|
||||
|
||||
klass.define_attribute_methods([:foo])
|
||||
klass.define_attribute_methods(:foo)
|
||||
|
||||
assert_equal 'value of foo', klass.new.foo
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue