Class: AttrEnumerator::Options

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Configurable
Defined in:
lib/attr_enumerator.rb

Constant Summary collapse

VALIDATION_OPTIONS =
[:in, :message, :allow_nil, :allow_blank, :if, :unless].freeze

Class Method Summary collapse

Class Method Details

.parse(field, choices, opts, klass) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/attr_enumerator.rb', line 41

def self.parse(field, choices, opts, klass)
  {}.merge!(config).merge!(opts).merge!(:in => choices).tap do |options|
    options[:validation_options] = options.slice(*VALIDATION_OPTIONS)
    options.reject!{|k,v| VALIDATION_OPTIONS.include?(k)}

    options[:create_scopes] &= klass.respond_to?(:scope)

    [:prefix, :suffix].each do |affix|
      options[affix] = case options[affix]
      when true then field.to_s
      when false then nil
      else options[affix].to_s
      end
    end

    options[:constant] = case options[:create_constant]
    when true then field.to_s.pluralize.upcase
    when false then nil
    else options[:create_constant].to_s
    end

    options[:formatted_choices] = choices.map do |choice|
      formatted_choice = choice.to_s.underscore.parameterize('_')
      formatted_choice.insert(0, options[:prefix] + '_') unless options[:prefix].blank?
      formatted_choice << '_' + options[:suffix] unless options[:suffix].blank?
      [formatted_choice, choice]
    end
  end
end