Module: Enumerize::ActiveRecordSupport

Defined in:
lib/enumerize/activerecord.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#enumerize(name, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/enumerize/activerecord.rb', line 3

def enumerize(name, options={})
  super

  _enumerize_module.dependent_eval do
    if self < ::ActiveRecord::Base
      include InstanceMethods

      # Since Rails use `allocate` method on models and initializes them with `init_with` method.
      # This way `initialize` method is not being called, but `after_initialize` callback always gets triggered.
      after_initialize :_set_default_value_for_enumerized_attributes

      # https://github.com/brainspec/enumerize/issues/111
      require 'enumerize/hooks/uniqueness'
    end
  end
end

#update_all(updates) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/enumerize/activerecord.rb', line 45

def update_all(updates)
  if updates.is_a?(Hash)
    enumerized_attributes.each do |attr|
      next if updates[attr.name].blank? || attr.kind_of?(Enumerize::Multiple)
      updates[attr.name] = attr.find_value(updates[attr.name]).value
    end
  end

  super(updates)
end