2009-10-07 10:05:54 -04:00
|
|
|
require 'cases/helper'
|
|
|
|
|
|
|
|
class ModelWithAttributes
|
|
|
|
include ActiveModel::AttributeMethods
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-10-07 10:05:54 -04:00
|
|
|
attribute_method_suffix ''
|
2009-10-07 10:06:54 -04:00
|
|
|
|
2011-03-16 20:08:02 -04:00
|
|
|
class << self
|
|
|
|
define_method(:bar) do
|
2011-03-16 20:19:35 -04:00
|
|
|
'original bar'
|
2011-03-16 20:08:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-07 10:06:54 -04:00
|
|
|
def attributes
|
|
|
|
{ :foo => 'value of foo' }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def attribute(name)
|
|
|
|
attributes[name.to_sym]
|
|
|
|
end
|
2009-10-07 10:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class ModelWithAttributes2
|
|
|
|
include ActiveModel::AttributeMethods
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-10-07 10:05:54 -04:00
|
|
|
attribute_method_suffix '_test'
|
|
|
|
end
|
|
|
|
|
2011-02-03 13:46:03 -05:00
|
|
|
class ModelWithAttributesWithSpaces
|
|
|
|
include ActiveModel::AttributeMethods
|
|
|
|
|
|
|
|
attribute_method_suffix ''
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
{ :'foo bar' => 'value of foo bar'}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def attribute(name)
|
|
|
|
attributes[name.to_sym]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-16 18:17:47 -04:00
|
|
|
class ModelWithWeirdNamesAttributes
|
|
|
|
include ActiveModel::AttributeMethods
|
|
|
|
|
|
|
|
attribute_method_suffix ''
|
|
|
|
|
2011-03-16 18:31:57 -04:00
|
|
|
class << self
|
|
|
|
define_method(:'c?d') do
|
2011-03-16 20:19:35 -04:00
|
|
|
'original c?d'
|
2011-03-16 18:31:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-16 18:17:47 -04:00
|
|
|
def attributes
|
|
|
|
{ :'a?b' => 'value of a?b' }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def attribute(name)
|
|
|
|
attributes[name.to_sym]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-07 10:05:54 -04:00
|
|
|
class AttributeMethodsTest < ActiveModel::TestCase
|
|
|
|
test 'unrelated classes should not share attribute method matchers' do
|
|
|
|
assert_not_equal ModelWithAttributes.send(:attribute_method_matchers),
|
|
|
|
ModelWithAttributes2.send(:attribute_method_matchers)
|
|
|
|
end
|
2009-10-07 10:06:54 -04:00
|
|
|
|
2011-02-13 12:27:33 -05:00
|
|
|
test '#define_attribute_method generates attribute method' do
|
|
|
|
ModelWithAttributes.define_attribute_method(:foo)
|
|
|
|
|
|
|
|
assert_respond_to ModelWithAttributes.new, :foo
|
|
|
|
assert_equal "value of foo", ModelWithAttributes.new.foo
|
|
|
|
end
|
|
|
|
|
2011-03-16 18:17:47 -04:00
|
|
|
test '#define_attribute_method generates attribute method with invalid identifier characters' do
|
|
|
|
ModelWithWeirdNamesAttributes.define_attribute_method(:'a?b')
|
|
|
|
ModelWithWeirdNamesAttributes.define_attribute_method(:'a?b')
|
|
|
|
|
|
|
|
assert_respond_to ModelWithWeirdNamesAttributes.new, :'a?b'
|
|
|
|
assert_equal "value of a?b", ModelWithWeirdNamesAttributes.new.send('a?b')
|
|
|
|
end
|
|
|
|
|
2009-10-07 10:06:54 -04:00
|
|
|
test '#define_attribute_methods generates attribute methods' do
|
|
|
|
ModelWithAttributes.define_attribute_methods([:foo])
|
|
|
|
|
2010-05-18 21:47:24 -04:00
|
|
|
assert_respond_to ModelWithAttributes.new, :foo
|
2009-10-07 10:06:54 -04:00
|
|
|
assert_equal "value of foo", ModelWithAttributes.new.foo
|
|
|
|
end
|
|
|
|
|
2011-02-03 13:46:03 -05:00
|
|
|
test '#define_attribute_methods generates attribute methods with spaces in their names' do
|
|
|
|
ModelWithAttributesWithSpaces.define_attribute_methods([:'foo bar'])
|
2011-03-16 18:17:47 -04:00
|
|
|
|
2011-02-03 13:46:03 -05:00
|
|
|
assert_respond_to ModelWithAttributesWithSpaces.new, :'foo bar'
|
|
|
|
assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.send(:'foo bar')
|
|
|
|
end
|
2011-03-16 18:17:47 -04:00
|
|
|
|
2011-03-16 20:08:02 -04:00
|
|
|
test '#define_attr_method generates attribute method' do
|
|
|
|
ModelWithAttributes.define_attr_method(:bar, 'bar')
|
|
|
|
|
|
|
|
assert_respond_to ModelWithAttributes, :bar
|
2011-03-16 20:19:35 -04:00
|
|
|
assert_equal "original bar", ModelWithAttributes.original_bar
|
2011-03-16 20:08:02 -04:00
|
|
|
assert_equal "bar", ModelWithAttributes.bar
|
2011-03-17 22:17:20 -04:00
|
|
|
ModelWithAttributes.define_attr_method(:bar)
|
|
|
|
assert !ModelWithAttributes.bar
|
2011-03-16 20:08:02 -04:00
|
|
|
end
|
|
|
|
|
2011-03-16 18:31:57 -04:00
|
|
|
test '#define_attr_method generates attribute method with invalid identifier characters' do
|
|
|
|
ModelWithWeirdNamesAttributes.define_attr_method(:'c?d', 'c?d')
|
|
|
|
|
|
|
|
assert_respond_to ModelWithWeirdNamesAttributes, :'c?d'
|
2011-03-16 20:19:35 -04:00
|
|
|
assert_equal "original c?d", ModelWithWeirdNamesAttributes.send('original_c?d')
|
2011-03-16 18:31:57 -04:00
|
|
|
assert_equal "c?d", ModelWithWeirdNamesAttributes.send('c?d')
|
|
|
|
end
|
|
|
|
|
2011-02-03 13:46:03 -05:00
|
|
|
test '#alias_attribute works with attributes with spaces in their names' do
|
|
|
|
ModelWithAttributesWithSpaces.define_attribute_methods([:'foo bar'])
|
|
|
|
ModelWithAttributesWithSpaces.alias_attribute(:'foo_bar', :'foo bar')
|
2011-03-16 18:17:47 -04:00
|
|
|
|
2011-02-03 13:46:03 -05:00
|
|
|
assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.foo_bar
|
|
|
|
end
|
|
|
|
|
2009-10-07 10:06:54 -04:00
|
|
|
test '#undefine_attribute_methods removes attribute methods' do
|
|
|
|
ModelWithAttributes.define_attribute_methods([:foo])
|
|
|
|
ModelWithAttributes.undefine_attribute_methods
|
|
|
|
|
|
|
|
assert !ModelWithAttributes.new.respond_to?(:foo)
|
|
|
|
assert_raises(NoMethodError) { ModelWithAttributes.new.foo }
|
|
|
|
end
|
2009-10-07 10:05:54 -04:00
|
|
|
end
|