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/object_extension.rb

Defined Under Namespace

Modules: Extension, ObjectExtension Classes: 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)


111
112
113
# File 'lib/rails_admin/adapters/mongoid.rb', line 111

def adapter_supports_joins?
  false
end

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_admin/adapters/mongoid.rb', line 46

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]
  scope = scope.send(Kaminari.config.page_method_name, options[:page]).per(options[:per]) if options[:page] && options[:per]
  scope = sort_by(options, scope) if options[:sort]
  scope
rescue NoMethodError => e
  if /page/.match?(e.message)
    e = e.exception <<~ERROR
      #{e.message}
      If you don't have kaminari-mongoid installed, add `gem 'kaminari-mongoid'` to your Gemfile.
    ERROR
  end
  raise e
end

#associationsObject



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

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

#base_classObject



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

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

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



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

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

#cyclic?Boolean

Returns:

  • (Boolean)


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

def cyclic?
  model.cyclic?
end

#destroy(objects) ⇒ Object



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

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

#embedded?Boolean

Returns:

  • (Boolean)


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

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

#encodingObject



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

def encoding
  Encoding::UTF_8
end

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



42
43
44
# File 'lib/rails_admin/adapters/mongoid.rb', line 42

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

#get(id, scope = scoped) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rails_admin/adapters/mongoid.rb', line 23

def get(id, scope = scoped)
  object = scope.find(id)
  return nil unless object

  object.extend(ObjectExtension)
rescue StandardError => e
  raise e if %w[
    Mongoid::Errors::DocumentNotFound
    Mongoid::Errors::InvalidFind
    Moped::Errors::InvalidObjectId
    BSON::InvalidObjectId
    BSON::Error::InvalidObjectId
  ].exclude?(e.class.to_s)
end

#new(params = {}) ⇒ Object



19
20
21
# File 'lib/rails_admin/adapters/mongoid.rb', line 19

def new(params = {})
  model.new(params).extend(ObjectExtension)
end

#parse_object_id(value) ⇒ Object



15
16
17
# File 'lib/rails_admin/adapters/mongoid.rb', line 15

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

#primary_keyObject



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

def primary_key
  '_id'
end

#propertiesObject



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

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



38
39
40
# File 'lib/rails_admin/adapters/mongoid.rb', line 38

def scoped
  model.scoped
end

#table_nameObject



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

def table_name
  model.collection_name.to_s
end