I don't know whether there is a gem for this, but you can overwrite a class designed for rendering the output of the all the views and partials located in:
.bundle/ruby/ruby-2.3.1/gems/action-view-4.2.5.1/lib/action_view/template.rb
(replace your version number)
There is a method called def compile(mod)
owerwrite that method to add your custom strings that you want to present in the view. For your case you could do something like this:
def compile(mod) #:nodoc:
encode!
method_name = self.method_name
code = @handler.call(self)
# This is the code I have added
code.insert(63, "@output_buffer.safe_append='\n<!--#{self.inspect} start -->\n'.freeze\;")
code.insert(code.size-19, "@output_buffer.safe_append='\n<!--#{self.inspect} end -->\n'.freeze\;")
# Make sure that the resulting String to be eval'd is in the
# encoding of the code
source = <<-end_src
def #{method_name}(local_assigns, output_buffer)
_old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code}
ensure
@virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer
end
end_src
# Make sure the source is in the encoding of the returned code
source.force_encoding(code.encoding)
# In case we get back a String from a handler that is not in
# BINARY or the default_internal, encode it to the default_internal
source.encode!
# Now, validate that the source we got back from the template
# handler is valid in the default_internal. This is for handlers
# that handle encoding but screw up
unless source.valid_encoding?
raise WrongEncodingError.new(@source, Encoding.default_internal)
end
mod.module_eval(source, identifier, 0)
ObjectSpace.define_finalizer(self, Finalizer[method_name, mod])
end
The code I have added is:
code.insert(63, "@output_buffer.safe_append='\n<!--#{self.inspect} start -->\n'.freeze\;")
code.insert(code.size-19, "@output_buffer.safe_append='\n<!--#{self.inspect} end -->\n'.freeze\;")
And it will output in the views all the partials and all the views as they are being rendered.
The result would be like:
<!--app/views/profiles/_header.html.erb start -->
Your text on the page
<!--app/views/profiles/_header.html.erb end -->
If you would like to extract that into a separate class, create a new initializes in config/initializers/
directory of your Rails project, called, for example render_override.rb
and paste code something like this:
ActionView::Template.class_eval do
# @override
def compile(mod)
encode!
method_name = self.method_name
code = @handler.call(self)
# This is the code I have added
code.insert(63, "@output_buffer.safe_append='\n<!--#{self.inspect} start -->\n'.freeze\;")
code.insert(code.size-19, "@output_buffer.safe_append='\n<!--#{self.inspect} end -->\n'.freeze\;")
# Make sure that the resulting String to be eval'd is in the
# encoding of the code
source = <<-end_src
def #{method_name}(local_assigns, output_buffer)
_old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code}
ensure
@virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer
end
end_src
# Make sure the source is in the encoding of the returned code
source.force_encoding(code.encoding)
# In case we get back a String from a handler that is not in
# BINARY or the default_internal, encode it to the default_internal
source.encode!
# Now, validate that the source we got back from the template
# handler is valid in the default_internal. This is for handlers
# that handle encoding but screw up
unless source.valid_encoding?
raise WrongEncodingError.new(@source, Encoding.default_internal)
end
mod.module_eval(source, identifier, 0)
ObjectSpace.define_finalizer(self, Finalizer[method_name, mod])
end
end
Restart the server so the changes can be picked up, and your views will now be rendered with those new settings.
You might want to surround your eval class with a check RAILS_ENV='dev'
or something similar so it could be run just in development, and you are ready to go.