Over a million developers have joined DZone.

Rails Task To Find Code Typos In Rhtml Templates

·

namespace :typos do 
  task :rhtml do
    require 'erb'
    require 'active_support'
    require 'action_view'

    module TempTemplates; end
    Dir["./app/views/**/*.rhtml"].each do |fname|
      local_source = ERB.new(IO.read(fname), nil, '-').src
      template_source = "def _tmpl\n#{local_source}\nend"
      begin
        TempTemplates.module_eval(template_source, fname, 0 )
      rescue Object => e
        t = ActionView::TemplateError.new("./app/views/", fname, {}, template_source, e)
        puts "TemplateError (#{t.message}) on line ##{t.line_number} of #{t.file_name}:\n"+t.source_extract + "\n------\n"
      end
    end
  end
end

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}