Skip to content

Commit

Permalink
Added a basic generator to install into your project. Also ability to…
Browse files Browse the repository at this point in the history
… install from HEAD
  • Loading branch information
keithpitt committed Jan 25, 2012
1 parent 54a3f00 commit c5af910
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 19 deletions.
48 changes: 36 additions & 12 deletions README.md
Expand Up @@ -10,42 +10,66 @@ You can see an example of how to use the gem [here](https://github.com/keithpitt

Add the gem to your application Gemfile:

gem "ember-rails"
```ruby
gem "ember-rails"
```

Run `bundle install` and add the following line to
Run `bundle install` and add the following line to
`app/assets/javascripts/application.js`:

//= require ember
```javascript
//= require ember
```

If yopu just want to use Ember Metal (no View or Handlebars support)
you can use:

//= require ember-runtime
```javascript
//= require ember-runtime
```

Ember-rails also provides a way to run Ember in development mode, you
can switch out your require statements to use the dev copies like so:

//= require ember-dev
```javascript
//= require ember-dev
```

If you want to copy ember to your own project, you can use the inbuilt
generator

```bash
rails g ember_rails:install
```

If you want to use the latest and greatest Ember, you can use the
generator to download and compile the latest version for you.

```bash
rails g ember_rails:install --head
```

Ask Rails to serve HandlebarsJS and pre-compile templates to Ember
by putting each template in a dedicated ".js.hjs" or ".handlebars" file
(e.g. `app/assets/javascripts/templates/admin_panel.handlebars`)
and including the assets in your layout:

<%= javascript_include_tag "templates/admin_panel" %>
```ruby
<%= javascript_include_tag "templates/admin_panel" %>
```
Bundle all templates together thanks to Sprockets,
e.g create `app/assets/javascripts/templates/all.js` with:
//= require_tree .
```javascript
//= require_tree .
```
Now a single line in the layout loads everything:
<%= javascript_include_tag "templates/all" %>

## History

ember-rails is based on https://github.com/kiskolabs/sproutcore-rails.
```ruby
<%= javascript_include_tag "templates/all" %>
```

## Note on Patches/Pull Requests

Expand Down
6 changes: 3 additions & 3 deletions ember-rails.gemspec
@@ -1,15 +1,15 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "ember-rails/version"
require "ember_rails/version"

Gem::Specification.new do |s|
s.name = "ember-rails"
s.version = EmberRails::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Keith Pitt", "Rob Monie", "Joao Carlos"]
s.email = ["me@keithpitt.com"]
s.homepage = "https://github.com/keithpitt/ember-rails"
s.summary = "Ember for Rails 3.1"
s.homepage = "https://github.com/ember/ember-rails"
s.summary = "Ember for Rails 3.x"

s.add_dependency "execjs", [">= 1.2"]
s.add_dependency "railties", [">= 3.0"]
Expand Down
2 changes: 1 addition & 1 deletion lib/ember-rails.rb → lib/ember_rails.rb
@@ -1,6 +1,6 @@
require 'sprockets'
require 'sprockets/engines'
require 'ember-rails/hjs_template'
require 'ember_rails/hjs_template'

module EmberRails
class Engine < Rails::Engine
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions lib/ember-rails/version.rb → lib/ember_rails/version.rb
@@ -1,3 +1,4 @@
module EmberRails
VERSION = "0.2.3"
EMBER_VERSION = "0.9.4"
end
70 changes: 70 additions & 0 deletions lib/generators/ember_rails/install/install_generator.rb
@@ -0,0 +1,70 @@
require 'rails'
require 'bundler'

module EmberRails
module Generators
class InstallGenerator < ::Rails::Generators::Base

FILES = [ "ember.js", "ember-dev.js", "ember-runtime.js", "ember-runtime-dev.js" ]

desc "This generator installs Ember.js #{EmberRails::EMBER_VERSION}"
class_option :head, :type => :boolean, :default => false, :desc => "Download latest Ember.js from GitHub and copy it into your project"

def remove_ember
FILES.each do |name|
remove_file "vendor/assets/javascripts/#{name}"
end
end

def copy_ember
if options.head?
git_root = File.expand_path "../../../../../vendor/ember", __FILE__
gem_file = File.join git_root, "Gemfile"
self.class.source_root git_root

Dir.chdir git_root do
say_status("downloading", "Ember.js (HEAD)", :green)
cmd "git fetch --force --quiet --tags"
cmd "git reset --hard"
say_status("building", "", :green)

Bundler.with_clean_env do
cmd "bundle --gemfile #{gem_file}"
cmd %{BUNDLE_GEMFILE="#{gem_file}" bundle exec rake}
end
end

Dir[File.join(git_root, "dist", "*.js")].each do |file|
name = File.basename file
if name.match /\.min/
name.gsub! /\.min/, ''
else
name.gsub! /\.js/, '-dev.js'
end
copy_file file, "vendor/assets/javascripts/#{name}"
end
else
self.class.source_root File.expand_path('../../../../../vendor/assets/javascripts', __FILE__)

say_status("copying", "Ember.js (#{EmberRails::EMBER_VERSION})", :green)

FILES.each do |name|
copy_file name, "vendor/assets/javascripts/#{name}"
end
end
end

private

def cmd(command)
out = %x{#{command}}

if $?.exitstatus != 0
raise "Command error: command `#{command}` in directory #{Dir.pwd} has failed."
end
out
end

end
end
end
2 changes: 1 addition & 1 deletion test/dummy/config/application.rb
Expand Up @@ -5,7 +5,7 @@
require 'sprockets/railtie'

Bundler.require
require "ember-rails"
require "ember_rails"

module Dummy
class Application < Rails::Application
Expand Down
2 changes: 1 addition & 1 deletion test/hjstemplate_test.rb
Expand Up @@ -15,7 +15,7 @@ class HjsTemplateTest < ActionController::IntegrationTest
test "asset pipeline should serve template" do
get "/assets/templates/test.js"
assert_response :success
assert @response.body == "Ember.TEMPLATES[\"templates/test\"] = Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) {\nhelpers = helpers || Ember.Handlebars.helpers;\n var buffer = '', stack1, stack2, stack3, stack4, tmp1, self=this, functionType=\"function\", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;\n\n\n stack1 = depth0;\n stack2 = \"test\";\n stack3 = {};\n stack4 = \"true\";\n stack3['escaped'] = stack4;\n stack4 = helpers.bind || depth0.bind;\n tmp1 = {};\n tmp1.hash = stack3;\n tmp1.contexts = [];\n tmp1.contexts.push(stack1);\n tmp1.data = data;\n if(typeof stack4 === functionType) { stack1 = stack4.call(depth0, stack2, tmp1); }\n else if(stack4=== undef) { stack1 = helperMissing.call(depth0, \"bind\", stack2, tmp1); }\n else { stack1 = stack4; }\n data.buffer.push(escapeExpression(stack1) + \"\\n\");\n return buffer;\n});\n", @response.body.inspect
assert @response.body == "Ember.TEMPLATES[\"templates/test\"] = Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) {\nhelpers = helpers || Ember.Handlebars.helpers;\n var buffer = '', stack1, stack2, stack3, stack4, tmp1, self=this, functionType=\"function\", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;\n\n\n stack1 = depth0;\n stack2 = \"test\";\n stack3 = {};\n stack4 = \"true\";\n stack3['escaped'] = stack4;\n stack4 = helpers._triageMustache || depth0._triageMustache;\n tmp1 = {};\n tmp1.hash = stack3;\n tmp1.contexts = [];\n tmp1.contexts.push(stack1);\n tmp1.data = data;\n if(typeof stack4 === functionType) { stack1 = stack4.call(depth0, stack2, tmp1); }\n else if(stack4=== undef) { stack1 = helperMissing.call(depth0, \"_triageMustache\", stack2, tmp1); }\n else { stack1 = stack4; }\n data.buffer.push(escapeExpression(stack1) + \"\\n\");\n return buffer;\n});\n", @response.body.inspect
end

test "ensure new lines inside the anon function are persisted" do
Expand Down
3 changes: 2 additions & 1 deletion vendor/assets/javascripts/ember-precompiler.js
Expand Up @@ -12,9 +12,10 @@ var console = window.console = {};
console.log = console.info = console.warn = console.error = function(){};

// jQuery
var jQuery = function() { return jQuery };
var jQuery = window.jQuery = function() { return jQuery };
jQuery.ready = function() { return jQuery };
jQuery.inArray = function() { return jQuery };
jQuery.jquery = "1.7";
var $ = jQuery;

// Precompiler
Expand Down

0 comments on commit c5af910

Please sign in to comment.