Skip to content

Commit

Permalink
updated to Rails 3.1, which removes the one irksome part I had integr…
Browse files Browse the repository at this point in the history
…ating Angular.js into Rails
  • Loading branch information
Daniel Nelson committed Sep 10, 2011
1 parent 822d348 commit dbf06c0
Show file tree
Hide file tree
Showing 79 changed files with 411 additions and 23,568 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.bundle
db/*.sqlite3
log/*.log
log/
tmp/
.DS_Store
public/system
2 changes: 1 addition & 1 deletion .rvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rvm use 1.9.2@angular_demo
rvm use 1.9.2@angular_rails_demo
13 changes: 12 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
source 'http://rubygems.org'

gem 'rails', '3.0.7'
gem 'rails', '3.1.0'
gem 'paperclip', '2.3.11'
gem 'rmagick'
# gem 'jquery-rails'

gem 'sqlite3'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end

group :development, :test do
gem 'rspec-rails'
gem 'ruby-debug19', :require => 'ruby-debug'
end

group :test do
gem 'jasmine'
gem 'capybara', '1.0.0'
gem 'launchy'
gem 'spork', '~> 0.9.0.rc2'
gem 'database_cleaner'
end

147 changes: 95 additions & 52 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,88 +1,113 @@
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.7)
actionpack (= 3.0.7)
mail (~> 2.2.15)
actionpack (3.0.7)
activemodel (= 3.0.7)
activesupport (= 3.0.7)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.5.0)
rack (~> 1.2.1)
rack-mount (~> 0.6.14)
rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
activemodel (3.0.7)
activesupport (= 3.0.7)
builder (~> 2.1.2)
i18n (~> 0.5.0)
activerecord (3.0.7)
activemodel (= 3.0.7)
activesupport (= 3.0.7)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activeresource (3.0.7)
activemodel (= 3.0.7)
activesupport (= 3.0.7)
activesupport (3.0.7)
actionmailer (3.1.0)
actionpack (= 3.1.0)
mail (~> 2.3.0)
actionpack (3.1.0)
activemodel (= 3.1.0)
activesupport (= 3.1.0)
builder (~> 3.0.0)
erubis (~> 2.7.0)
i18n (~> 0.6)
rack (~> 1.3.2)
rack-cache (~> 1.0.3)
rack-mount (~> 0.8.2)
rack-test (~> 0.6.1)
sprockets (~> 2.0.0)
activemodel (3.1.0)
activesupport (= 3.1.0)
bcrypt-ruby (~> 3.0.0)
builder (~> 3.0.0)
i18n (~> 0.6)
activerecord (3.1.0)
activemodel (= 3.1.0)
activesupport (= 3.1.0)
arel (~> 2.2.1)
tzinfo (~> 0.3.29)
activeresource (3.1.0)
activemodel (= 3.1.0)
activesupport (= 3.1.0)
activesupport (3.1.0)
multi_json (~> 1.0)
addressable (2.2.6)
archive-tar-minitar (0.5.2)
arel (2.0.10)
builder (2.1.2)
arel (2.2.1)
bcrypt-ruby (3.0.0)
builder (3.0.0)
capybara (1.0.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 0.2.0)
xpath (~> 0.1.4)
childprocess (0.2.1)
childprocess (0.2.2)
ffi (~> 1.0.6)
coffee-rails (3.1.0)
coffee-script (>= 2.2.0)
railties (~> 3.1.0.rc1)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.1.2)
columnize (0.3.4)
database_cleaner (0.6.7)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.2.4)
multi_json (~> 1.0)
ffi (1.0.9)
i18n (0.5.0)
json_pure (1.5.3)
hike (1.2.1)
i18n (0.6.0)
jasmine (1.0.2.1)
json_pure (>= 1.4.3)
rack (>= 1.1)
rspec (>= 1.3.1)
selenium-webdriver (>= 0.1.3)
json_pure (1.5.4)
spruz (~> 0.2.8)
launchy (2.0.5)
addressable (~> 2.2.6)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
mail (2.2.19)
activesupport (>= 2.3.6)
mail (2.3.0)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
multi_json (1.0.3)
nokogiri (1.5.0)
paperclip (2.3.11)
activerecord (>= 2.3.0)
activesupport (>= 2.3.2)
polyglot (0.3.2)
rack (1.2.3)
rack-mount (0.6.14)
rack (1.3.2)
rack-cache (1.0.3)
rack (>= 0.4)
rack-mount (0.8.3)
rack (>= 1.0.0)
rack-test (0.5.7)
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
rack (>= 1.0)
rails (3.0.7)
actionmailer (= 3.0.7)
actionpack (= 3.0.7)
activerecord (= 3.0.7)
activeresource (= 3.0.7)
activesupport (= 3.0.7)
rails (3.1.0)
actionmailer (= 3.1.0)
actionpack (= 3.1.0)
activerecord (= 3.1.0)
activeresource (= 3.1.0)
activesupport (= 3.1.0)
bundler (~> 1.0)
railties (= 3.0.7)
railties (3.0.7)
actionpack (= 3.0.7)
activesupport (= 3.0.7)
railties (= 3.1.0)
railties (3.1.0)
actionpack (= 3.1.0)
activesupport (= 3.1.0)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
thor (~> 0.14.4)
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2)
rdoc (3.9.4)
rmagick (2.13.1)
rspec (2.6.0)
rspec-core (~> 2.6.0)
Expand All @@ -108,18 +133,32 @@ GEM
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
rubyzip (0.9.4)
sass (3.1.7)
sass-rails (3.1.0)
actionpack (~> 3.1.0)
railties (~> 3.1.0)
sass (>= 3.1.4)
selenium-webdriver (0.2.2)
childprocess (>= 0.1.9)
ffi (>= 1.0.7)
json_pure
rubyzip
spork (0.9.0.rc9)
sprockets (2.0.0)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
spruz (0.2.13)
sqlite3 (1.3.4)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.29)
uglifier (1.0.3)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
xpath (0.1.4)
nokogiri (~> 1.3)

Expand All @@ -128,12 +167,16 @@ PLATFORMS

DEPENDENCIES
capybara (= 1.0.0)
coffee-rails (~> 3.1.0)
database_cleaner
jasmine
launchy
paperclip (= 2.3.11)
rails (= 3.0.7)
rails (= 3.1.0)
rmagick
rspec-rails
ruby-debug19
sass-rails (~> 3.1.0)
spork (~> 0.9.0.rc2)
sqlite3
uglifier
61 changes: 21 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Angular in Rails
================

This demo includes a fully working Rails 3.1 app integrated with Angular.js. It uses the Jasmine gem to run the Angular.js unit specs and RSpec integration specs to test the fully functional app.


Installing
==========

Expand All @@ -8,12 +14,13 @@ Installing
$ rake db:migrate
$ rake db:seed

To run the unit specs:

Angular in Rails
================
$ rake jasmine:ci

This demo includes a fully working Rails app integrated with Angular.js. Note that I have opted to use Capybara based RSpec integration specs, with Javascript enabled, instead of the end-to-end testing built into Angular.js (in part because I couldn't get it to work with the Rails app, but I only gave it a cursory attempt because I planned to use RSPec integration specs anyway).
To run the integration specs:

$ rake spec


Basic Approach
Expand All @@ -24,50 +31,24 @@ ApplicationController intercepts all html traffic, rendering 'layout/dynamic.htm
All json traffic reaches its intended controller/action and renders the object for Angular.js to process


Running the Unit Specs
Starting from Scratch
=====================

To run the unit tests (copied from http://docs.angularjs.org/#!/tutorial/step_02 (slightly modified)):
* In a separate terminal window or tab, go to the angular_demo directory and run ./scripts/test-server.sh to start the test web server.
* Open a new browser tab or window and navigate to http://localhost:9876.
* Choose "Capture this browser in strict mode".
* At this point, you can leave this tab open and forget about it. JsTestDriver will use it to execute the tests and report the results in the terminal.
* Execute the test by running ./js_script/test.sh
Because Rails makes it so easy to run Jasmine unit specs and RSpec (or Cucumber) integration specs on Javascript apps, we don't need most of the extra stuff that comes with Angular.js. All we really need are the angular.js and agular-ie-compat.js files in app/assets/javascripts, which you can get from https://github.com/angular/angular-seed/tree/master/app/lib/angular.

You should see something similar to the following:
All of the javascript files are in app/assets/javascript. The html partials are in public/partials

angular_demo$ js_script/test.sh
...
Total 3 tests (Passed: 3; Fails: 0; Errors: 0) (6.00 ms)
Chrome 13.0.782.218 Mac OS: Run 3 tests (Passed: 3; Fails: 0; Errors 0) (6.00 ms)
Wrap Params
===========

Rails 3.1 comes with some useful defaults for working with Angular.js. In earlier versions of Rails, the json representation of an object included the model name as a root element. This could be disabled, but then the params coming back to rails lacked the model-specific sub params.

That is, prior to 3.0, we would have:

Running the Integration Specs
============================

angular_demo $ rspec spec/requests



Starting from Scratch
====================

This is how I began, starting with a new Rails project and the Angular.js seed project:

demo $ git clone git://github.com/angular/angular-seed.git
demo $ rails new angular_demo --skip-test-unit
demo $ cd angular_demo
angular_demo $ mv ../angular-seed/app/js/* public/javascripts
angular_demo $ mv ../angular-seed/app/lib public/javascripts
angular_demo $ mv ../angular-seed/app/partials public/javascripts
angular_demo $ mv ../angular-seed/test js_spec # or js_test if you prefer a test dir
angular_demo $ mv ../angular-seed/scripts js_script
angular_demo $ mv ../angular-seed/config/* config/
params = { 'id' => 1, 'controller' => 'my_controller', 'action' => 'my_action', 'real_param_1' => 'a', 'real_param_2' => 'b' }

Then, to configure unit specs:
Thanks to wrap_parameters in 3.1, we have:

(Note that the configuration steps have already been performed in the rails project.)
params = { 'id' => 1, 'controller' => 'my_controller', 'action' => 'my_action', 'my_model' => { 'real_param_1' => 'a', 'real_param_2' => 'b' } }

* modify the config/js... configuration files to match the project hierarchy
* modify js_script/test.sh and js_script/test-server.sh so that the path to the jar is ../js_spec instead of ../test
The only downside to wrap_parameters is that it builds itself from the database schema, not from attr_accessible, so if you have attributes that don't correspond to the database, you will have to list them not only in attr_accessible, but also in the controller. See http://api.rubyonrails.org/classes/ActionController/ParamsWrapper/ClassMethods.html for details on how to customize what is passed.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)
require 'rake'

AngularDemo::Application.load_tasks
Binary file added app/assets/images/rails.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit dbf06c0

Please sign in to comment.