backbone_handlebars 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in backbone_handlebars.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Blake Williams
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # BackboneHandlebars
2
+
3
+ A gem that vendors the latest Backbone, Underscore, and Handlebars libraries and compiles/precompiles Handlebars templates.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'backbone_handlebars'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ rails generate backbone_handlebars:install
16
+
17
+ This will generate the applications structure in the app/assets/javascripts directory and add the precompile option to your production environment.
18
+
19
+ ## Options
20
+
21
+ ```ruby
22
+ config.handlebars.precompile
23
+ ```
24
+
25
+ Enables precompilation and will serve only the Handlebars runtime instead of the full library.
26
+
27
+ ```
28
+ config.handlebars.path
29
+ ```
30
+
31
+ Changes the path of the Handlebars library that compiles/precompiles your templates
32
+
33
+ If you want to use your own Handlebars or Handlebars runtime you have to place them in `vendor/assets/handlebars/development` and `vendor/assets/handlebars/production` respectively.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'backbone_handlebars/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "backbone_handlebars"
8
+ gem.version = BackboneHandlebars::VERSION
9
+ gem.authors = ["Blake Williams"]
10
+ gem.email = ["blake@blakewilliams.me"]
11
+ gem.summary = %q{Backbone and Handlebars for Rails 3.1+}
12
+ gem.homepage = "http://github.com/BlakeWilliams/backbone_handlebars"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency "execjs"
20
+ gem.add_dependency "sprockets"
21
+ gem.add_dependency "tilt"
22
+ end
@@ -0,0 +1,26 @@
1
+ require 'backbone_handlebars/template'
2
+
3
+ module BackboneHandlebars
4
+ class Engine < Rails::Engine
5
+ config.handlebars = ActiveSupport::OrderedOptions.new
6
+
7
+ config.handlebars.path = File.expand_path("../../../vendor/assets/handlebars/development/handlebars.js", __FILE__)
8
+ config.handlebars.templates_root "templates"
9
+
10
+ initializer "backbone_handlebars.setup", after: 'sprockets.environment', group: :all do |app|
11
+ version = "production" if app.config.handlebars.precompile == true
12
+ version ||= "development"
13
+
14
+ path = File.expand_path("../../../vendor/assets/handlebars/#{version}", __FILE__)
15
+ app.config.assets.paths.push(path.to_s)
16
+
17
+ path = app.root.join("vendor/assets/handlebars/#{version}")
18
+ app.config.assets.paths.unshift(path.to_s) if path.exist?
19
+
20
+ next unless app.assets
21
+ app.assets.register_engine '.handlebars', BackboneHandlebars::Template
22
+ app.assets.register_engine '.hbs', BackboneHandlebars::Template
23
+ app.assets.register_engine '.hjs', BackboneHandlebars::Template
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,58 @@
1
+ require 'sprockets'
2
+ require 'sprockets/engines'
3
+ require 'execjs'
4
+
5
+ module BackboneHandlebars
6
+ class Template < Tilt::Template
7
+
8
+ def self.handlebars
9
+ @@handlebars ||= File.new(::Rails.configuration.handlebars.path).read
10
+ end
11
+
12
+ def self.default_mime_type
13
+ 'application/javascript'
14
+ end
15
+
16
+ def prepare; end
17
+
18
+ def evaluate(scope, locals, &block)
19
+ path = scope.logical_path.gsub(/^templates\//i, '')
20
+
21
+ if ::Rails.configuration.handlebars.precompile == true
22
+ template = precompile_handlebars(path, data)
23
+ else
24
+ template = compile_handlebars(path, data)
25
+ end
26
+ end
27
+
28
+ private
29
+ def compile_handlebars(path, string)
30
+ <<-TARGET.gsub /^\s{/, ''\
31
+ (function() {
32
+ Handlebars.templates || (Handlebars.templates = {})
33
+ Handlebars.templates['#{path}'] = Handlebars.compile(#{indent(string).inspect});
34
+ }).call(this);
35
+ TARGET
36
+ end
37
+
38
+ def precompile_handlebars(path, string)
39
+ context = ExecJS.compile(BackboneHandlebars::Template.handlebars)
40
+ template = context.call "Handlebars.precompile", strip(string)
41
+ <<-TARGET.gsub /^\s{/, ''\
42
+ (function() {
43
+ Handlebars.templates || (Handlebars.templates = {})
44
+ Handlebars.templates['#{path}'] = Handlebars.template(#{template});
45
+ }).call(this);
46
+ TARGET
47
+ end
48
+
49
+ def indent(string)
50
+ string.gsub(/$(.)/m, "\\1 ").strip
51
+ end
52
+
53
+ def strip(string)
54
+ string.gsub(/\n/, '')
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module BackboneHandlebars
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "backbone_handlebars/version"
2
+ require "backbone_handlebars/engine"
@@ -0,0 +1,47 @@
1
+ module BackboneHandlebars
2
+ module Generators
3
+ module Helpers
4
+
5
+ def assets_path
6
+ File.join('app', 'assets')
7
+ end
8
+
9
+ def javascripts_path
10
+ File.join(assets_path, 'javascripts')
11
+ end
12
+
13
+ def models_path
14
+ File.join(javascripts_path, 'models')
15
+ end
16
+
17
+ def collections_path
18
+ File.join(javascripts_path, 'collections')
19
+ end
20
+
21
+ def helpers_path
22
+ File.join(javascripts_path, 'helpers')
23
+ end
24
+
25
+ def routers_path
26
+ File.join(javascripts_path, 'routers')
27
+ end
28
+
29
+ def views_path
30
+ File.join(javascripts_path, 'views')
31
+ end
32
+
33
+ def templates_path
34
+ File.join(javascripts_path, 'templates')
35
+ end
36
+
37
+ def production_config_path
38
+ File.join('config', 'environments', 'production.rb')
39
+ end
40
+
41
+ def app_name
42
+ Rails.application.class.name.split('::').first.camelize
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,68 @@
1
+ require 'generators/backbone_handlebars/helpers'
2
+
3
+ module BackboneHandlebars
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include BackboneHandlebars::Generators::Helpers
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ desc "Generates Backbone skeleton directory structure"
11
+
12
+ class_option :manifest,
13
+ type: :string,
14
+ aliases: "-m",
15
+ default: "application.js",
16
+ desc: "Javascript manifest file to modify"
17
+
18
+ def create_dir_layout
19
+ empty_directory collections_path
20
+ empty_directory helpers_path
21
+ empty_directory models_path
22
+ empty_directory routers_path
23
+ empty_directory templates_path
24
+ empty_directory views_path
25
+ end
26
+
27
+ def create_app_file
28
+ template "app.coffee", "#{javascripts_path}/#{app_name.downcase}.coffee"
29
+ end
30
+
31
+ def inject_files
32
+ manifest = File.join(javascripts_path, options.manifest)
33
+
34
+ libs = %w[underscore backbone handlebars]
35
+ paths = %w(./helpers ./templates ./models ./collections ./views ./routers)
36
+
37
+ out = []
38
+ out << libs.map { |lib| "//= require #{lib}" }
39
+ out << "//= require #{app_name.downcase}"
40
+ out << paths.map { |path| "//= require_tree #{path}" }
41
+ out = out.join("\n") + "\n"
42
+
43
+ in_root do
44
+ return unless File.exists?(manifest)
45
+ if File.open(manifest).read().include?('//= require_tree .')
46
+ inject_into_file(manifest, out, before: '//= require_tree .')
47
+ else
48
+ append_file(manifest, out)
49
+ end
50
+ end
51
+ end
52
+
53
+ def inject_production_config
54
+ in_root do
55
+ return unless File.exists?(production_config_path)
56
+
57
+ out = []
58
+ out << [" # Specify if you want Handlebars precompiled in this environment"]
59
+ out << [" config.handlebars.precompile = true"]
60
+ out = "\n#{out.join("\n")}\n"
61
+
62
+ inject_into_file(production_config_path, out, before: "\nend")
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ window.<%= app_name %> =
2
+ Models: {}
3
+ Collections: {}
4
+ Views: {}
5
+ Routers: {}
6
+ initialize: ->
7
+ # Initialize your app here
8
+
9
+ $ ->
10
+ <%= app_name %>.initialize()