class CckForms::ParameterTypeClass::Enum

Represents a enum (SELECT).

Public Instance Methods

build_form(form_builder, options) click to toggle source

options:

as        - if 'checkboxes', display a set of checkboxes, not SELECT
only      - leave only these keys (array/string)
except    - reverse of :only
required  - HTML required attr
# File lib/cck_forms/parameter_type_class/enum.rb, line 37
def build_form(form_builder, options)

  if options.is_a? Hash and options[:as] == 'checkboxes'
    options = options.except(:for, :as)
    checkboxes = CckForms::ParameterTypeClass::Checkboxes.new valid_values: self.valid_values, value: self.value
    return checkboxes.build_form(form_builder, options)
  end

  set_value_in_hash options

  values = valid_values_enum
  if options[:only]
    options[:only] = [options[:only]] unless options[:only].is_a? Array
    values.select! { |o| o[1].in? options[:only] }
  end

  if options[:except]
    options[:except] = [options[:except]] unless options[:except].is_a? Array
    values.reject! { |o| o[1].in? options[:except] }
  end

  form_builder.select :value, values, {selected: options[:value], required: options[:required], include_blank: !options[:required]}, class: 'form-control '
end
mongoize() click to toggle source
# File lib/cck_forms/parameter_type_class/enum.rb, line 6
def mongoize
  value.presence
end
to_s(_options = nil) click to toggle source

Current string value from valid_values

# File lib/cck_forms/parameter_type_class/enum.rb, line 11
def to_s(_options = nil)
  return '' if value.blank?
  valid_values[value].to_s
end