|
3 | 3 | module ActiveRecord
|
4 | 4 | module AttributeMethods
|
5 | 5 | # = Active Record Attribute Methods \Query
|
| 6 | + # |
| 7 | + # Adds query methods for attributes that return either +true+ or +false+ |
| 8 | + # depending on the attribute type and value. |
| 9 | + # |
| 10 | + # For Boolean attributes this will return +true+ if the value is present |
| 11 | + # and return +false+ otherwise: |
| 12 | + # |
| 13 | + # class Product < ActiveRecord::Base |
| 14 | + # end |
| 15 | + # |
| 16 | + # product = Product.new(archived: false) |
| 17 | + # product.archived? # => false |
| 18 | + # product.archived = true |
| 19 | + # product.archived? # => true |
| 20 | + # |
| 21 | + # For Numeric attributes this will return +true+ if the value is a non-zero |
| 22 | + # number and return +false+ otherwise: |
| 23 | + # |
| 24 | + # product.inventory_count = 0 |
| 25 | + # product.inventory_count? # => false |
| 26 | + # product.inventory_count = 1 |
| 27 | + # product.inventory_count? # => true |
| 28 | + # |
| 29 | + # For other attributes it will return +true+ if the value is present |
| 30 | + # and return +false+ otherwise: |
| 31 | + # |
| 32 | + # product.name = nil |
| 33 | + # product.name? # => false |
| 34 | + # product.name = " " |
| 35 | + # product.name? # => false |
| 36 | + # product.name = "Orange" |
| 37 | + # product.name? # => true |
6 | 38 | module Query
|
7 | 39 | extend ActiveSupport::Concern
|
8 | 40 |
|
9 | 41 | included do
|
10 | 42 | attribute_method_suffix "?", parameters: false
|
11 | 43 | end
|
12 | 44 |
|
| 45 | + # Returns +true+ or +false+ for the attribute identified by +attr_name+, |
| 46 | + # depending on the attribute type and value. |
13 | 47 | def query_attribute(attr_name)
|
14 | 48 | value = self.public_send(attr_name)
|
15 | 49 |
|
|
0 commit comments