Created
April 7, 2023 18:45
-
-
Save jbennett/35198e3e320c0b188999a9097155300f to your computer and use it in GitHub Desktop.
Polymorphic Render
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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