Sha256: c6bbf266bda02909d7d8e06511d0ccb1496fefcbc47daa29ea0b85ecf7578db4

Contents?: true

Size: 1010 Bytes

Versions: 11

Compression:

Stored size: 1010 Bytes

Contents

# frozen_string_literal: true

module Phlexi
  module Form
    module Components
      module Concerns
        module HasOptions
          protected

          def build_attributes
            super
            @collection = attributes.delete(:collection) || field.collection
            @label_method = attributes.delete(:label_method)
            @value_method = attributes.delete(:value_method)
          end

          def option_mapper
            @option_mapper ||= OptionMapper.new(@collection, label_method: @label_method, value_method: @value_method)
          end

          def selected?(option)
            if attributes[:multiple]
              @options_list ||= Array(field.value)
              @options_list.any? { |item| item.to_s == option.to_s }
            else
              field.value.to_s == option.to_s
            end
          end

          def normalize_simple_input(input_value)
            ([super] & option_mapper.values)[0]
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
phlexi-form-0.4.8 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.4.7 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.4.6 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.4.5 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.4.4 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.4.3 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.4.2 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.4.1 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.4.0 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.3.0 lib/phlexi/form/components/concerns/has_options.rb
phlexi-form-0.3.0.rc1 lib/phlexi/form/components/concerns/has_options.rb