Revert "Add test to make sure this method will not be removed again"
This reverts commit d93a5d385e5bc2392a1f47dc2885e353898b62e1. Revert "Revert "Remove unused internal methods in ActiveModel::Attributes"" This reverts commit 2d7967204e7f7d5ba846b0a6ed51088c7a7db365. Reason: read_attribute was added in 6.1 as a performance optimization and it is not needed anymore and write_attribute only existed to make possible to call something that is not `attribute=` with send. We don't need those methods internally and since they were never part of the public API we can remove them.
This commit is contained in:
parent
43daedcb7d
commit
e14e78bf44
@ -120,27 +120,11 @@ def freeze
|
||||
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
|
||||
|
||||
@attributes.write_from_user(name, value)
|
||||
end
|
||||
|
||||
def _write_attribute(attr_name, value)
|
||||
@attributes.write_from_user(attr_name, value)
|
||||
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
|
||||
|
||||
@attributes.fetch_value(name)
|
||||
end
|
||||
|
||||
def attribute(attr_name)
|
||||
@attributes.fetch_value(attr_name)
|
||||
end
|
||||
|
@ -14,19 +14,6 @@ class ModelForAttributesTest
|
||||
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
|
||||
@ -142,19 +129,5 @@ class GrandchildModelForAttributesTest < ChildModelForAttributesTest
|
||||
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 New Issue
Block a user