Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add dynamic finder methods (find_by and find_all_by) for regular obje…

…ct attributes, and remove object specific finders, add spec tests to base_spec
  • Loading branch information...
commit 370abb2ef8860b3c8969e64ef9758a9abb121d95 1 parent 5eaf099
@odorcicd authored
View
16 lib/highrise/base.rb
@@ -3,5 +3,21 @@
module Highrise
class Base < ActiveResource::Base
include ::Cachable
+
+ protected
+ # Dynamic finder for attributes
+ 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?
+ if $1 == 'all_'
+ return send(:"find", :all).select { |container| container.send($2) == args.first }
+ else
+ return send(:"find", :all).detect { |container| container.send($2) == args.first }
+ end
+ else
+ super
+ end
+ end
end
end
View
3  lib/highrise/deal_category.rb
@@ -1,7 +1,4 @@
module Highrise
class DealCategory < Base
- def self.find_by_name(name)
- find(:all).detect { |dc| dc.name == name}
- end
end
end
View
5 lib/highrise/tag.rb
@@ -3,10 +3,5 @@ class Tag < Base
def ==(object)
(object.instance_of?(self.class) && object.id == self.id && object.name == self.name)
end
-
- # You can't find :one because that finds all *objects* with that tag
- def self.find_by_name(arg)
- tags = self.find(:all).detect{|tag| tag.name == arg}
- end
end
end
View
3  lib/highrise/task_category.rb
@@ -1,7 +1,4 @@
module Highrise
class TaskCategory < Base
- def self.find_by_name(name)
- find(:all).detect { |tc| tc.name == name}
- end
end
end
View
25 spec/highrise/base_spec.rb
@@ -10,4 +10,29 @@
@base.kind_of?(ActiveResource::Base).should be_true
end
+ 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
+
+ it "should find 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
+ end
+
+ it "should find 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]
+ end
+
+ it "should raise an argument when no arguments passed to method missing" do
+ lambda { Highrise::Person.find_all_by_first_name }.should raise_error(ArgumentError)
+ end
+
+ it "should fallback to regular method missing if method doesnt match regex" do
+ lambda { Highrise::Person.any_other_method }.should raise_error(NoMethodError)
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.