Permalink
Browse files

add asset helper

  • Loading branch information...
1 parent 9a8b249 commit 4278be2eb235e5e9d33d4350c70a74e57921760e @shawndrost committed Jan 31, 2012
Showing with 39 additions and 1 deletion.
  1. +3 −0 lib/handlebars-rails.rb
  2. +35 −0 lib/handlebars-rails/asset_helper.rb
  3. +1 −1 lib/handlebars-rails/version.rb
View
3 lib/handlebars-rails.rb
@@ -1,7 +1,10 @@
require 'handlebars-rails/version'
require 'handlebars-rails/v8'
+require 'handlebars-rails/asset_helper'
require "active_support"
+ActionView::Base.send :include, Handlebars::AssetHelper
+
module Handlebars
class TemplateHandler
View
35 lib/handlebars-rails/asset_helper.rb
@@ -0,0 +1,35 @@
+module Handlebars
+ module AssetHelper
+ def template_include_tag(*sources)
+ sources.collect do |source|
+ template = File.open(resolve_partial_path(source), "rb")
+ content_tag :script, template.read.html_safe, :type => "text/handlebars", :id => "#{template_name(source).parameterize.dasherize}-template"
+ end.join("\n").html_safe
+ end
+
+ private
+
+ def is_absolute_path?(source)
+ source.to_s =~ /^\//
+ end
+
+ def template_name(source)
+ if is_absolute_path?(source)
+ source.to_s.split('/').last
+ else
+ source
+ end
+ end
+
+ def resolve_partial_path(source)
+ if is_absolute_path?(source)
+ segments = source.to_s.split('/')
+ partial_name = "_#{segments.pop}.html.hbs"
+ segments << partial_name
+ Rails.root.join('app/views', *segments)
+ else
+ Rails.root.join('app/views', controller_name, "_#{source}.html.hbs")
+ end
+ end
+ end
+end
View
2 lib/handlebars-rails/version.rb
@@ -1,3 +1,3 @@
module Handlebars
- VERSION = "0.1.0"
+ VERSION = "0.1.2"
end

0 comments on commit 4278be2

Please sign in to comment.