Module: ConfigurableEnum

Defined in:
lib/configurable_enum.rb,
lib/configurable_enum/version.rb,
lib/configurable_enum/enum_type.rb,
lib/generators/configurable_enum/setup_generator.rb

Defined Under Namespace

Modules: Generators Classes: EnumType

Constant Summary collapse

VERSION =
'1.0.0'

Instance Method Summary collapse

Instance Method Details

#configurable_enum(column_name, mapping_class: [name, column_name.to_s.camelize, 'Mapping'].join) ⇒ Object



5
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
33
34
35
36
37
38
39
# File 'lib/configurable_enum.rb', line 5

def configurable_enum column_name,
                      mapping_class: [name, column_name.to_s.camelize, 'Mapping'].join
  mapping_model = mapping_class.to_s.constantize
  query_method = column_name.to_s.pluralize

  decorate_attribute_type(column_name, :enum) do |subtype|
    EnumType.new(column_name, self, subtype)
  end

  cache_key = "configurable_enum/#{self.name}/#{column_name}"
  # === Example for model `Book`'s column `category` ===
  # `Book.categories` returns the hash mapping from the stored data
  define_singleton_method query_method do
    Rails.cache.fetch(cache_key) do
      ActiveSupport::HashWithIndifferentAccess.new(
          mapping_model.all.map { |record| [ record.label, record.value ] }.to_h
      )
    end
  end

  # `Book.set_categories` will update or create the mapping records
  # that is specified by keys of the hash you provided
  define_singleton_method "set_#{query_method}" do |mapping|
    mapping.each_pair do |label, value|
      value, remarks = value if value.is_a?(Array)
      mapping_model.find_by(label: label)&.update(value: value, remarks: remarks) ||
          mapping_model.create(label: label, value: value, remarks: remarks)
    end
  end

  mapping_model.class_eval do
    default_scope { order(value: :asc) }
    after_commit { Rails.cache.delete(cache_key) }
  end
end