forked from mongomapper/mongomapper
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
insert ScopedFinder as a proxy in Document#all
- Loading branch information
Showing
7 changed files
with
105 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module MongoMapper | ||
class ScopedFinder | ||
include Enumerable | ||
|
||
attr_reader :model, :options | ||
|
||
delegate :[], :each, :to_a, :to => :records | ||
|
||
def initialize(model, options = {}) | ||
@model = model | ||
@options = FinderOptions.new(@model, options) | ||
end | ||
|
||
def proxy_options | ||
@proxy_options ||= options.to_hash | ||
end | ||
|
||
def records | ||
@records ||= model.send :find_many, proxy_options | ||
end | ||
alias :to_a :records | ||
|
||
def count | ||
@count ||= model.count(options.to_hash) | ||
end | ||
alias :size :count | ||
|
||
def empty? | ||
if @records | ||
@records.empty? | ||
else | ||
count == 0 | ||
end | ||
end | ||
|
||
def ==(other) | ||
case other | ||
when ScopedFinder | ||
@model == other.model && proxy_options == other.proxy_options | ||
else | ||
records == other | ||
end | ||
end | ||
|
||
def all(options = {}) | ||
if options.empty? | ||
self | ||
else | ||
scoped(options) | ||
end | ||
end | ||
|
||
def scoped(options = {}) | ||
self.class.new model, @options.compose(FinderOptions.new(model, options)) | ||
end | ||
|
||
def method_missing(method, *args) | ||
records.send method, *args | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'test_helper' | ||
require 'models' | ||
|
||
class ScopedFinderTest < Test::Unit::TestCase | ||
include MongoMapper | ||
|
||
context "from #all" do | ||
should "be the right class" do | ||
PostComment.all.class.should == ScopedFinder | ||
end | ||
|
||
should "retain options" do | ||
PostComment.all(:foo => "bar").proxy_options.should == { :foo => "bar", :limit => 0, :fields => nil, :skip => 0, :sort => nil } | ||
end | ||
|
||
should "compose options with scoped" do | ||
PostComment.all(:foo => "bar").scoped(:baz => "qux").proxy_options.should == | ||
{ :foo => "bar", :baz => "qux", :limit => 0, :fields => nil, :skip => 0, :sort => nil } | ||
end | ||
end | ||
end |