Module: ActiveMongoid::Finders::ClassMethods

Defined in:
lib/active_mongoid/finders.rb

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/active_mongoid/finders.rb', line 7

def find(*args)
  key = args.flatten.first
  if !key.is_a?(Fixnum) && (key.is_a?(::ActiveMongoid::BSON::ObjectId) || ::ActiveMongoid::BSON::ObjectId.legal?(key))
    where({_id: key.to_s}).first.tap do |obj|
      raise ActiveRecord::RecordNotFound unless obj
    end
  else
    FinderProxy.new(super(*args))
  end
end

#includes(*args) ⇒ Object



40
41
42
# File 'lib/active_mongoid/finders.rb', line 40

def includes(*args)
  FinderProxy.new(super(*args))
end

#joins(*args) ⇒ Object



44
45
46
# File 'lib/active_mongoid/finders.rb', line 44

def joins(*args)
  FinderProxy.new(super(*args))
end

#scoped(options = nil) ⇒ Object



36
37
38
# File 'lib/active_mongoid/finders.rb', line 36

def scoped(options = nil)
  FinderProxy.new(super(options))
end

#select(select = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/active_mongoid/finders.rb', line 48

def select(select = nil)
  FinderProxy.new(
    if block_given?
      load_target.select.each { |e| yield e }
    else
      scoped.select(select)
    end
  )
end

#where(opts = :chain, *rest) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_mongoid/finders.rb', line 18

def where(opts = :chain, *rest)
  if opts && opts.respond_to?(:select)
    bson_opts = opts.select{|k,v| v.is_a?(::ActiveMongoid::BSON::ObjectId)}

    if bson_opts[:id]
      opts.delete(:id)
      bson_opts[:_id] = bson_opts.delete(:id)
    end

    bson_opts.each do |k,v|
      bson_opts[k] = v.to_s
    end

    opts.merge!(bson_opts)
  end
  FinderProxy.new(super(opts, *rest))
end