Created
July 26, 2024 07:23
-
-
Save Largo/bb9d01c6ca0095aafdc7dde4ebd57119 to your computer and use it in GitHub Desktop.
render_with_debug
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
# config/initializers/render_with_debug.rb | |
if Rails.env.development? | |
ActionView::Base.class_eval do | |
alias_method :original_render, :render | |
def render(options = {}, locals = {}, &block) | |
if self.respond_to?(:render_with_debug) | |
self.render_with_debug(options, locals, &block) | |
else | |
original_render(options, locals, &block) | |
end | |
end | |
def render_with_debug(options = {}, locals = {}, &block) | |
if Rails.env.development? | |
partial_name = options.is_a?(Hash) ? options[:partial] : options | |
debug_start_comment = "<!-- Start of #{partial_name} partial -->\n" | |
debug_end_comment = "\n<!-- End of #{partial_name} partial -->" | |
if block_given? | |
raw(debug_start_comment) + original_render(options, locals, &block) + raw(debug_end_comment) | |
else | |
raw(debug_start_comment) + original_render(options, locals) + raw(debug_end_comment) | |
end | |
else | |
render(options, locals, &block) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment