Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a :polymorphic option to decorates_association

  • Loading branch information...
commit 108d4baaafb5990872c9eea979a2ce1f40bb0f31 1 parent 279ea02
Simon COURTOIS authored
View
2  lib/draper/base.rb
@@ -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)
View
7 spec/draper/base_spec.rb
@@ -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
View
2  spec/spec_helper.rb
@@ -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'
View
4 spec/support/samples/product.rb
@@ -64,4 +64,8 @@ def similar_products
def previous_version
Product.new
end
+
+ def thing
+ SomeThing.new
+ end
end
View
2  spec/support/samples/some_thing.rb
@@ -0,0 +1,2 @@
+class SomeThing < Product
+end
View
3  spec/support/samples/some_thing_decorator.rb
@@ -0,0 +1,3 @@
+class SomeThingDecorator < Draper::Base
+ decorates :some_thing
+end
Please sign in to comment.
Something went wrong with that request. Please try again.