Support STI in CollectionDecorator subclasses
This commit is contained in:
parent
4c6a182b1d
commit
44f4c18b5e
|
@ -4,6 +4,10 @@ module Draper
|
||||||
include Draper::ViewHelpers
|
include Draper::ViewHelpers
|
||||||
extend Draper::Delegation
|
extend Draper::Delegation
|
||||||
|
|
||||||
|
# @return [Class] the decorator class used to decorate each item, as set by
|
||||||
|
# {#initialize}.
|
||||||
|
attr_reader :decorator_class
|
||||||
|
|
||||||
# @return [Hash] extra data to be used in user-defined methods, and passed
|
# @return [Hash] extra data to be used in user-defined methods, and passed
|
||||||
# to each item's decorator.
|
# to each item's decorator.
|
||||||
attr_accessor :context
|
attr_accessor :context
|
||||||
|
@ -49,13 +53,7 @@ module Draper
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
klass = begin
|
"#<#{self.class.name} of #{decorator_class || "inferred decorators"} for #{source.inspect}>"
|
||||||
decorator_class
|
|
||||||
rescue Draper::UninferrableDecoratorError
|
|
||||||
"inferred decorators"
|
|
||||||
end
|
|
||||||
|
|
||||||
"#<#{self.class.name} of #{klass} for #{source.inspect}>"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def context=(value)
|
def context=(value)
|
||||||
|
@ -63,13 +61,6 @@ module Draper
|
||||||
each {|item| item.context = value } if @decorated_collection
|
each {|item| item.context = value } if @decorated_collection
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Class] the decorator class used to decorate each item, as set by
|
|
||||||
# {#initialize} or as inferred from the collection decorator class (e.g.
|
|
||||||
# `ProductsDecorator` maps to `ProductDecorator`).
|
|
||||||
def decorator_class
|
|
||||||
@decorator_class ||= self.class.inferred_decorator_class
|
|
||||||
end
|
|
||||||
|
|
||||||
# @return [true]
|
# @return [true]
|
||||||
def decorated?
|
def decorated?
|
||||||
true
|
true
|
||||||
|
@ -87,24 +78,10 @@ module Draper
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.inferred_decorator_class
|
|
||||||
decorator_name = "#{name.chomp("Decorator").singularize}Decorator"
|
|
||||||
decorator_uninferrable if decorator_name == name
|
|
||||||
|
|
||||||
decorator_name.constantize
|
|
||||||
|
|
||||||
rescue NameError
|
|
||||||
decorator_uninferrable
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.decorator_uninferrable
|
|
||||||
raise Draper::UninferrableDecoratorError.new(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
def item_decorator
|
def item_decorator
|
||||||
@item_decorator ||= begin
|
if decorator_class
|
||||||
decorator_class.method(:decorate)
|
decorator_class.method(:decorate)
|
||||||
rescue Draper::UninferrableDecoratorError
|
else
|
||||||
->(item, options) { item.decorate(options) }
|
->(item, options) { item.decorate(options) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue