Module: RailsAdmin::Adapters::Mongoid

Defined in:
lib/rails_admin/adapters/mongoid.rb,
lib/rails_admin/adapters/mongoid/property.rb,
lib/rails_admin/adapters/mongoid/extension.rb,
lib/rails_admin/adapters/mongoid/association.rb,
lib/rails_admin/adapters/mongoid/abstract_object.rb

Defined Under Namespace

Modules: Extension Classes: AbstractObject, Association, Property, StatementBuilder

Constant Summary collapse

DISABLED_COLUMN_TYPES =
['Range', 'Moped::BSON::Binary', 'BSON::Binary', 'Mongoid::Geospatial::Point']
ObjectId =

rubocop:disable ConstantName

defined?(Moped::BSON) ? Moped::BSON::ObjectId : BSON::ObjectId

Instance Method Summary collapse

Instance Method Details

#adapter_supports_joins?Boolean

Returns:

  • (Boolean)

91
92
93
# File 'lib/rails_admin/adapters/mongoid.rb', line 91

def adapter_supports_joins?
  false
end

#all(options = {}, scope = nil) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_admin/adapters/mongoid.rb', line 36

def all(options = {}, scope = nil)
  scope ||= scoped
  scope = scope.includes(*options[:include]) if options[:include]
  scope = scope.limit(options[:limit]) if options[:limit]
  scope = scope.any_in(_id: options[:bulk_ids]) if options[:bulk_ids]
  scope = scope.where(query_conditions(options[:query])) if options[:query]
  scope = scope.where(filter_conditions(options[:filters])) if options[:filters]
  if options[:page] && options[:per]
    scope = scope.send(Kaminari.config.page_method_name, options[:page]).per(options[:per])
  end
  scope = sort_by(options, scope) if options[:sort]
  scope
end

#associationsObject


62
63
64
65
66
# File 'lib/rails_admin/adapters/mongoid.rb', line 62

def associations
  model.relations.values.collect do |association|
    Association.new(association, model)
  end
end

#count(options = {}, scope = nil) ⇒ Object


50
51
52
# File 'lib/rails_admin/adapters/mongoid.rb', line 50

def count(options = {}, scope = nil)
  all(options.merge(limit: false, page: false), scope).count
end

#cyclic?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'lib/rails_admin/adapters/mongoid.rb', line 87

def cyclic?
  model.cyclic?
end

#destroy(objects) ⇒ Object


54
55
56
# File 'lib/rails_admin/adapters/mongoid.rb', line 54

def destroy(objects)
  Array.wrap(objects).each(&:destroy)
end

#embedded?Boolean

Returns:

  • (Boolean)

83
84
85
# File 'lib/rails_admin/adapters/mongoid.rb', line 83

def embedded?
  model.relations.values.detect { |a| a.macro.to_sym == :embedded_in }
end

#encodingObject


79
80
81
# File 'lib/rails_admin/adapters/mongoid.rb', line 79

def encoding
  'UTF-8'
end

#first(options = {}, scope = nil) ⇒ Object


32
33
34
# File 'lib/rails_admin/adapters/mongoid.rb', line 32

def first(options = {}, scope = nil)
  all(options, scope).first
end

#get(id) ⇒ Object


17
18
19
20
21
22
23
24
25
26
# File 'lib/rails_admin/adapters/mongoid.rb', line 17

def get(id)
  AbstractObject.new(model.find(id))
rescue => e
  raise e if %w(
    BSON::InvalidObjectId
    Mongoid::Errors::DocumentNotFound
    Mongoid::Errors::InvalidFind
    Moped::Errors::InvalidObjectId
  ).exclude?(e.class.to_s)
end

#new(params = {}) ⇒ Object


13
14
15
# File 'lib/rails_admin/adapters/mongoid.rb', line 13

def new(params = {})
  AbstractObject.new(model.new(params))
end

#primary_keyObject


58
59
60
# File 'lib/rails_admin/adapters/mongoid.rb', line 58

def primary_key
  '_id'
end

#propertiesObject


68
69
70
71
72
73
# File 'lib/rails_admin/adapters/mongoid.rb', line 68

def properties
  fields = model.fields.reject { |_name, field| DISABLED_COLUMN_TYPES.include?(field.type.to_s) }
  fields.collect do |_name, field|
    Property.new(field, model)
  end
end

#scopedObject


28
29
30
# File 'lib/rails_admin/adapters/mongoid.rb', line 28

def scoped
  model.scoped
end

#table_nameObject


75
76
77
# File 'lib/rails_admin/adapters/mongoid.rb', line 75

def table_name
  model.collection_name.to_s
end