Module: RailsAdmin::Adapters::Mongoid

Defined in:
lib/rails_admin/adapters/mongoid.rb,
lib/rails_admin/adapters/mongoid/bson.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, Bson, Property, StatementBuilder

Constant Summary collapse

DISABLED_COLUMN_TYPES =
%w(Range Moped::BSON::Binary BSON::Binary Mongoid::Geospatial::Point).freeze

Instance Method Summary collapse

Instance Method Details

#adapter_supports_joins?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/rails_admin/adapters/mongoid.rb', line 107

def adapter_supports_joins?
  false
end

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails_admin/adapters/mongoid.rb', line 40

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 = query_scope(scope, options[:query]) if options[:query]
  scope = filter_scope(scope, 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
rescue NoMethodError => e
  if e.message =~ /page/
    e = e.exception <<-EOM.gsub(/^\s{12}/, '')
      #{e.message}
      If you don't have kaminari-mongoid installed, add `gem 'kaminari-mongoid'` to your Gemfile.
    EOM
  end
  raise e
end

#associationsObject



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

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

#base_classObject



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

def base_class
  klass = model
  klass = klass.superclass while klass.hereditary?
  klass
end

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



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

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

#cyclic?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/rails_admin/adapters/mongoid.rb', line 103

def cyclic?
  model.cyclic?
end

#destroy(objects) ⇒ Object



66
67
68
# File 'lib/rails_admin/adapters/mongoid.rb', line 66

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

#embedded?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rails_admin/adapters/mongoid.rb', line 99

def embedded?
  associations.detect { |a| a.macro == :embedded_in }
end

#encodingObject



95
96
97
# File 'lib/rails_admin/adapters/mongoid.rb', line 95

def encoding
  Encoding::UTF_8
end

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



36
37
38
# File 'lib/rails_admin/adapters/mongoid.rb', line 36

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

#get(id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_admin/adapters/mongoid.rb', line 21

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

#new(params = {}) ⇒ Object



17
18
19
# File 'lib/rails_admin/adapters/mongoid.rb', line 17

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

#parse_object_id(value) ⇒ Object



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

def parse_object_id(value)
  Bson.parse_object_id(value)
end

#primary_keyObject



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

def primary_key
  '_id'
end

#propertiesObject



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

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

#scopedObject



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

def scoped
  model.scoped
end

#table_nameObject



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

def table_name
  model.collection_name.to_s
end