Module: MaybeSo::ActiveModel::BooleanAttribute::ClassMethods

Defined in:
lib/maybe_so/active_model/boolean_attribute.rb

Instance Method Summary collapse

Instance Method Details

#boolean_attribute(attribute, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/maybe_so/active_model/boolean_attribute.rb', line 9

def boolean_attribute(attribute, options = {})
  truthy_values = options.delete(:truthy_values) || MaybeSo::DEFAULT_TRUTHY_VALUES
  skip_validator = options.delete(:skip_validator)

  # Override the attribute's ActiveModel or ActiveRecord setter to always set a boolean
  #
  # Example:
  # def hidden=(hidden)
  #   ...
  # end
  define_method "#{attribute}=" do |value|
    boolean = truthy_values.include? value.to_s.downcase
    if respond_to? :[]= # Use ActiveRecord setter
      self[attribute.to_sym] = boolean
    else
      instance_variable_set("@#{attribute}", boolean)
    end
  end

  # Add a validator that ensures only `true` or `false` are ever written to
  # the database, regardless of whether or not the setter above was used for
  # assignment.
  unless skip_validator
    instance_eval do
      validates_inclusion_of attribute.to_sym, in: [true, false], allow_blank: true
    end
  end
end