Add with option to decorates_association to allow the decoration to be decorated by a specific association
This commit is contained in:
parent
7b6cd2634d
commit
22461fd7c1
|
@ -57,12 +57,17 @@ module Draper
|
||||||
# the assocation to be decorated when it is retrieved.
|
# the assocation to be decorated when it is retrieved.
|
||||||
#
|
#
|
||||||
# @param [Symbol] name of association to decorate, like `:products`
|
# @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
|
define_method(association_symbol) do
|
||||||
orig_association = model.send(association_symbol)
|
orig_association = model.send(association_symbol)
|
||||||
return orig_association if orig_association.nil?
|
return orig_association if orig_association.nil?
|
||||||
reflection = model.class.reflect_on_association(association_symbol)
|
if options[:with]
|
||||||
"#{reflection.klass}Decorator".constantize.decorate(orig_association)
|
options[:with].decorate(orig_association)
|
||||||
|
else
|
||||||
|
reflection = model.class.reflect_on_association(association_symbol)
|
||||||
|
"#{reflection.klass}Decorator".constantize.decorate(orig_association)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -93,6 +93,13 @@ describe Draper::Base do
|
||||||
subject.previous_version.should be_nil
|
subject.previous_version.should be_nil
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
context('.decorates_associations') do
|
context('.decorates_associations') do
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
class SpecificProductDecorator < ProductDecorator
|
||||||
|
end
|
Loading…
Reference in New Issue