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, NestedAttributesExtension Classes: AbstractObject

Constant Summary collapse

STRING_TYPE_COLUMN_NAMES =
[:name, :title, :subject]
DISABLED_COLUMN_TYPES =
['Range']

Instance Method Summary collapse

Instance Method Details

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



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

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]
  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
end

#associationsObject



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

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_nested_attributes_options_lookup(association)
    }
  end
end

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



45
46
47
# File 'lib/rails_admin/adapters/mongoid.rb', line 45

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

#destroy(objects) ⇒ Object



49
50
51
# File 'lib/rails_admin/adapters/mongoid.rb', line 49

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

#embedded?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/rails_admin/adapters/mongoid.rb', line 134

def embedded?
  @embedded ||= !!model.associations.values.find{|a| a.macro.to_sym == :embedded_in }
end

#encodingObject



130
131
132
# File 'lib/rails_admin/adapters/mongoid.rb', line 130

def encoding
  'UTF-8'
end

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



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

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

#get(id) ⇒ Object



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

def get(id)
  begin
    AbstractObject.new(model.find(id))
  rescue BSON::InvalidObjectId, ::Mongoid::Errors::DocumentNotFound
    nil
  end
end

#new(params = {}) ⇒ Object



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

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

#primary_keyObject



53
54
55
# File 'lib/rails_admin/adapters/mongoid.rb', line 53

def primary_key
  '_id'
end

#propertiesObject



77
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
116
117
118
# File 'lib/rails_admin/adapters/mongoid.rb', line 77

def properties
  fields = model.fields.reject{|name, field| DISABLED_COLUMN_TYPES.include?(field.type.to_s) }
  fields.map do |name,field|
    ar_type = {
      "Array"          => { :type => :serialized },
      "BigDecimal"     => { :type => :decimal },
      "Boolean"        => { :type => :boolean },
      "BSON::ObjectId" => { :type => :bson_object_id, :serial? => (name == primary_key) },
      "Date"           => { :type => :date },
      "DateTime"       => { :type => :datetime },
      "Float"          => { :type => :float },
      "Hash"           => { :type => :serialized },
      "Integer"        => { :type => :integer },
      "Object"         => (
        if associations.find{|a| a[:type] == :belongs_to && a[:foreign_key] == name.to_sym}
          { :type => :bson_object_id }
        else
          { :type => :string, :length => 255 }
        end
      ),
      "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 }
        end
      ),
      "Symbol"         => { :type => :string, :length => 255 },
      "Time"           => { :type => :datetime },
    }[field.type.to_s] or raise "Need to map field #{field.type.to_s} for field name #{name} in #{model.inspect}"

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

#scopedObject



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

def scoped
  model.scoped
end

#serialized_attributesObject



124
125
126
127
128
# File 'lib/rails_admin/adapters/mongoid.rb', line 124

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

#table_nameObject



120
121
122
# File 'lib/rails_admin/adapters/mongoid.rb', line 120

def table_name
  model.collection.name
end