Sha256: fdc98a0656bbb666998e7ef716f60bb77ee0836801f6936bb9ccc1789f33789d

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

require 'rails_admin/config/fields/types/enum'

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

          def type
            :enum
          end

          register_instance_option :enum do
            abstract_model.model.defined_enums[name.to_s]
          end

          register_instance_option :pretty_value do
            bindings[:object].send(name).presence || ' - '
          end

          register_instance_option :multiple? do
            false
          end

          register_instance_option :queryable do
            false
          end

          def parse_value(value)
            return unless value.present?
            if ::Rails.version >= '5'
              abstract_model.model.attribute_types[name.to_s].serialize(value)
            else
              # Depending on the colum type and AR version, we might get a
              # string or an integer, so we need to handle both cases.
              enum.fetch(value) do
                type_cast_value(value)
              end
            end
          end

          def parse_input(params)
            value = params[name]
            return unless value
            params[name] = parse_input_value(value)
          end

          def form_value
            enum[super] || super
          end

        private

          def parse_input_value(value)
            if ::Rails.version >= '5'
              abstract_model.model.attribute_types[name.to_s].deserialize(value)
            else
              enum.invert[type_cast_value(value)]
            end
          end

          def type_cast_value(value)
            if ::Rails.version >= '4.2'
              abstract_model.model.column_types[name.to_s].type_cast_from_user(value)
            else
              abstract_model.model.column_types[name.to_s].type_cast(value)
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_admin-1.4.3 lib/rails_admin/config/fields/types/active_record_enum.rb
rails_admin-1.4.2 lib/rails_admin/config/fields/types/active_record_enum.rb
rails_admin-1.4.1 lib/rails_admin/config/fields/types/active_record_enum.rb
rails_admin-1.4.0 lib/rails_admin/config/fields/types/active_record_enum.rb
rails_admin-1.3.0 lib/rails_admin/config/fields/types/active_record_enum.rb