Skip to content

Commit

Permalink
Update render_coverage plugin to handle case where both :scope_class …
Browse files Browse the repository at this point in the history
…and fixed locals are used

In this case, Tilt will compile the template during initialization,
before the call to compiled_path=.  Use the newly supported
:compiled_path option in Tilt to handle this case.
  • Loading branch information
jeremyevans committed Jan 7, 2025
1 parent 0174fc8 commit b1aaa04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
25 changes: 22 additions & 3 deletions lib/roda/plugins/render_coverage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,36 @@ module ClassMethods
# Set a compiled path on the created template, if the path for
# the template is in one of the allowed_views.
def create_template(opts, template_opts)
template = super
return template if opts[:template_block]
return super if opts[:template_block]

path = File.expand_path(opts[:path])
compiled_path = nil
(self.opts[:render_coverage_strip_paths] || render_opts[:allowed_paths]).each do |dir|
if path.start_with?(dir + '/')
template.compiled_path = File.join(self.opts[:render_coverage_dir], path[dir.length+1, 10000000].gsub('/', '-'))
compiled_path = File.join(self.opts[:render_coverage_dir], path[dir.length+1, 10000000].gsub('/', '-'))
break
end
end

# For Tilt 2.6+, when using :scope_class and fixed locals, must provide
# compiled path as option, since compilation happens during initalization
# in that case. This option should be ignored if the template does not
# support it, but some template class may break if the option is not
# handled, so for compatibility, only set the method if Tilt::Template
# will handle it.
if compiled_path && Tilt::Template.method_defined?(:fixed_locals?)
template_opts = template_opts.dup
template_opts[:compiled_path] = compiled_path
compiled_path = nil
end

template = super

# Set compiled path for template when using older tilt versions.
# :nocov:
template.compiled_path = compiled_path if compiled_path
# :nocov:

template
end
end
Expand Down
7 changes: 6 additions & 1 deletion spec/plugin/render_coverage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

define_method(:setup_app) do |render_opts={}, render_coverage_opts={}, prefix=''|
app(:bare) do
if render_opts == :scope_class_and_fixed_locals
render_opts = {:template_opts=>{:scope_class=>self, :default_fixed_locals=>'()'}}
end

plugin :render, {:views=>"./spec/views/about", :check_paths=>true, :layout=>false}.merge!(render_opts)
plugin :render_coverage, {:dir=>coverage_dir}.merge!(render_coverage_opts)
plugin :render_coverage
Expand All @@ -25,7 +29,7 @@
end

r.get "path" do
render(:path=>"./spec/views/about.erb", :locals=>{:title => "About Roda"})
render(:path=>"./spec/views/about.erb", :locals=>{:title => "About Roda"}, :template_opts=>{:fixed_locals=>'(title:)'})
end

r.get "not-exist" do
Expand Down Expand Up @@ -58,6 +62,7 @@

{
[] => "should store files in specified directory",
[:scope_class_and_fixed_locals] => "should handle using of :scope_class and fixed_locals",
[{:cache=>false}] => "should handle cache: false render plugin option",
[{:views=>'./spec/views', :allowed_paths=>%w'./spec/views/about'}, {}, 'about/'] => "should strip paths based on render plugin :allowed_paths option",
[{:views=>'./spec/views'}, {:strip_paths=>%w'./spec/views/about'}, 'about/'] => "should strip paths based on render_coverage plugin :strip_paths option"
Expand Down

0 comments on commit b1aaa04

Please sign in to comment.