# File lib/rails_admin/adapters/mongoid.rb, line 74 def primary_key '_id' end
module RailsAdmin::Adapters::Mongoid
Constants
- DISABLED_COLUMN_TYPES
Public Instance Methods
adapter_supports_joins?()
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 111 def adapter_supports_joins? false end
all(options = {}, scope = nil)
click to toggle source
# 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
associations()
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 78 def associations model.relations.values.collect do |association| Association.new(association, model) end end
base_class()
click to toggle source
# 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)
click to toggle source
# 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?()
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 107 def cyclic? model.cyclic? end
destroy(objects)
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 70 def destroy(objects) Array.wrap(objects).each(&:destroy) end
embedded?()
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 103 def embedded? associations.detect { |a| a.macro == :embedded_in } end
encoding()
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 99 def encoding Encoding::UTF_8 end
first(options = {}, scope = nil)
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 42 def first(options = {}, scope = nil) all(options, scope).first end
get(id, scope = scoped)
click to toggle source
# 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 = {})
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 19 def new(params = {}) model.new(params).extend(ObjectExtension) end
parse_object_id(value)
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 15 def parse_object_id(value) Bson.parse_object_id(value) end
primary_key()
click to toggle source
properties()
click to toggle source
# 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
scoped()
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 38 def scoped model.scoped end
table_name()
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 95 def table_name model.collection_name.to_s end
Private Instance Methods
build_statement(column, type, value, operator)
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 117 def build_statement(column, type, value, operator) StatementBuilder.new(column, type, value, operator).to_statement end
filter_scope(scope, filters, fields = config.list.fields.select(&:filterable?))
click to toggle source
filters example => {“string_field”=>{“0055”=>{“o”=>“like”, “v”=>“test_value”}}, …} “0055” is the filter index, no use here. o is the operator, v the value
# File lib/rails_admin/adapters/mongoid.rb, line 152 def filter_scope(scope, filters, fields = config.list.fields.select(&:filterable?)) statements = [] filters.each_pair do |field_name, filters_dump| filters_dump.each_value do |filter_dump| field = fields.detect { |f| f.name.to_s == field_name } next unless field value = parse_field_value(field, filter_dump[:v]) conditions_per_collection = make_field_conditions(field, value, (filter_dump[:o] || 'default')) field_statements = make_condition_for_current_collection(field, conditions_per_collection) if field_statements.many? statements << {'$or' => field_statements} elsif field_statements.any? statements << field_statements.first end end end scope.where(statements.any? ? {'$and' => statements} : {}) end
make_condition_for_current_collection(target_field, conditions_per_collection)
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 183 def make_condition_for_current_collection(target_field, conditions_per_collection) result = [] conditions_per_collection.each do |collection_name, conditions| if collection_name == table_name # conditions referring current model column are passed directly result.concat conditions else # otherwise, collect ids of documents that satisfy search condition result.concat perform_search_on_associated_collection(target_field.name, conditions) end end result end
make_field_conditions(field, value, operator)
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 121 def make_field_conditions(field, value, operator) conditions_per_collection = {} field.searchable_columns.each do |column_infos| collection_name, column_name = parse_collection_name(column_infos[:column]) statement = build_statement(column_name, column_infos[:type], value, operator) next unless statement conditions_per_collection[collection_name] ||= [] conditions_per_collection[collection_name] << statement end conditions_per_collection end
parse_collection_name(column)
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 174 def parse_collection_name(column) collection_name, column_name = column.split('.') if associations.detect { |a| a.name == collection_name.to_sym }.try(:embeds?) [table_name, column] else [collection_name, column_name] end end
perform_search_on_associated_collection(field_name, conditions)
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 197 def perform_search_on_associated_collection(field_name, conditions) target_association = associations.detect { |a| a.name == field_name } return [] unless target_association model = target_association.klass case target_association.type when :belongs_to, :has_and_belongs_to_many [{target_association.foreign_key.to_s => {'$in' => model.where('$or' => conditions).all.collect { |r| r.send(target_association.primary_key) }}}] when :has_many, :has_one [{target_association.primary_key.to_s => {'$in' => model.where('$or' => conditions).all.collect { |r| r.send(target_association.foreign_key) }}}] end end
query_scope(scope, query, fields = config.list.fields.select(&:queryable?))
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 134 def query_scope(scope, query, fields = config.list.fields.select(&:queryable?)) if config.list.search_by scope.send(config.list.search_by, query) else statements = [] fields.each do |field| value = parse_field_value(field, query) conditions_per_collection = make_field_conditions(field, value, field.search_operator) statements.concat make_condition_for_current_collection(field, conditions_per_collection) end scope.where(statements.any? ? {'$or' => statements} : {}) end end
sort_by(options, scope)
click to toggle source
# File lib/rails_admin/adapters/mongoid.rb, line 210 def sort_by(options, scope) return scope unless options[:sort] case options[:sort] when String field_name, collection_name = options[:sort].split('.').reverse raise 'sorting by associated model column is not supported in Non-Relational databases' if collection_name && collection_name != table_name when Symbol field_name = options[:sort].to_s end if options[:sort_reverse] scope.asc field_name else scope.desc field_name end end