Module: ActiveMongoid::Finders::ClassMethods
- Defined in:
- lib/active_mongoid/finders.rb
Instance Method Summary collapse
- #__am_primary_key ⇒ Object
- #find(*args) ⇒ Object
- #includes(*args) ⇒ Object
- #joins(*args) ⇒ Object
- #scoped(options = nil) ⇒ Object
- #select(select = nil) ⇒ Object
- #where(opts = :chain, *rest) ⇒ Object
Instance Method Details
#__am_primary_key ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/active_mongoid/finders.rb', line 61 def __am_primary_key if self.respond_to?(:primary_bson_key) && self.primary_bson_key self.primary_bson_key else :_id end end |
#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({__am_primary_key => key.to_s}).first.tap do |obj| raise ActiveRecord::RecordNotFound unless obj end else FinderProxy.new(super(*args)) end end |
#includes(*args) ⇒ Object
43 44 45 |
# File 'lib/active_mongoid/finders.rb', line 43 def includes(*args) FinderProxy.new(super(*args)) end |
#joins(*args) ⇒ Object
47 48 49 |
# File 'lib/active_mongoid/finders.rb', line 47 def joins(*args) FinderProxy.new(super(*args)) end |
#scoped(options = nil) ⇒ Object
39 40 41 |
# File 'lib/active_mongoid/finders.rb', line 39 def scoped( = nil) FinderProxy.new(super()) end |
#select(select = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/active_mongoid/finders.rb', line 51 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 35 36 37 |
# 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] && __am_primary_key != :id opts.delete(:id) bson_opts[:_id] = bson_opts.delete(:id) elsif bson_opts[:_id] && __am_primary_key == :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 |