mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add test to make sure this method will not be removed again
This commit is contained in:
parent
2d7967204e
commit
d93a5d385e
2 changed files with 29 additions and 0 deletions
|
@ -120,6 +120,7 @@ module ActiveModel
|
|||
end
|
||||
|
||||
private
|
||||
# Writes a value to an attribute by name following aliases if necessary.
|
||||
def write_attribute(attr_name, value)
|
||||
name = attr_name.to_s
|
||||
name = self.class.attribute_aliases[name] || name
|
||||
|
@ -132,6 +133,7 @@ module ActiveModel
|
|||
end
|
||||
alias :attribute= :_write_attribute
|
||||
|
||||
# Reads a value from an attribute by name following aliases if necessary.
|
||||
def read_attribute(attr_name)
|
||||
name = attr_name.to_s
|
||||
name = self.class.attribute_aliases[name] || name
|
||||
|
|
|
@ -14,6 +14,19 @@ module ActiveModel
|
|||
attribute :string_with_default, :string, default: "default string"
|
||||
attribute :date_field, :date, default: -> { Date.new(2016, 1, 1) }
|
||||
attribute :boolean_field, :boolean
|
||||
|
||||
alias_attribute :integer, :integer_field
|
||||
alias_attribute :string, :string_field
|
||||
|
||||
def method_that_writes_to_an_attribute
|
||||
value = read_attribute(:integer_field)
|
||||
write_attribute(:string_field, value.to_s)
|
||||
end
|
||||
|
||||
def method_that_writes_to_an_attribute_using_aliases
|
||||
value = read_attribute(:integer)
|
||||
write_attribute(:string, value.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
class ChildModelForAttributesTest < ModelForAttributesTest
|
||||
|
@ -129,5 +142,19 @@ module ActiveModel
|
|||
assert data.frozen?
|
||||
assert_raise(FrozenError) { data.integer_field = 1 }
|
||||
end
|
||||
|
||||
test "attributes can be read and written using the public API" do
|
||||
data = ModelForAttributesTest.new(integer_field: 20)
|
||||
data.method_that_writes_to_an_attribute
|
||||
|
||||
assert_equal "20", data.string_field
|
||||
end
|
||||
|
||||
test "attributes can be read and written using the public API even with aliases" do
|
||||
data = ModelForAttributesTest.new(integer: 20)
|
||||
data.method_that_writes_to_an_attribute
|
||||
|
||||
assert_equal "20", data.string
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue