Class: HungryForm::Elements::Base::OptionsElement
- Inherits:
-
ActiveElement
- Object
- Element
- ActiveElement
- HungryForm::Elements::Base::OptionsElement
- 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
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Attributes inherited from ActiveElement
Attributes inherited from Element
#attributes, #dependency, #label, #name, #placeholders, #resolver, #visible
Instance Method Summary collapse
-
#initialize(name, parent, resolver, attributes = {}, &block) ⇒ OptionsElement
constructor
A new instance of OptionsElement.
-
#set_value ⇒ Object
Sets a value of the element Checks the value from the resolver params against the available options.
Methods inherited from ActiveElement
#clear_error, #invalid?, #valid?, #validate_rule
Methods inherited from Element
#configuration, #dependency_json
Methods included from Hashable
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. = attributes.delete(:options) else fail HungryFormException, "No options provided for #{name}" end unless .is_a?(Hash) self. = resolver.get_value(, self) end self. = ActiveSupport::HashWithIndifferentAccess.new() super end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/hungryform/elements/base/options_element.rb', line 7 def @options end |
Instance Method Details
#set_value ⇒ Object
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) && .key?(resolver.params[name]) self.value = resolver.params[name] else self.value = attributes.delete(:value) end end |