Module: RailsAdmin::Adapters::Mongoid
- Defined in:
- lib/rails_admin/adapters/mongoid.rb,
lib/rails_admin/adapters/mongoid/extension.rb,
lib/rails_admin/adapters/mongoid/abstract_object.rb
Defined Under Namespace
Modules: Extension Classes: AbstractObject
Constant Summary collapse
- STRING_TYPE_COLUMN_NAMES =
[:name, :title, :subject]
- DISABLED_COLUMN_TYPES =
['Range']
- ObjectId =
(::Mongoid::VERSION >= '3' ? ::Moped::BSON::ObjectId : ::BSON::ObjectId)
Instance Method Summary collapse
- #adapter_supports_joins? ⇒ Boolean
- #all(options = {}, scope = nil) ⇒ Object
- #associations ⇒ Object
- #count(options = {}, scope = nil) ⇒ Object
- #destroy(objects) ⇒ Object
- #embedded? ⇒ Boolean
- #encoding ⇒ Object
- #first(options = {}, scope = nil) ⇒ Object
- #get(id) ⇒ Object
- #new(params = {}) ⇒ Object
- #object_id_from_string(str) ⇒ Object
- #primary_key ⇒ Object
- #properties ⇒ Object
- #scoped ⇒ Object
- #table_name ⇒ Object
Instance Method Details
#adapter_supports_joins? ⇒ Boolean
112 113 114 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 112 def adapter_supports_joins? false end |
#all(options = {}, scope = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 37 def all( = {},scope=nil) scope ||= self.scoped scope = scope.includes(*[:include]) if [:include] scope = scope.limit([:limit]) if [:limit] scope = scope.any_in(:_id => [:bulk_ids]) if [:bulk_ids] scope = scope.where(query_conditions([:query])) if [:query] scope = scope.where(filter_conditions([:filters])) if [:filters] if [:page] && [:per] scope = scope.send(Kaminari.config.page_method_name, [:page]).per([:per]) end scope = sort_by(, scope) if [:sort] scope end |
#associations ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 63 def associations model.associations.values.map do |association| { :name => association.name.to_sym, :pretty_name => association.name.to_s.tr('_', ' ').capitalize, :type => association_type_lookup(association.macro), :model_proc => Proc.new { association_model_proc_lookup(association) }, :primary_key_proc => Proc.new { association_primary_key_lookup(association) }, :foreign_key => association_foreign_key_lookup(association), :foreign_type => association_foreign_type_lookup(association), :foreign_inverse_of => association_foreign_inverse_of_lookup(association), :as => association_as_lookup(association), :polymorphic => association_polymorphic_lookup(association), :inverse_of => association_inverse_of_lookup(association), :read_only => nil, :nested_form => (association) } end end |
#count(options = {}, scope = nil) ⇒ Object
51 52 53 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 51 def count( = {},scope=nil) all(.merge({:limit => false, :page => false}), scope).count end |
#destroy(objects) ⇒ Object
55 56 57 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 55 def destroy(objects) Array.wrap(objects).each &:destroy end |
#embedded? ⇒ Boolean
104 105 106 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 104 def @embedded ||= !!model.associations.values.find{|a| a.macro.to_sym == :embedded_in } end |
#encoding ⇒ Object
100 101 102 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 100 def encoding 'UTF-8' end |
#first(options = {}, scope = nil) ⇒ Object
33 34 35 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 33 def first( = {},scope=nil) all(, scope).first end |
#get(id) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 16 def get(id) begin AbstractObject.new(model.find(id)) rescue => e if ['BSON::InvalidObjectId', 'Mongoid::Errors::DocumentNotFound', 'Mongoid::Errors::InvalidFind', 'Moped::Errors::InvalidObjectId'].include? e.class.to_s nil else raise e end end end |
#new(params = {}) ⇒ Object
12 13 14 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 12 def new(params = {}) AbstractObject.new(model.new) end |
#object_id_from_string(str) ⇒ Object
108 109 110 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 108 def object_id_from_string(str) ObjectId.from_string(str) end |
#primary_key ⇒ Object
59 60 61 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 59 def primary_key '_id' end |
#properties ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 83 def properties fields = model.fields.reject{|name, field| DISABLED_COLUMN_TYPES.include?(field.type.to_s) } fields.map do |name,field| { :name => field.name.to_sym, :length => nil, :pretty_name => field.name.to_s.gsub('_', ' ').strip.capitalize, :nullable? => true, :serial? => false, }.merge(type_lookup(name, field)) end end |
#scoped ⇒ Object
29 30 31 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 29 def scoped model.scoped end |
#table_name ⇒ Object
96 97 98 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 96 def table_name model.collection_name.to_s end |