Class: ArDocStore::Attributes::Enumeration

Inherits:
Base
  • Object
show all
Defined in:
lib/ar_doc_store/attributes/enumeration.rb

Instance Attribute Summary

Attributes inherited from Base

#attribute, #conversion, #default, #model, #options, #predicate

Instance Method Summary collapse

Methods inherited from Base

#add_ransacker, #attribute_options, #attribute_type, build, #define_query_method, #embedded?, #initialize, #store_attribute

Methods included from CallbackSupport

included

Constructor Details

This class inherits a constructor from ArDocStore::Attributes::Base

Instance Method Details

#buildObject



4
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
# File 'lib/ar_doc_store/attributes/enumeration.rb', line 4

def build
  key = attribute.to_sym
  dictionary = options[:values]
  multiple = options[:multiple]
  strict = options[:strict]
  default_value = default
  model.class_eval do
    if multiple
      json_attribute key, as: :array, default: default_value
      if strict
        define_method "validate_#{key}" do
          value = public_send(key)
          errors.add(key, :invalid) if value.is_a?(Array) && value.present? && value.reject(&:blank?).detect {|d| !dictionary.include?(d)}
        end
        validate "validate_#{key}".to_sym
      end
    else
      json_attribute key, as: :string, default: default_value
      if strict
        validates_inclusion_of key, in: dictionary, allow_blank: true
      end
    end
    define_singleton_method "#{key}_choices" do
      dictionary
    end
  end
end

#typeObject



32
33
34
# File 'lib/ar_doc_store/attributes/enumeration.rb', line 32

def type
  :string
end