Permalink
Browse files

Proxy all dynamic finders

  • Loading branch information...
1 parent 00b09df commit c8375b5e3768bf1255ac03d90fb29c61d056d238 @dbroeglin dbroeglin committed Mar 21, 2012
Showing with 21 additions and 1 deletion.
  1. +1 −1 lib/draper/base.rb
  2. +20 −0 spec/draper/base_spec.rb
View
@@ -230,7 +230,7 @@ def method_missing(method, *args, &block)
end
def self.method_missing(method, *args, &block)
- if method.to_s.match(/^find_by.*/)
+ if method.to_s.match(/^find_((all_|last_)?by_|or_(initialize|create)_by_).*/)
self.decorate(model_class.send(method, *args, &block))
else
model_class.send(method, *args, &block)
@@ -264,6 +264,26 @@ class CustomDecorator < Draper::Base
ProductDecorator.find_by_name_and_size("apples", "large")
end
+ it "runs find_all_by_(x) finders" do
+ Product.should_receive(:find_all_by_name_and_size)
+ ProductDecorator.find_all_by_name_and_size("apples", "large")
+ end
+
+ it "runs find_last_by_(x) finders" do
+ Product.should_receive(:find_last_by_name_and_size)
+ ProductDecorator.find_last_by_name_and_size("apples", "large")
+ end
+
+ it "runs find_or_initialize_by_(x) finders" do
+ Product.should_receive(:find_or_initialize_by_name_and_size)
+ ProductDecorator.find_or_initialize_by_name_and_size("apples", "large")
+ end
+
+ it "runs find_or_create_by_(x) finders" do
+ Product.should_receive(:find_or_create_by_name_and_size)
+ ProductDecorator.find_or_create_by_name_and_size("apples", "large")
+ end
+
it "accepts an options hash" do
Product.should_receive(:find_by_name_and_size).with("apples", "large", {:role => :admin})
ProductDecorator.find_by_name_and_size("apples", "large", {:role => :admin})

0 comments on commit c8375b5

Please sign in to comment.