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.25.2 lib/avo/fields/select_field.rb
avo-2.7.1.pre.1 lib/avo/fields/select_field.rb
avo-2.7.0 lib/avo/fields/select_field.rb
avo-2.6.1.pre.2 lib/avo/fields/select_field.rb
avo-2.6.1.pre.1 lib/avo/fields/select_field.rb
avo-2.6.0 lib/avo/fields/select_field.rb
avo-2.5.2.pre.7 lib/avo/fields/select_field.rb
avo-2.5.2.pre.6 lib/avo/fields/select_field.rb
avo-2.5.2.pre.5 lib/avo/fields/select_field.rb
avo-2.5.2.pre.4 lib/avo/fields/select_field.rb
avo-2.5.2.pre.3 lib/avo/fields/select_field.rb
avo-2.5.2.pre.2 lib/avo/fields/select_field.rb
avo-2.5.2.pre.1 lib/avo/fields/select_field.rb
avo-2.5.1 lib/avo/fields/select_field.rb
avo-2.5.0 lib/avo/fields/select_field.rb
avo-2.4.1 lib/avo/fields/select_field.rb
avo-2.4.0 lib/avo/fields/select_field.rb
avo-2.3.1.pre.6 lib/avo/fields/select_field.rb
avo-2.3.1.pre.5 lib/avo/fields/select_field.rb
avo-2.3.1.pre.4 lib/avo/fields/select_field.rb