Skip to content

Commit

Permalink
Adding a :polymorphic option to decorates_association
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon COURTOIS committed Mar 8, 2012
1 parent 279ea02 commit 108d4ba
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/draper/base.rb
Expand Up @@ -67,6 +67,8 @@ def self.decorates_association(association_symbol, options = {})
return orig_association if orig_association.nil?
if options[:with]
options[:with].decorate(orig_association)
elsif options[:polymorphic]
"#{orig_association.class}Decorator".constantize.decorate(orig_association)
else
reflection = model.class.reflect_on_association(association_symbol)
"#{reflection.klass}Decorator".constantize.decorate(orig_association)
Expand Down
7 changes: 7 additions & 0 deletions spec/draper/base_spec.rb
Expand Up @@ -138,6 +138,13 @@ class CustomDecorator < Draper::Base
subject.previous_version.should be_instance_of(SpecificProductDecorator)
end
end

context "for a polymorphic association" do
before(:each){ subject.class_eval{ decorates_association :thing, :polymorphic => true } }
it "causes the association to be decorated with the right decorator" do
subject.thing.should be_instance_of(SomeThingDecorator)
end
end
end

context('.decorates_associations') do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -15,5 +15,7 @@
require './spec/support/samples/product.rb'
require './spec/support/samples/product_decorator.rb'
require './spec/support/samples/specific_product_decorator.rb'
require './spec/support/samples/some_thing.rb'
require './spec/support/samples/some_thing_decorator.rb'
require './spec/support/samples/widget.rb'
require './spec/support/samples/widget_decorator.rb'
4 changes: 4 additions & 0 deletions spec/support/samples/product.rb
Expand Up @@ -64,4 +64,8 @@ def similar_products
def previous_version
Product.new
end

def thing
SomeThing.new
end
end
2 changes: 2 additions & 0 deletions spec/support/samples/some_thing.rb
@@ -0,0 +1,2 @@
class SomeThing < Product
end
3 changes: 3 additions & 0 deletions spec/support/samples/some_thing_decorator.rb
@@ -0,0 +1,3 @@
class SomeThingDecorator < Draper::Base
decorates :some_thing
end

0 comments on commit 108d4ba

Please sign in to comment.