Permalink
Browse files

find_all_across_pages should use dynamic finders, too

integrating work from odorcicd:

odorcicd@f785b9d
  • Loading branch information...
1 parent b5a8f09 commit 082ef8ebd7a912e1d35c7bcd143a483cba05ae48 @kmayer kmayer committed Nov 6, 2010
Showing with 35 additions and 16 deletions.
  1. +3 −1 lib/highrise/base.rb
  2. +32 −15 spec/highrise/base_spec.rb
@@ -6,7 +6,9 @@ def self.method_missing(method, *args)
case method.to_s
when /^find_(all_)?by_([_a-zA-Z]\w*)$/
raise ArgumentError, "Dynamic finder method must take an argument." if args.empty?
- send("find", :all).send($1 == 'all_' ? 'select' : 'detect') { |container| container.send($2) == args.first }
+ options = args.extract_options!
+ resources = respond_to?(:find_all_across_pages) ? send(:find_all_across_pages, options) : send(:find, :all)
+ resources.send($1 == 'all_' ? 'select' : 'detect') { |container| container.send($2) == args.first }
else
super
end
@@ -4,28 +4,45 @@
it { subject.should be_a_kind_of ActiveResource::Base }
describe "dynamic finder methods" do
- before(:each) do
- @john_doe = Highrise::Person.new(:id => 1, :first_name => "John", :last_name => "Doe")
- @john_baker = Highrise::Person.new(:id => 2, :first_name => "John", :last_name => "Baker")
- @joe_smith = Highrise::Person.new(:id => 3, :first_name => "Joe", :last_name => "Smith")
- end
+ context "without pagination" do
+ before do
+ @deal_one = Highrise::Base.new(:id => 1, :name => "A deal")
+ @deal_two = Highrise::Base.new(:id => 2, :name => "A deal")
+ @deal_three = Highrise::Base.new(:id => 3, :name => "Another deal")
+ Highrise::Base.should_receive(:find).with(:all).and_return([@deal_one, @deal_two, @deal_three])
+ end
+ it ".find_by_(attribute) finds one" do
+ Highrise::Base.find_by_name("A deal").should == @deal_one
+ end
- it "finds one when using find_by_(attribute)" do
- Highrise::Person.should_receive(:find).with(:all).and_return([@john_doe, @john_baker, @joe_smith])
- Highrise::Person.find_by_first_name("John").should == @john_doe
+ it ".find_all_by_(attribute) finds all" do
+ Highrise::Base.find_all_by_name("A deal").should == [@deal_one, @deal_two]
+ end
end
- it "finds all when using find_all_by_(attribute)" do
- Highrise::Person.should_receive(:find).with(:all).and_return([@john_doe, @john_baker, @joe_smith])
- Highrise::Person.find_all_by_first_name("John").should == [@john_doe, @john_baker]
+ context "with pagination" do
+ before do
+ class PaginatedBaseClass < Highrise::Base; include Highrise::Pagination; end
+ @john_doe = PaginatedBaseClass.new(:id => 1, :first_name => "John")
+ @john_baker = PaginatedBaseClass.new(:id => 2, :first_name => "John")
+ @joe_smith = PaginatedBaseClass.new(:id => 3, :first_name => "Joe")
+ PaginatedBaseClass.should_receive(:find_all_across_pages).and_return([@john_doe, @john_baker, @joe_smith])
+ end
+ it ".find_by_(attribute) finds one" do
+ PaginatedBaseClass.find_by_first_name("John").should == @john_doe
+ end
+
+ it ".find_all_by_(attribute) finds all" do
+ PaginatedBaseClass.find_all_by_first_name("John").should == [@john_doe, @john_baker]
+ end
end
- it "raises an argument when no arguments passed to method missing" do
- lambda { Highrise::Person.find_all_by_first_name }.should raise_error(ArgumentError)
+ it "expects arguments to the finder" do
+ expect { Highrise::Base.find_all_by_first_name }.to raise_error(ArgumentError)
end
- it "falls back to regular method missing if method doesnt match regex" do
- lambda { Highrise::Person.any_other_method }.should raise_error(NoMethodError)
+ it "falls back to regular method missing" do
+ expect { Highrise::Base.any_other_method }.to raise_error(NoMethodError)
end
end
end

0 comments on commit 082ef8e

Please sign in to comment.