Permalink
Browse files

make ScopedFinder#first trigger the find

  • Loading branch information...
1 parent d7dd1d0 commit cf34ab108a56990950af05f18efb33c1a31be6c9 @fauxparse fauxparse committed Feb 1, 2010
Showing with 11 additions and 6 deletions.
  1. +0 −4 lib/mongo_mapper/plugins/scopes.rb
  2. +5 −1 lib/mongo_mapper/scoped_finder.rb
  3. +6 −1 test/functional/test_scopes.rb
@@ -12,10 +12,6 @@ def find(*args)
@model.all(options_for(*args))
end
- def first(options = {})
- scoped(options).limit(1)
- end
-
private
def options_for(*args)
if @options.respond_to? :call
@@ -4,7 +4,7 @@ class ScopedFinder
attr_reader :model, :options
- delegate :[], :each, :to_a, :to => :records
+ delegate :[], :each, :to_a, :inspect, :to => :records
def initialize(model, options = {})
@model = model
@@ -50,6 +50,10 @@ def all(options = {})
end
end
+ def first(options = {})
+ scoped(options).limit(1).to_a.first
+ end
+
def scoped(options = {})
self.class.new model, @options.compose(FinderOptions.new(model, options))
end
@@ -11,8 +11,9 @@ def setup
key :age, Integer
key :date, Date
- scope :nunemaker, { :last_name => "Nunemaker" }
+ scope :nunemaker, :last_name => "Nunemaker"
scope :named, lambda { |filter| { :first_name => filter } }
+ scope :oldest_first, :sort => [[ :age, -1 ]]
end
end
@@ -42,5 +43,9 @@ def setup
should "compose scopes correctly" do
assert_same_elements @document.nunemaker.named("John"), [ @doc1 ]
end
+
+ should "accept scope options for sort" do
+ @document.oldest_first.limit(1).should == [ @doc2 ]
+ end
end
end

0 comments on commit cf34ab1

Please sign in to comment.