Skip to content

Commit 6415dbb

Browse files
p8alexcameron89
andcommitted
Document AttributeMethods::Query [ci skip]
Co-authored-by: Alex Kitchens <[email protected]>
1 parent b78637e commit 6415dbb

File tree

1 file changed

+34
-0
lines changed
  • activerecord/lib/active_record/attribute_methods

1 file changed

+34
-0
lines changed

activerecord/lib/active_record/attribute_methods/query.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,47 @@
33
module ActiveRecord
44
module AttributeMethods
55
# = 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
638
module Query
739
extend ActiveSupport::Concern
840

941
included do
1042
attribute_method_suffix "?", parameters: false
1143
end
1244

45+
# Returns +true+ or +false+ for the attribute identified by +attr_name+,
46+
# depending on the attribute type and value.
1347
def query_attribute(attr_name)
1448
value = self.public_send(attr_name)
1549

0 commit comments

Comments
 (0)