Class: HungryForm::Elements::Base::OptionsElement

Inherits:
ActiveElement show all
Defined in:
lib/hungryform/elements/base/options_element.rb

Overview

The BaseOptionsElement class can be used as a base class for fields with options, such as select or radio

Direct Known Subclasses

RadioGroup, SelectField

Instance Attribute Summary collapse

Attributes inherited from ActiveElement

#error, #required, #value

Attributes inherited from Element

#attributes, #dependency, #label, #name, #placeholders, #resolver, #visible

Instance Method Summary collapse

Methods inherited from ActiveElement

#clear_error, #invalid?, #valid?, #validate_rule

Methods inherited from Element

#configuration, #dependency_json

Methods included from Hashable

included, #to_hash

Constructor Details

#initialize(name, parent, resolver, attributes = {}, &block) ⇒ OptionsElement

Returns a new instance of OptionsElement.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hungryform/elements/base/options_element.rb', line 11

def initialize(name, parent, resolver, attributes = {}, &block)
  if attributes.key?(:options)
    self.options = attributes.delete(:options)
  else
    fail HungryFormException, "No options provided for #{name}"
  end

  unless options.is_a?(Hash)
    self.options = resolver.get_value(options, self)
  end

  self.options = ActiveSupport::HashWithIndifferentAccess.new(options)

  super
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/hungryform/elements/base/options_element.rb', line 7

def options
  @options
end

Instance Method Details

#set_valueObject

Sets a value of the element Checks the value from the resolver params against the available options



29
30
31
32
33
34
35
# File 'lib/hungryform/elements/base/options_element.rb', line 29

def set_value
  if resolver.params.key?(name) && options.key?(resolver.params[name])
    self.value = resolver.params[name]
  else
    self.value = attributes.delete(:value)
  end
end