Adding a :polymorphic option to decorates_association

This commit is contained in:
Simon COURTOIS 2012-03-08 14:21:26 +01:00
parent 279ea0202d
commit 108d4baaaf
6 changed files with 20 additions and 0 deletions

View File

@ -67,6 +67,8 @@ module Draper
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 File

@ -138,6 +138,13 @@ describe Draper::Base do
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 File

@ -15,5 +15,7 @@ require './spec/support/samples/namespaced_product_decorator.rb'
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 File

@ -64,4 +64,8 @@ class Product < ActiveRecord::Base
def previous_version
Product.new
end
def thing
SomeThing.new
end
end

View File

@ -0,0 +1,2 @@
class SomeThing < Product
end

View File

@ -0,0 +1,3 @@
class SomeThingDecorator < Draper::Base
decorates :some_thing
end