class RailsAdmin::Adapters::Mongoid::Property

Constants

STRING_TYPE_COLUMN_NAMES

Attributes

model[R]
property[R]

Public Class Methods

new(property, model) click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 10
def initialize(property, model)
  @property = property
  @model = model
end

Public Instance Methods

association?() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 64
def association?
  false
end
length() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 52
def length
  (length_validation_lookup || 255) if type == :string
end
name() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 15
def name
  (property.options[:as] || property.name).to_sym
end
nullable?() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 56
def nullable?
  true
end
pretty_name() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 19
def pretty_name
  (property.options[:as] || property.name).to_s.tr('_', ' ').capitalize
end
read_only?() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 68
def read_only?
  model.readonly_attributes.include? property.name.to_s
end
serial?() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 60
def serial?
  name == :_id
end
type() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 23
def type
  case property.type.to_s
  when 'Array', 'Hash', 'Money'
    :serialized
  when 'BigDecimal'
    :decimal
  when 'Boolean', 'Mongoid::Boolean'
    :boolean
  when 'BSON::ObjectId', 'Moped::BSON::ObjectId'
    :bson_object_id
  when 'Date'
    :date
  when 'ActiveSupport::TimeWithZone', 'DateTime', 'Time'
    :datetime
  when 'Float'
    :float
  when 'Integer'
    :integer
  when 'Object'
    object_field_type
  when 'String'
    string_field_type
  when 'Symbol'
    :string
  else
    :string
  end
end

Private Instance Methods

length_validation_lookup() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 91
def length_validation_lookup
  shortest = model.validators.select { |validator| validator.respond_to?(:attributes) && validator.attributes.include?(name.to_sym) && validator.kind == :length && validator.options[:maximum] }.min do |a, b|
    a.options[:maximum] <=> b.options[:maximum]
  end

  shortest && shortest.options[:maximum]
end
object_field_type() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 74
def object_field_type
  association = Association.new model.relations.values.detect { |r| r.try(:foreign_key).try(:to_sym) == name }, model
  if %i[belongs_to referenced_in embedded_in].include?(association.macro)
    :bson_object_id
  else
    :string
  end
end
string_field_type() click to toggle source
# File lib/rails_admin/adapters/mongoid/property.rb, line 83
def string_field_type
  if ((length = length_validation_lookup) && length < 256) || STRING_TYPE_COLUMN_NAMES.include?(name)
    :string
  else
    :text
  end
end