Module: ActiveRecord::AttributeMethods::Query

Defined in:
lib/patches/active_record/query_method.rb

Instance Method Summary collapse

Instance Method Details

#query_attribute(attr_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/patches/active_record/query_method.rb', line 6

def query_attribute(attr_name)
  unless value = read_attribute(attr_name)
    false
  else
    column = self.class.columns_hash[attr_name]
    if column.nil?
      
      # TODO submit a rails patch

      # not sure what active_record tests say but i guess this should mean:
      # call to_i and check zero? if the value is a Numeric or starts with
      # a digit, so it can meaningfully be typecasted by to_i

      # if Numeric === value || value !~ /[^0-9]/
      if Numeric === value || value.to_s =~ /^[0-9]/
        !value.to_i.zero?
      else
        return false if ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.include?(value)
        !value.blank?
      end
    elsif column.number?
      !value.zero?
    else
      !value.blank?
    end
  end
end