Commit 0a72aef
committed
Mix
By including the [ActiveModel::AttributeAssignment][], the `Base` class
can access the [assign_attributes][] method for bulk assignment of
attributes **without** saving them to the server (like through
`Base#update_attributes`).
```ruby
person = Person.new
person.id # => nil
person.name # => nil
person.assign_attributes id: 1, name: "Matz"
person.id # => 1
person.name # => "Matz"
```
Support for versions prior to 7.2.0
---
This commit includes a conditional monkey-patch of the
`_assign_attribute` method when Active Model's version is < 7.2.0:
```ruby
# 7.1.5.2
def _assign_attribute(k, v)
setter = :"#{k}="
if respond_to?(setter)
public_send(setter, v)
else
raise UnknownAttributeError.new(self, k.to_s)
end
end
# 7.2.0
def _assign_attribute(k, v)
setter = :"#{k}="
public_send(setter, v)
rescue NoMethodError
if respond_to?(setter)
raise
else
raise UnknownAttributeError.new(self, k.to_s)
end
end
```
The change is necessary because the [7.1.5.2][] version queries the
instance with `respond_to?`, whereas the [7.2.0][] version skips the
query and sends the method first, falling back to a `NoMethodError`
rescue in case that it doesn't.
[#450][] is an alternative to including the monkey patch. If merged,
it'd make `respond_to?` behavior more like `method_missing`, so the
monkey patch would be unnecessary.
[ActiveModel::AttributeAssignment]: https://edgeapi.rubyonrails.org/classes/ActiveModel/AttributeAssignment.html
[assign_attributes]: https://edgeapi.rubyonrails.org/classes/ActiveModel/AttributeAssignment.html#method-i-assign_attributes
[7.1.5.2]: https://github.com/rails/rails/blob/v7.1.5.2/activemodel/lib/active_model/attribute_assignment.rb
[7.2.0]: https://github.com/rails/rails/blob/v7.2.0/activemodel/lib/active_model/attribute_assignment.rb
[#450]: #450ActiveModel::AttributeAssignment into Base
1 parent b549319 commit 0a72aef
2 files changed
Lines changed: 25 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1589 | 1589 | | |
1590 | 1590 | | |
1591 | 1591 | | |
1592 | | - | |
| 1592 | + | |
1593 | 1593 | | |
1594 | 1594 | | |
1595 | 1595 | | |
| |||
1607 | 1607 | | |
1608 | 1608 | | |
1609 | 1609 | | |
| 1610 | + | |
1610 | 1611 | | |
1611 | 1612 | | |
1612 | 1613 | | |
| |||
1827 | 1828 | | |
1828 | 1829 | | |
1829 | 1830 | | |
| 1831 | + | |
| 1832 | + | |
| 1833 | + | |
| 1834 | + | |
| 1835 | + | |
| 1836 | + | |
| 1837 | + | |
| 1838 | + | |
| 1839 | + | |
| 1840 | + | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
1830 | 1844 | | |
1831 | 1845 | | |
1832 | 1846 | | |
| |||
1835 | 1849 | | |
1836 | 1850 | | |
1837 | 1851 | | |
1838 | | - | |
| 1852 | + | |
1839 | 1853 | | |
1840 | 1854 | | |
1841 | 1855 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1179 | 1179 | | |
1180 | 1180 | | |
1181 | 1181 | | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
1182 | 1191 | | |
1183 | 1192 | | |
1184 | 1193 | | |
| |||
0 commit comments