lib/mongoid/finders.rb in mongoid-0.8.9 vs lib/mongoid/finders.rb in mongoid-0.8.10

- old
+ new

@@ -34,23 +34,48 @@ # <tt>Person.find(Mongo::ObjectID.new.to_s)</tt> def find(*args) Criteria.translate(*args).execute(self) end - # Find a +Document+ by its id. - def find_by_id(id) - find(id) - end - # Find the first +Document+ given the conditions. # # Options: # # args: A +Hash+ with a conditions key and other options # # <tt>Person.first(:conditions => { :attribute => "value" })</tt> def first(*args) find(:first, *args) + end + + # Find the last +Document+ given the conditions. + # + # Options: + # + # args: A +Hash+ with a conditions key and other options + # + # <tt>Person.last(:conditions => { :attribute => "value" })</tt> + def last(*args) + return find(:last, :conditions => {}, :sort => [[:_id, :desc]]) if args.empty? + return find(:last, *args) unless args.empty? + end + + # Will execute a +Criteria+ based on the +DynamicFinder+ that gets + # generated. + # + # Options: + # + # name: The finder method name + # args: The arguments to pass to the method. + # + # Example: + # + # <tt>Person.find_all_by_title_and_age("Sir", 30)</tt> + def method_missing(name, *args) + dyna = DynamicFinder.new(name, *args) + finder, conditions = dyna.finder, dyna.conditions + results = Criteria.translate(finder, :conditions => conditions).execute(self) + results ? results : dyna.create(self) end # Find all documents in paginated fashion given the supplied arguments. # If no parameters are passed just default to offset 0 and limit 20. #