diff --git a/lib/draper/base.rb b/lib/draper/base.rb index c005da2..001d67f 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -57,12 +57,17 @@ module Draper # the assocation to be decorated when it is retrieved. # # @param [Symbol] name of association to decorate, like `:products` - def self.decorates_association(association_symbol) + # @option opts [Class] :with The decorator to decorate the association with + def self.decorates_association(association_symbol, options = {}) define_method(association_symbol) do orig_association = model.send(association_symbol) return orig_association if orig_association.nil? - reflection = model.class.reflect_on_association(association_symbol) - "#{reflection.klass}Decorator".constantize.decorate(orig_association) + if options[:with] + options[:with].decorate(orig_association) + else + reflection = model.class.reflect_on_association(association_symbol) + "#{reflection.klass}Decorator".constantize.decorate(orig_association) + end end end diff --git a/spec/draper/base_spec.rb b/spec/draper/base_spec.rb index 64132af..7888e34 100644 --- a/spec/draper/base_spec.rb +++ b/spec/draper/base_spec.rb @@ -93,6 +93,13 @@ describe Draper::Base do subject.previous_version.should be_nil end end + + context "with a specific decorator specified" do + before(:each){ subject.class_eval{ decorates_association :previous_version, :with => SpecificProductDecorator } } + it "causes the association to be decorated with the specified association" do + subject.previous_version.should be_instance_of(SpecificProductDecorator) + end + end end context('.decorates_associations') do diff --git a/spec/support/samples/specific_product_decorator.rb b/spec/support/samples/specific_product_decorator.rb new file mode 100644 index 0000000..3136ba1 --- /dev/null +++ b/spec/support/samples/specific_product_decorator.rb @@ -0,0 +1,2 @@ +class SpecificProductDecorator < ProductDecorator +end \ No newline at end of file