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]

Instance Method Summary collapse

Instance Method Details

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rails_admin/adapters/mongoid.rb', line 30

def all(options = {},scope=nil)
  scope ||= self.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]
  scope = scope.page(options[:page]).per(options[:per]) if options[:page] && options[:per]
  scope = if options[:sort] && options[:sort_reverse]
    scope.desc(options[:sort])
  elsif options[:sort]
    scope.asc(options[:sort])
  else
    scope
  end
end

#associationsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rails_admin/adapters/mongoid.rb', line 59

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),
      :as => association_as_lookup(association),
      :polymorphic => association_polymorphic_lookup(association),
      :inverse_of => association_inverse_of_lookup(association),
      :read_only => nil,
      :nested_form => nil
    }
  end
end

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



47
48
49
# File 'lib/rails_admin/adapters/mongoid.rb', line 47

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

#destroy(objects) ⇒ Object



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

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

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



26
27
28
# File 'lib/rails_admin/adapters/mongoid.rb', line 26

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

#get(id) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rails_admin/adapters/mongoid.rb', line 14

def get(id)
  if object = model.where(:_id=>BSON::ObjectId(id)).first
    AbstractObject.new object
  else
    nil
  end
end

#new(params = {}) ⇒ Object



10
11
12
# File 'lib/rails_admin/adapters/mongoid.rb', line 10

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

#primary_keyObject



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

def primary_key
  :_id
end

#propertiesObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rails_admin/adapters/mongoid.rb', line 78

def properties
  @properties if @properties
  @properties = model.fields.map do |name,field|
    ar_type =
      if name == '_type'
        { :type => :mongoid_type, :length => 1024 }
      elsif field.type.to_s == 'String'
        if (length = length_validation_lookup(name)) && length < 256
          { :type => :string, :length => length }
        elsif STRING_TYPE_COLUMN_NAMES.include?(name.to_sym)
          { :type => :string, :length => 255 }
        else
          { :type => :text, :length => nil }
        end
      else
        {
          "Array"          => { :type => :serialized, :length => nil },
          "BigDecimal"     => { :type => :string, :length => 1024 },
          "Boolean"        => { :type => :boolean, :length => nil },
          "BSON::ObjectId" => { :type => :bson_object_id, :length => nil },
          "Date"           => { :type => :date, :length => nil },
          "DateTime"       => { :type => :datetime, :length => nil },
          "Float"          => { :type => :float, :length => nil },
          "Hash"           => { :type => :serialized, :length => nil },
          "Integer"        => { :type => :integer, :length => nil },
          "Time"           => { :type => :datetime, :length => nil },
          "Object"         => { :type => :bson_object_id, :length => nil },
        }[field.type.to_s] or raise "Need to map field #{field.type.to_s} for field name #{name} in #{model.inspect}"
      end

    {
      :name => field.name.to_sym,
      :pretty_name => field.name.to_s.gsub('_', ' ').strip.capitalize,
      :nullable? => true,
      :serial? => false,
    }.merge(ar_type)
  end
end

#scopedObject



22
23
24
# File 'lib/rails_admin/adapters/mongoid.rb', line 22

def scoped
  model.scoped
end

#serialized_attributesObject



121
122
123
124
125
# File 'lib/rails_admin/adapters/mongoid.rb', line 121

def serialized_attributes
  # Mongoid Array and Hash type columns are mapped to RA serialized type
  # through type detection in self#properties.
  []
end

#table_nameObject



117
118
119
# File 'lib/rails_admin/adapters/mongoid.rb', line 117

def table_name
  model.collection.name
end