Sha256: 82c5f6af898d69f400d98957d680928f5f1609341567152a0ae89b64acf57607

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 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].deserialize(value)
            else
              enum.invert[type_cast_value(value)]
            end
          end

          def parse_input(params)
            params[name] = parse_value(params[name]) if params[name]
          end

          def form_value
            enum[super] || super
          end

        private

          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

4 entries across 4 versions & 1 rubygems

Version Path
rails_admin-1.2.0 lib/rails_admin/config/fields/types/active_record_enum.rb
rails_admin-1.1.1 lib/rails_admin/config/fields/types/active_record_enum.rb
rails_admin-1.1.0 lib/rails_admin/config/fields/types/active_record_enum.rb
rails_admin-1.0.0 lib/rails_admin/config/fields/types/active_record_enum.rb