1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Merge pull request #144 from simonc/polymorphic-associations-decoration

Adding a :polymorphic option to decorates_association
This commit is contained in:
Steve Klabnik 2012-03-13 12:05:27 -07:00
commit d327038e1e
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, options)

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