Module: ActiveRecord::Exnum

Defined in:
lib/exnum/active_record/exnum.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

OPTIONS =
%i[_prefix _suffix]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



5
6
7
# File 'lib/exnum/active_record/exnum.rb', line 5

def self.prepended(base)
  base.singleton_class.class_eval { self.prepend(ClassMethods) }
end

Instance Method Details

#exnum(definitions) ⇒ Object




24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/exnum/active_record/exnum.rb', line 24

def exnum(definitions)
  enum_num, enum_values, enum_options = extract_enum_args(definitions)
  enum(enum_num, enum_values, **enum_options)

  pram_definitions = definitions.each_with_object({}) do |(name, values), ret|
    next if OPTIONS.include?(name)

    ret[name] = extract_params(values)
    self.send(name.to_s.pluralize).each do |k, v|
      ret[name][k.to_sym] ||= {}
      ret[name][k.to_sym][:val] = v
    end
  end

  enum_i18n(pram_definitions)
  enum_param(pram_definitions)
  enum_for_select(pram_definitions)
end

#inherited(base) ⇒ Object



17
18
19
20
# File 'lib/exnum/active_record/exnum.rb', line 17

def inherited(base)
  base.defined_params = defined_params.deep_dup
  super
end