Sha256: 8a0baa5d35911cf1a0a2245ef1e4def27995b1fa23c9f53e2a72add77a21a12c

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

module HungryForm
  module Elements
    module Base
      # The BaseOptionsElement class can be used as a base class for
      # fields with options, such as select or radio
      class OptionsElement < ActiveElement
        attr_accessor :options

        hashable :options

        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

        # Sets a value of the element
        # Checks the value from the resolver params against the available options
        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
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hungryform-0.0.11 lib/hungryform/elements/base/options_element.rb
hungryform-0.0.10 lib/hungryform/elements/base/options_element.rb
hungryform-0.0.9 lib/hungryform/elements/base/options_element.rb
hungryform-0.0.8 lib/hungryform/elements/base/options_element.rb
hungryform-0.0.7 lib/hungryform/elements/base/options_element.rb
hungryform-0.0.6 lib/hungryform/elements/base/options_element.rb