Proxy all dynamic finders

This commit is contained in:
Dominique Broeglin 2012-03-21 10:23:53 +01:00
parent 00b09dfbdd
commit c8375b5e37
2 changed files with 21 additions and 1 deletions

View File

@ -230,7 +230,7 @@ module Draper
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)

View File

@ -264,6 +264,26 @@ describe Draper::Base do
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})