Permalink
Browse files

move scopes into a plugin

  • Loading branch information...
fauxparse committed Feb 1, 2010
1 parent cf3dd56 commit 66267b147897c60a695322cab16df433de9721c5
Showing with 58 additions and 1 deletion.
  1. +1 −0 lib/mongo_mapper.rb
  2. +2 −1 lib/mongo_mapper/document.rb
  3. +24 −0 lib/mongo_mapper/plugins/scopes.rb
  4. +31 −0 test/functional/test_scopes.rb
View
@@ -120,6 +120,7 @@ def self.normalize_object_id(value)
require 'mongo_mapper/plugins/pagination'
require 'mongo_mapper/plugins/protected'
require 'mongo_mapper/plugins/rails'
+require 'mongo_mapper/plugins/scopes'
require 'mongo_mapper/plugins/serialization'
require 'mongo_mapper/plugins/validations'
@@ -20,6 +20,7 @@ def self.included(model)
plugin Plugins::Pagination
plugin Plugins::Protected
plugin Plugins::Rails
+ plugin Plugins::Scopes
plugin Plugins::Serialization
plugin Plugins::Validations
plugin Plugins::Callbacks # for now callbacks needs to be after validations
@@ -95,7 +96,7 @@ def last(options={})
end
def all(options={})
- ScopedFinder.new(self, options)
+ find_many(options)
end
def scoped(options={})
@@ -0,0 +1,24 @@
+module MongoMapper
+ module Plugins
+ module Scopes
+ module ClassMethods
+ def all(options={})
+ ScopedFinder.new(self, options)
+ end
+
+ def scoped(options={})
+ all(options)
+ end
+
+ def method_missing(method, *args)
+ case method.to_s
+ when /^scoped_by_(.*)$/
+ send :"find_all_by_#{$1}", *args
+ else
+ super
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,31 @@
+require 'test_helper'
+require 'models'
+
+class ScopesTest < Test::Unit::TestCase
+ def setup
+ @document = Doc do
+ set_collection_name 'users'
+
+ key :first_name, String
+ key :last_name, String
+ key :age, Integer
+ key :date, Date
+ end
+ end
+
+ context "scoped find" do
+ setup do
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => 27})
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => 28})
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => 26})
+ end
+
+ should "find scoped documents with a dynamic method name" do
+ @document.scoped_by_last_name('Nunemaker', :order => 'age DESC').should == [ @doc1, @doc3 ]
+ end
+
+ should "return a scope when called with a dynamic method name" do
+ @document.scoped_by_last_name('Nunemaker').all(:age.gt => 26).should == [ @doc1 ]
+ end
+ end
+end

0 comments on commit 66267b1

Please sign in to comment.