Module: Extensions::RenderCollectionWithPrefixSuffix::ActionView::PartialRenderer::PrependMethods

Defined in:
lib/extensions/render_collection_with_prefix_suffix/action_view/partial_renderer.rb

Instance Method Summary collapse

Instance Method Details

#partial_pathObject

Adds support for the +prefix+ and +suffix+ options to partial:.

This allows for a prefix or suffix to be specified when rendering a polymorphic collection, so that different partials can be used for different contexts.

Examples:

Rendering a collection of objects

@collection = [event, assessment]
render partial: @collection, prefix: 'lesson_plan'
# Renders event/_lesson_plan_event.html
# Renders assessment/_lesson_plan_assessment.html

14
15
16
17
18
19
20
21
22
# File 'lib/extensions/render_collection_with_prefix_suffix/action_view/partial_renderer.rb', line 14

def partial_path(*)
  result = super
  return result unless @options.key?(:prefix) || @options.key?(:suffix)

  dirname, basename = File.split(result)
  basename = basename.prepend("#{@options[:prefix]}_") if @options.key?(:prefix)
  basename = basename.concat("_#{@options[:suffix]}") if @options.key?(:suffix)
  File.join(dirname, basename)
end