Sha256: 85969475566c8b8d69c307f26b90a37c6e57961ac99c13d33428a840212070a7

Contents?: true

Size: 1.73 KB

Versions: 114

Compression:

Stored size: 1.73 KB

Contents

module Avo
  module Fields
    class SelectField < BaseField
      attr_reader :options
      attr_reader :enum
      attr_reader :display_value

      def initialize(id, **args, &block)
        args[:placeholder] ||= I18n.t("avo.choose_an_option")

        super(id, **args, &block)

        @options = args[:options].present? ? args[:options] : args[:enum]
        @options = ActiveSupport::HashWithIndifferentAccess.new @options if @options.is_a? Hash
        @enum = args[:enum].present? ? args[:enum] : nil
        @display_value = args[:display_value].present? ? args[:display_value] : false
      end

      def options_for_select
        if options.respond_to? :call
          computed_options = options.call model: model, resource: resource, view: view, field: self
          if display_value
            computed_options.map { |label, value| [value, value] }.to_h
          else
            computed_options
          end
        elsif enum.present?
          if display_value
            options.invert
          else
            options.map { |label, value| [label, label] }.to_h
          end
        elsif display_value
          options.map { |label, value| [value, value] }.to_h
        else
          options
        end
      end

      def label
        if options.respond_to? :call
          computed_options = options.call model: model, resource: resource, view: view, field: self

          return value if display_value

          computed_options.invert.stringify_keys[value]
        elsif enum.present?
          if display_value
            options[value]
          else
            value
          end
        elsif display_value
          value
        else
          options.invert.stringify_keys[value]
        end
      end
    end
  end
end

Version data entries

114 entries across 114 versions & 1 rubygems

Version Path
avo-1.10.0 lib/avo/fields/select_field.rb
avo-1.9.1 lib/avo/fields/select_field.rb
avo-1.9.0 lib/avo/fields/select_field.rb
avo-1.8.4 lib/avo/fields/select_field.rb
avo-1.8.3 lib/avo/fields/select_field.rb
avo-1.8.2 lib/avo/fields/select_field.rb
avo-1.8.1 lib/avo/fields/select_field.rb
avo-1.8.0 lib/avo/fields/select_field.rb
avo-1.7.3 lib/avo/fields/select_field.rb
avo-1.7.3.pre.1 lib/avo/fields/select_field.rb
avo-1.7.2 lib/avo/fields/select_field.rb
avo-1.7.1 lib/avo/fields/select_field.rb
avo-1.7.0 lib/avo/fields/select_field.rb
avo-1.6.4.pre.1 lib/avo/fields/select_field.rb