Sha256: 1885638c7272add632522b1a36f230a40f17b20b3569471ee1d87e2e20d240ef

Contents?: true

Size: 1.75 KB

Versions: 11

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require 'rails_admin/config/fields/types/string'

module RailsAdmin
  module Config
    module Fields
      module Types
        class Enum < RailsAdmin::Config::Fields::Base
          RailsAdmin::Config::Fields::Types.register(self)

          register_instance_option :filter_operators do
            %w[_discard] +
              enum.map do |label, value|
                {label: label, value: value || label}
              end + (required? ? [] : %w[_separator _present _blank])
          end

          register_instance_option :partial do
            :form_enumeration
          end

          register_instance_option :enum_method do
            @enum_method ||= bindings[:object].class.respond_to?("#{name}_enum") || (bindings[:object] || abstract_model.model.new).respond_to?("#{name}_enum") ? "#{name}_enum" : name
          end

          register_instance_option :enum do
            if abstract_model.model.respond_to?(enum_method)
              abstract_model.model.send(enum_method)
            else
              (bindings[:object] || abstract_model.model.new).send(enum_method)
            end
          end

          register_instance_option :pretty_value do
            if enum.is_a?(::Hash)
              enum.select { |_k, v| v.to_s == value.to_s }.keys.first.to_s.presence || value.presence || ' - '
            elsif enum.is_a?(::Array) && enum.first.is_a?(::Array)
              enum.detect { |e| e[1].to_s == value.to_s }.try(:first).to_s.presence || value.presence || ' - '
            else
              value.presence || ' - '
            end
          end

          register_instance_option :multiple? do
            properties && [:serialized].include?(properties.type)
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rails_admin-3.2.1 lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.2.0 lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.2.0.rc lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.2.0.beta lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.1.4 lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.1.3 lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.1.2 lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.1.1 lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.1.0 lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.1.0.rc2 lib/rails_admin/config/fields/types/enum.rb
rails_admin-3.1.0.rc lib/rails_admin/config/fields/types/enum.rb