mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Make A String Attribute Easy To Inquire About as a Rails TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1333 TILs and counting..._
|
_1334 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -840,6 +840,7 @@ _1333 TILs and counting..._
|
|||||||
- [Load Records In Batches With find_each](rails/load-records-in-batches-with-find-each.md)
|
- [Load Records In Batches With find_each](rails/load-records-in-batches-with-find-each.md)
|
||||||
- [Log SQL Queries Executed By ActiveRecord](rails/log-sql-queries-executed-by-activerecord.md)
|
- [Log SQL Queries Executed By ActiveRecord](rails/log-sql-queries-executed-by-activerecord.md)
|
||||||
- [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md)
|
- [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md)
|
||||||
|
- [Make A String Attribute Easy To Inquire About](rails/make-a-string-attribute-easy-to-inquire-about.md)
|
||||||
- [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md)
|
- [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md)
|
||||||
- [Make Remove Column Migration Reversible](rails/make-remove-column-migration-reversible.md)
|
- [Make Remove Column Migration Reversible](rails/make-remove-column-migration-reversible.md)
|
||||||
- [Manually Run A Migration From Rails Console](rails/manually-run-a-migration-from-rails-console.md)
|
- [Manually Run A Migration From Rails Console](rails/manually-run-a-migration-from-rails-console.md)
|
||||||
|
|||||||
49
rails/make-a-string-attribute-easy-to-inquire-about.md
Normal file
49
rails/make-a-string-attribute-easy-to-inquire-about.md
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
# Make A String Attribute Easy to Inquire About
|
||||||
|
|
||||||
|
Have you ever been curious why Rails environment checks work the way they do?
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> Rails.env
|
||||||
|
#=> 'development'
|
||||||
|
> Rails.env.development?
|
||||||
|
#=> true
|
||||||
|
```
|
||||||
|
|
||||||
|
What is powering this is
|
||||||
|
[`ActiveSupport::StringInquirer`](https://api.rubyonrails.org/classes/ActiveSupport/StringInquirer.html).
|
||||||
|
And since it is part of `ActiveSupport`, you can use that functionality
|
||||||
|
elsewhere in your Rails code.
|
||||||
|
|
||||||
|
Let's look at an example:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class UserWithRole
|
||||||
|
attr_reader :name, :role
|
||||||
|
|
||||||
|
def initialize(name, role)
|
||||||
|
@name = name
|
||||||
|
@role = ActiveSupport::StringInquirer.new(role)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
With that class defined, we can initialize a user and inquire about their role.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> user = UserWithRole.new('Bob', 'instructor')
|
||||||
|
#=> <UserWithRole ...>
|
||||||
|
> user.role.instructor?
|
||||||
|
#=> true
|
||||||
|
> user.role.admin?
|
||||||
|
#=> false
|
||||||
|
> user.role
|
||||||
|
#=> 'instructor'
|
||||||
|
```
|
||||||
|
|
||||||
|
This helper class makes it much cleaner to inquire about the role of a user.
|
||||||
|
Notice we don't have to do a string comparison to check if they are an
|
||||||
|
instructor, e.g.:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> user.role == 'instructor'
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user