Class: Mutton::HandlebarsTemplateHandler
- Inherits:
-
Object
- Object
- Mutton::HandlebarsTemplateHandler
- Defined in:
- lib/mutton/handlebars_template_handler.rb
Class Method Summary collapse
-
.call(template) ⇒ Object
template handler contract this is called from rendering stack.
Instance Method Summary collapse
- #custom_javascript(asset_environment, helper_path, _options = {}) ⇒ Object
-
#initialize(view) ⇒ HandlebarsTemplateHandler
constructor
A new instance of HandlebarsTemplateHandler.
- #render_handlebars(virtual_path, asset_environment, assigns = {}) ⇒ Object
- #template_content(asset_environment) ⇒ Object
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, = {}) # 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 (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 = 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.(, 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) = '' asset_environment.each_file.select { |x| x.ends_with?('.hbs') || x.ends_with?('.handlebars') }.each do |asset| content = Rails.application.assets[asset] << content.source end end |