Skip to content

Conversation

@seanpdoyle
Copy link
Contributor

Replace all access of @attributes and attributes key-value pairs with calls to read_attribute and write_attribute.

The read_attribute and write_attribute implementations draw inspiration from ActiveRecord::AttributeMethods::Read and ActiveRecord::AttributeMethods::Write, respectively.

rails/rails#53886 proposes implementing each method at the Active Model layer. While that proposal is considered, this commit implements each method in terms of accessing the underlying @attributes hash instance. This change is also in support of a first-party integration with ActiveModel::Attributes proposed in #410, and aims to be compatible with its attribute reading and writing interfaces.

person = Person.find(1)
person.read_attribute("name")           # => "Matz"
person.name                             # => "Matz"

person.write_attribute("name", "matz")
person.name                             # => "matz"

Replace all access of `@attributes` and `attributes` key-value pairs
with calls to `read_attribute` and `write_attribute`.

The `read_attribute` and `write_attribute` implementations draw
inspiration from [ActiveRecord::AttributeMethods::Read][] and
[ActiveRecord::AttributeMethods::Write][], respectively.

[rails/rails#53886][] proposes implementing each method at the Active
Model layer. While that proposal is considered, this commit implements
each method in terms of accessing the underlying `@attributes` hash
instance. This change is also in support of a first-party integration
with [ActiveModel::Attributes][] proposed in [rails#410][], and aims to be
compatible with its attribute reading and writing interfaces.

```ruby
person = Person.find(1)
person.read_attribute("name")           # => "Matz"
person.name                             # => "Matz"

person.write_attribute("name", "matz")
person.name                             # => "matz"
```

[ActiveRecord::AttributeMethods::Read]: https://edgeapi.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Read.html#method-i-read_attribute
[ActiveRecord::AttributeMethods::Write]: https://edgeapi.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Write.html#method-i-write_attribute
[rails/rails#53886]: rails/rails#53886
[ActiveModel::Attributes]: https://edgeapi.rubyonrails.org/classes/ActiveModel/Attributes.html
[rails#410]: rails#410
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant