Sha256: 3fc5a819f7dd1cb8956ca5f7daf4b078ebebdfb38c9b9ea4e11aeed286f3370e

Contents?: true

Size: 872 Bytes

Versions: 5

Compression:

Stored size: 872 Bytes

Contents

module HungryForm
  module Elements
    class SelectField < Base::OptionsElement
      # 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)
          self.value = resolver.params[name] if acceptable_values?
        else
          self.value = attributes.delete(:value)
        end
      end

      private

      # Check if all of the values from the resolver params are present
      # in the options.
      def acceptable_values?
        options_sym_keys = options.keys.map(&:to_sym)

        if @attributes[:multiple] && resolver.params[name].respond_to?(:map)
          (resolver.params[name].map(&:to_sym) - options_sym_keys).empty?
        else
          options_sym_keys.include?(resolver.params[name].to_sym)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hungryform-0.0.10 lib/hungryform/elements/select_field.rb
hungryform-0.0.9 lib/hungryform/elements/select_field.rb
hungryform-0.0.8 lib/hungryform/elements/select_field.rb
hungryform-0.0.7 lib/hungryform/elements/select_field.rb
hungryform-0.0.6 lib/hungryform/elements/select_field.rb