Skip to content

Instantly share code, notes, and snippets.

@jbennett
Created April 7, 2023 18:45
Show Gist options
  • Save jbennett/35198e3e320c0b188999a9097155300f to your computer and use it in GitHub Desktop.
Save jbennett/35198e3e320c0b188999a9097155300f to your computer and use it in GitHub Desktop.
Polymorphic Render
module ApplicationHelper
def polymorphic_render(resource, suffix = nil, options = {})
if resource.nil?
return
elsif resource.is_a? Array
capture do
resource.each { |res| concat polymorphic_render(res, suffix, options) }
end
elsif resource.respond_to? :to_partial_path
path = [resource.to_partial_path, *Array(suffix)].compact_blank.join('_')
name = File.basename(resource.to_partial_path)
render options.merge(partial: path, object: resource, as: name)
else
# maybe Rails can figure it out
render resource, options
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment