class DryHamlHandlebars::Handler
Public Class Methods
call(template, options = {})
click to toggle source
Calls superclass method
# File lib/dry_haml_handlebars/handler.rb, line 56 def call(template, options = {}) view_match = template.identifier.match(/^#{Rails.root.join('app', 'views')}[\/](?<view_path>[\/\w]+)[\/](?<view_name>\w+).html/) relative_view_path = view_match[:view_path] view_name = view_match[:view_name] view_type = get_view_type(template, relative_view_path, view_name) env = Rails.env.to_sym out = [] #when in dev mode we can set this variable to true on each request (in the app controller) #and we scan for all changed partials (whether needed for the current view or not) #this is intended to ensure that all changes are picked up ready for deployment #even if the dev forgets to run the view that requires a changed partial #the rule is, render ANY page, and ALL partials should be re-compiled (if needed) #while for changed templates, you MUST run the view that you have changed if DryHamlHandlebars.on_next_request and DryHamlHandlebars.on_next_request.values.any? DryHamlHandlebars.on_next_request.keys.each do |method| DryHamlHandlebars.on_next_request[method] = false case method when :compile_all_partials out += compile_and_load_all_partials else DryHamlHandlebars.send method end end end if [:layout, :ignored_partial].include? view_type out << super(template) return out.join("\n") end partial_name = [relative_view_path.gsub('/', '_'), view_name[1..-1]].join('_') rabl_path, template_path, compiled_template_path = generate_file_names(relative_view_path, view_name) # Rails.logger.info <<-INFO_STRING # template_path = #{template_path} # options[:force_handlebars_compile] = #{options[:force_handlebars_compile]} # File.exist?(compiled_template_path) = #{File.exist?(compiled_template_path)} # env = #{env} # INFO_STRING if options[:force_handlebars_compile] or !File.exist?(compiled_template_path) or ( [:development, :test].include?(env) and ( File.mtime(compiled_template_path) < File.mtime(template.identifier) ) ) source = template.source source = DryHamlHandlebars.dedent_hbs(source) template.instance_variable_set "@source", source rendered_haml = <<-RUBY rendered_haml = eval(%q( #{super(template)} )).html_safe RUBY else rendered_haml = nil end runner = Runner.new( template, rendered_haml, view_type, view_name, partial_name, relative_view_path, rabl_path, template_path, compiled_template_path ) out << runner.run out.join("\n") end
compile_and_load_all_partials()
click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 127 def compile_and_load_all_partials partials = Dir.glob(Rails.root.join('app', 'views', '**', '_*.html.haml')) out = [] partials.each do |fname| File.open(fname) do |file| source = file.read next unless source.starts_with?('-#handlebars_partial') source = DryHamlHandlebars.dedent_hbs(source) template = ActionView::Template.new(source, fname, nil, {:locals => ["__handlebars_partial"]}) out << call(template, :force_handlebars_compile => true) end end out end
generate_file_names(relative_view_path, view_name)
click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 162 def generate_file_names(relative_view_path, view_name) template_partial_path = Rails.root.join( *%w(app assets templates) << "#{relative_view_path}" ) compiled_template_partial_path = Rails.root.join( *%w(app assets compiled_templates) << "#{relative_view_path}" ) rabl_path = Rails.root.join( 'app', 'views', relative_view_path, "#{view_name}.rabl" ) template_path = File.join( template_partial_path, "#{view_name}.hbs" ) compiled_template_path = File.join( compiled_template_partial_path, "#{view_name}.js" ) FileUtils.mkdir_p template_partial_path unless File.directory? template_partial_path FileUtils.mkdir_p compiled_template_partial_path unless File.directory? compiled_template_partial_path return rabl_path, template_path, compiled_template_path end
get_view_type(template, relative_view_path, view_name)
click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 142 def get_view_type(template, relative_view_path, view_name) #we have 4 types of view; # 1) layout - always handled by haml, no hbs/js versions are generated # 2) template - rendered as handlebars, we expect there to be html.haml AND .rabl for the JSON # 3) partial - pulled into view by handlebars syntax {{>name}} # 4) ignored_partial - a regular partial, it will be rendered by Haml, with no handlebars-related processing if relative_view_path == 'layouts' :layout elsif template.locals.include?("__handlebars_partial") :partial elsif view_name.starts_with? "_" :ignored_partial else :template end end