Index: activerecord/lib/active_record/base.rb
===================================================================
--- activerecord/lib/active_record/base.rb (revision 7347)
+++ activerecord/lib/active_record/base.rb (working copy)
@@ -424,13 +424,55 @@
validate_find_options(options)
set_readonly_option!(options)
- case args.first
- when :first then find_initial(options)
- when :all then find_every(options)
- else find_from_ids(args, options)
+ finder.dispatch(args, options)
+ end
+
+
+ # The Finder class allows you to easily define your own custom find implementations.
+ # Simply define this class in your model, and any methods will be available
+ # to your #find call. For example,
+ #
+ # class User < ActiveRecord::Base
+ # class Finder
+ # def last(options)
+ # @base.find(:first, options.update(:order => 'id desc'))
+ # end
+ # end
+ # end
+ #
+ # User.find(:last, :conditions => ['name = ?', 'John'])
+ #
+ class Finder
+ def initialize(base_instance)
+ @base = base_instance
end
+
+ def dispatch(args, options)
+ finder_method = args.first
+ if finder_method.is_a?(Symbol) and respond_to?(finder_method)
+ send(finder_method, options)
+ else
+ from_ids(args, options)
+ end
+ end
+
+ def all(options)
+ @base.send(:find_every, options)
+ end
+
+ def first(options)
+ @base.send(:find_initial, options)
+ end
+
+ def from_ids(ids, options)
+ @base.send(:find_from_ids, ids, options)
+ end
end
+ def finder
+ @finder ||= Finder.new(self)
+ end
+