-
Couldn't load subscription status.
- Fork 363
Integrate with Active Model Attributes #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seanpdoyle
wants to merge
2
commits into
rails:main
Choose a base branch
from
seanpdoyle:attributes-without-casting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+407
−52
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
seanpdoyle
commented
Nov 27, 2024
d1d2bca to
d789946
Compare
408844c to
5e5344a
Compare
2e56983 to
da59d07
Compare
b15f777 to
2676f37
Compare
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
The `schema { ... }` interface pre-dates the Active Model Attributes API
(defined as early as [v5.2.0][]), but clearly draws inspiration from
Active Record's Database Schema and Attribute casting (which was
extracted into `ActiveModel::Attributes`).
However, the type information captured in `schema { ... }` blocks or
assigned as `Hash` arguments to `schema=` is purely inert metadata.
Proposal
---
This commit aims to integrate with [ActiveModel::Model][] and
[ActiveModel::Attributes][]. Through the introduction of both modules,
subclasses of `ActiveResource::Schema` can benefit from type casting
attributes and constructing instances with default values.
This commit makes minimally incremental changes, prioritizing backwards
compatibility. The reliance on `#respond_to_missing?` and
`#method_missing` is left largely unchanged. Similarly, the `Schema`
interface continues to provide metadata about its attributes through the
`Schema#attr` method (instead of reading from
`ActiveModel::Attributes#attribute_names` or
`ActiveModel::Attributes.attribute_types`).
API Changes
---
To cast values to their specified types, declare the Schema with the
`:cast_values` set to true.
```ruby
class Person < ActiveResource::Base
schema cast_values: true do
integer 'age'
end
end
p = Person.new
p.age = "18"
p.age # => 18
```
To configure inheriting resources to cast values, set the `cast_values`
class attribute:
```ruby
class ApplicationResource < ActiveResource::Base
self.cast_values = true
end
class Person < ApplicationResource
schema do
integer 'age'
end
end
p = Person.new
p.age = "18"
p.age # => 18
```
To set all resources application-wide to cast values, set
`config.active_resource.cast_values`:
```ruby
# config/application.rb
config.active_resource.cast_values = true
```
[v5.2.0]: https://api.rubyonrails.org/v5.2.0/classes/ActiveModel/Attributes/ClassMethods.html
[ActiveModel::Model]: https://api.rubyonrails.org/classes/ActiveModel/Model.html
[ActiveModel::Attributes]: https://api.rubyonrails.org/classes/ActiveModel/Attributes/ClassMethods.html
2676f37 to
0389ff5
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
schema { ... }interface pre-dates the Active Model Attributes API (defined as early as v5.2.0), but clearly draws inspiration from Active Record's Database Schema and Attribute casting (which was extracted intoActiveModel::Attributes).However, the type information captured in
schema { ... }blocks or assigned asHasharguments toschema=is purely inert metadata.Proposal
This commit aims to integrate with ActiveModel::Model and ActiveModel::Attributes. Through the introduction of both modules, subclasses of
ActiveResource::Basecan benefit from type casting attributes and constructing instances with default values.This commit makes minimally incremental changes, prioritizing backwards compatibility. The reliance on
#respond_to_missing?and#method_missingis left largely unchanged. Similarly, theSchemainterface continues to provide metadata about its attributes through theSchema#attrmethod (instead of reading fromActiveModel::Attributes#attribute_namesorActiveModel::Attributes.attribute_types).API Changes
To cast values to their specified types, declare the Schema with the
:cast_valuesset to true.To configure inheriting resources to cast values, set the
cast_valuesclass attribute:To set all resources application-wide to cast values, set
config.active_resource.cast_values: