Sha256: aef298ee9152f5faf42b7701a2e29e759a8431332a0f8d5bab77fcca774c3f25

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

module HungryForm
  module Elements::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[:options].dup
        else
          fail HungryFormException, "No options provided for #{name}"
        end

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

        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[:value]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hungryform-0.0.4 lib/hungryform/elements/base/options_element.rb