The ActiveModel::Attributes
API allows us to define different types of
attributes.
It handles type casting, default values, and defines read/write methods.
Typically when defining a boolean attribute, you will often define the
respective predicate method.
For example:
class Person
include ActiveModel::Attributes
attribute :loves_rails, :boolean
def loves_rails?
!!loves_rails
end
end
person = Person.new
person.loves_rails # => nil
person.loves_rails? # => false
person.loves_rails = true
person.loves_rails? # => true
This proposal defines this predicate method automatically for all boolean
attributes.
I did a little bit of digging, and came up with this initial solution, but it might not
be an appropriate implementation.
rails:main
← cpjmcquillan:active-model-attributes-predicate-method
opened 09:04AM - 29 Oct 22 UTC
<!--
Thanks for contributing to Rails!
Please do not make *Draft* pull reque… sts, as they still send
notifications to everyone watching the Rails repo.
Create a pull request when it is ready for review and feedback
from the Rails team :).
If your pull request affects documentation or any non-code
changes, guidelines for those changes are [available
here](https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation)
About this template
The following template aims to help contributors write a good description for their pull requests.
We'd like you to provide a description of the changes in your pull request (i.e. bugs fixed or features added), motivation behind the changes, and complete the checklist below before opening a pull request.
Feel free to discard it if you need to (e.g. when you just fix a typo). -->
### Motivation / Background
<!--
Describe why this Pull Request needs to be merged. What bug have you fixed? What feature have you added? Why is it important?
If you are fixing a specific issue, include "Fixes #ISSUE" (replace with the issue number, remove the quotes) and the issue will be linked to this PR.
-->
The `ActiveModel::Attributes` API allows us to define different types of attributes.
It handles type casting, default values, and defines `attr_methods`.
Typically when defining a boolean attribute, you will often define the respective predicate method.
For example:
```ruby
class Person
include ActiveModel::Attributes
attribute :loves_rails, :boolean
def loves_rails?
!!loves_rails
end
end
person = Person.new
person.loves_rails # => nil
person.loves_rails? # => false
person.loves_rails = true
person.loves_rails? # => true
```
This Pull Request has been created because it would be useful for boolean
attributes to automatically have its respective predicate method defined.
### Detail
This Pull Request changes the `attribute` method in `ActiveModel::AttributeRegistration`,
so that it defines `attribute?` methods for attributes with a boolean type.
### Additional information
### Checklist
Before submitting the PR make sure the following are checked:
* [ ] This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
* [ ] Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: `[Fix #issue-number]`
* [ ] Tests are added or updated if you fix a bug or add a feature.
* [ ] CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.
* [ ] CI is passing.