Class: Mutton::HandlebarsTemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mutton/handlebars_template_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ HandlebarsTemplateHandler

Returns a new instance of HandlebarsTemplateHandler.



10
11
12
13
# File 'lib/mutton/handlebars_template_handler.rb', line 10

def initialize(view)
  # not used in this version
  @view = view
end

Class Method Details

.call(template) ⇒ Object

template handler contract this is called from rendering stack



6
7
8
# File 'lib/mutton/handlebars_template_handler.rb', line 6

def self.call(template)
  "Mutton::HandlebarsTemplateHandler.new(self).render_handlebars(#{template.virtual_path.inspect}, Rails.application.assets, assigns)"
end

Instance Method Details

#custom_javascript(asset_environment, helper_path, _options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/mutton/handlebars_template_handler.rb', line 45

def custom_javascript(asset_environment, helper_path, _options = {})
  # get everything from the helper directory
  javascript_content = ''
  javascript_helpers = asset_environment.each_file.select { |x| x.starts_with?(helper_path) }
  javascript_helpers.each do |helper|
    javascript_content << File.new(helper, 'r').read
  end
  javascript_content
end

#render_handlebars(virtual_path, asset_environment, assigns = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mutton/handlebars_template_handler.rb', line 15

def render_handlebars(virtual_path, asset_environment, assigns = {})
  timing = {}
  ActiveSupport::Notifications.instrument 'render_template.Mutton', timing do
    start_time = Time.zone.now
    # get every hbs file name and pull from sprockets
    handlebar_content = template_content(asset_environment)
    end_content_time= Time.zone.now
    # get custom javascript from the helper directory
    javascript_helpers = custom_javascript(Rails.application.assets, Mutton.helper_path)
    start_compile_time = Time.zone.now
    # compile it
    result = HandlebarsCompiler.process_handlebars(handlebar_content, virtual_path, Mutton.template_namespace, assigns.as_json, javascript_helpers)
    end_compile_time = Time.zone.now
    content_retrieval_time = ((end_content_time - start_time) * 1000).round(0)
    compile_time = ((end_compile_time - start_compile_time) * 1000).round(0)
    timing[:compile_time] = compile_time
    timing[:content_retrieval_time] = content_retrieval_time
    result.html_safe
  end
end

#template_content(asset_environment) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/mutton/handlebars_template_handler.rb', line 36

def template_content(asset_environment)
  handlebar_content = ''
  asset_environment.each_file.select { |x| x.ends_with?('.hbs') || x.ends_with?('.handlebars') }.each do |asset|
    content = Rails.application.assets[asset]
    handlebar_content << content.source
  end
  handlebar_content
end