rails-backbone 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +40 -0
  3. data/Rakefile +48 -0
  4. data/lib/backbone-rails.rb +12 -0
  5. data/lib/generators/backbone/controller/controller_generator.rb +47 -0
  6. data/lib/generators/backbone/controller/templates/controller.coffee +14 -0
  7. data/lib/generators/backbone/controller/templates/template.jst +2 -0
  8. data/lib/generators/backbone/controller/templates/view.coffee +8 -0
  9. data/lib/generators/backbone/install/install_generator.rb +39 -0
  10. data/lib/generators/backbone/install/templates/app.coffee +11 -0
  11. data/lib/generators/backbone/model/model_generator.rb +19 -0
  12. data/lib/generators/backbone/model/templates/model.coffee +11 -0
  13. data/lib/generators/backbone/resource_helpers.rb +31 -0
  14. data/lib/generators/backbone/scaffold/scaffold_generator.rb +31 -0
  15. data/lib/generators/backbone/scaffold/templates/controller.coffee +32 -0
  16. data/lib/generators/backbone/scaffold/templates/model.coffee +11 -0
  17. data/lib/generators/backbone/scaffold/templates/templates/edit.jst +17 -0
  18. data/lib/generators/backbone/scaffold/templates/templates/index.jst +16 -0
  19. data/lib/generators/backbone/scaffold/templates/templates/model.jst +7 -0
  20. data/lib/generators/backbone/scaffold/templates/templates/new.jst +17 -0
  21. data/lib/generators/backbone/scaffold/templates/templates/show.jst +9 -0
  22. data/lib/generators/backbone/scaffold/templates/views/edit_view.coffee +24 -0
  23. data/lib/generators/backbone/scaffold/templates/views/index_view.coffee +22 -0
  24. data/lib/generators/backbone/scaffold/templates/views/model_view.coffee +19 -0
  25. data/lib/generators/backbone/scaffold/templates/views/new_view.coffee +25 -0
  26. data/lib/generators/backbone/scaffold/templates/views/show_view.coffee +8 -0
  27. data/lib/tasks/backbone-rails_tasks.rake +4 -0
  28. data/vendor/assets/javascripts/backbone.js +1098 -0
  29. data/vendor/assets/javascripts/backbone_datalink.js +21 -0
  30. data/vendor/assets/javascripts/backbone_rails_sync.js +51 -0
  31. data/vendor/assets/javascripts/underscore.js +818 -0
  32. metadata +126 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Ryan Fitzgerald
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Backbone-Rails
2
+
3
+ Easily setup and use backbone.js with rails 3.1
4
+
5
+ ## Rails 3.1 setup
6
+ This gem requires the use of rails 3.1, coffeescript and the new rails asset pipeline provided by sprockets.
7
+
8
+ This gem vendors the latest version of underscore.js and backbones.js for Rails 3.1 and greater. The files will be added to the asset pipeline and available for you to use.
9
+
10
+ ### Installation
11
+
12
+ In your Gemfile, add this line:
13
+
14
+ gem "backbone-rails"
15
+
16
+ Then run the following commands:
17
+
18
+ bundle install
19
+ rails g backbone:install
20
+
21
+ ## Generators
22
+ backbone-rails provides 3 simple generators to help get you started using bacbone.js with rails 3.1
23
+
24
+ ### Model Generator
25
+
26
+ rails g backbone:model
27
+
28
+ This generator creates a backbone model and collection inside `app/assets/javascript/backbone/models` to be used to talk to the rails backend.
29
+
30
+ ### Controller
31
+
32
+ rails g backbone:controller
33
+
34
+ This generator creates a backbone controller with corresponding views and templates for the given actions provided.
35
+
36
+ ### Scaffolding
37
+
38
+ rails g backbone:scaffold
39
+
40
+ This generator creates a controller, views, templates, model and collection to create a simple crud single page app
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ require 'bundler/gem_tasks'
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'BackboneRails'
14
+ rdoc.options << '--line-numbers' << '--inline-source'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'lib'
23
+ t.libs << 'test'
24
+ t.pattern = 'test/**/*_test.rb'
25
+ t.verbose = false
26
+ end
27
+
28
+ task :default => :test
29
+
30
+ namespace :backbone do
31
+ desc "Download the latest versions of underscore and backbone.js from git"
32
+ task :download_latest do
33
+ files = {
34
+ 'underscore.js'=>'https://github.com/documentcloud/underscore/raw/master/underscore.js',
35
+ 'backbone.js' => 'https://github.com/documentcloud/backbone/raw/master/backbone.js'
36
+ }
37
+
38
+ vendor_dir = "vendor/assets/javascripts"
39
+
40
+ require 'open-uri'
41
+ files.each do |local,remote|
42
+ puts "Downloading #{local}"
43
+ File.open "#{vendor_dir}/#{local}", 'w' do |f|
44
+ f.write open(remote).read
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+
3
+ module BackboneRails
4
+ class Engine < Rails::Engine
5
+ # config.autoload_paths << File.expand_path("../backbone-rails", __FILE__)
6
+
7
+ config.after_initialize do |app|
8
+ # app.assets.register_engine '.jst', Tilt::UnderscoreTemplate
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,47 @@
1
+ require 'generators/backbone/resource_helpers'
2
+
3
+ module Backbone
4
+ module Generators
5
+ class ControllerGenerator < Rails::Generators::NamedBase
6
+ include Backbone::Generators::ResourceHelpers
7
+
8
+ source_root File.expand_path("../templates", __FILE__)
9
+ desc "This generator creates a backbone controller with views and templates for the provided actions"
10
+
11
+ argument :actions, :type => :array, :default => [], :banner => "action action"
12
+
13
+ RESERVED_JS_WORDS = %W{
14
+ break case catch continue debugger default delete do else finally for
15
+ function if in instanceof new return switch this throw try typeof var void while with
16
+ }
17
+
18
+ def validate_no_reserved_words
19
+ actions.each do |action|
20
+ if RESERVED_JS_WORDS.include? action
21
+ raise Thor::Error, "The name '#{action}' is reserved by javascript " <<
22
+ "Please choose an alternative action name and run this generator again."
23
+ end
24
+ end
25
+ end
26
+
27
+ def create_controller_files
28
+ template 'controller.coffee', File.join(backbone_path, "controllers", class_path, "#{file_name}_controller.coffee")
29
+ end
30
+
31
+ def create_view_files
32
+ actions.each do |action|
33
+ @action = action
34
+ @view_path = File.join(backbone_path, "views", plural_name, "#{action}_view.coffee")
35
+ @jst_path = File.join(backbone_path,"templates", plural_name, "#{action}.jst.ejs")
36
+
37
+ template "view.coffee", @view_path
38
+ template "template.jst", @jst_path
39
+ end
40
+ end
41
+
42
+ protected
43
+
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+ class <%= controller_namespace %>Controller extends Backbone.Controller
2
+ initialize: (options) ->
3
+
4
+ routes:
5
+ <% actions.each do |action| -%>
6
+ "/<%= action %>": "<%= action %>"
7
+ <% end -%>
8
+
9
+ <% actions.each do |action| -%>
10
+ <%= action %>: ->
11
+ @view = new <%= "#{view_namespace}.#{action.capitalize}View()" %>
12
+ $("#<%= plural_name %>").html(@view.render().el)
13
+
14
+ <% end -%>
@@ -0,0 +1,2 @@
1
+ <h1><%= class_name %>#<%= @action %></h1>
2
+ <p>Find me in <%= @jst_path %></p>
@@ -0,0 +1,8 @@
1
+ <%= view_namespace %> ||= {}
2
+
3
+ class <%= view_namespace %>.<%= @action.capitalize %>View extends Backbone.View
4
+ template: JST["<%= jst @action %>"]
5
+
6
+ render: ->
7
+ $(@el).html(@template())
8
+ return this
@@ -0,0 +1,39 @@
1
+ module Backbone
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ desc "This generator installs backbone.js with a default folder layout in app/assets/javascripts/backbone"
7
+
8
+ class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
9
+ :desc => "Skip Git ignores and keeps"
10
+
11
+ def inject_backbone
12
+ inject_into_file "app/assets/javascripts/application.js", :before => "//= require_tree" do
13
+ "//= require underscore\n//= require backbone\n//= require backbone_rails_sync\n//= require backbone_datalink\n//= require backbone/#{application_name}\n"
14
+ end
15
+ end
16
+
17
+ def create_dir_layout
18
+ %W{controllers models views templates}.each do |dir|
19
+ empty_directory "app/assets/javascripts/backbone/#{dir}"
20
+ create_file "app/assets/javascripts/backbone/#{dir}/.gitkeep" unless options[:skip_git]
21
+ end
22
+ end
23
+
24
+ def create_app_file
25
+ template "app.coffee", "app/assets/javascripts/backbone/#{application_name}.coffee"
26
+ end
27
+
28
+ protected
29
+ def application_name
30
+ if defined?(Rails) && Rails.application
31
+ Rails.application.class.name.split('::').first.underscore
32
+ else
33
+ "application"
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ #= require_self
2
+ #= require_tree ./templates
3
+ #= require_tree ./models
4
+ #= require_tree ./views
5
+ #= require_tree ./controllers
6
+
7
+ window.<%= application_name.capitalize %> =
8
+ Models: {}
9
+ Collections: {}
10
+ Controllers: {}
11
+ Views: {}
@@ -0,0 +1,19 @@
1
+ require 'generators/backbone/resource_helpers'
2
+
3
+ module Backbone
4
+ module Generators
5
+ class ModelGenerator < Rails::Generators::NamedBase
6
+ include Backbone::Generators::ResourceHelpers
7
+
8
+ source_root File.expand_path("../templates", __FILE__)
9
+ desc "This generator creates a backbone model"
10
+
11
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
12
+
13
+ def create_backbone_model
14
+ template "model.coffee", "#{backbone_path}/models/#{file_name}.coffee"
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ class <%= model_namespace %> extends Backbone.Model
2
+ paramRoot: '<%= singular_table_name %>'
3
+
4
+ defaults:
5
+ <% attributes.each do |attribute| -%>
6
+ <%= attribute.name %>: null
7
+ <% end -%>
8
+
9
+ class <%= collection_namespace %>Collection extends Backbone.Collection
10
+ model: <%= model_namespace %>
11
+ url: '<%= route_url %>'
@@ -0,0 +1,31 @@
1
+ module Backbone
2
+ module Generators
3
+ module ResourceHelpers
4
+
5
+ def backbone_path
6
+ "app/assets/javascripts/backbone"
7
+ end
8
+
9
+ def model_namespace
10
+ [application_name.capitalize, "Models", class_name].join(".")
11
+ end
12
+
13
+ def collection_namespace
14
+ [application_name.capitalize, "Collections", plural_name.capitalize].join(".")
15
+ end
16
+
17
+ def view_namespace
18
+ [application_name.capitalize, "Views", plural_name.capitalize].join(".")
19
+ end
20
+
21
+ def controller_namespace
22
+ [application_name.capitalize, "Controllers", plural_name.capitalize].join(".")
23
+ end
24
+
25
+ def jst(action)
26
+ "backbone/templates/#{plural_name}/#{action}"
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'generators/backbone/model/model_generator'
2
+
3
+ module Backbone
4
+ module Generators
5
+ class ScaffoldGenerator < ModelGenerator
6
+
7
+ source_root File.expand_path("../templates", __FILE__)
8
+ desc "This generator creates the client side crud scaffolding"
9
+
10
+ def create_controller_files
11
+ template 'controller.coffee', File.join(backbone_path, "controllers", class_path, "#{plural_name}_controller.coffee")
12
+ end
13
+
14
+ def create_view_files
15
+ available_views.each do |view|
16
+ template "views/#{view}_view.coffee", File.join(backbone_path, "views", plural_name, "#{view}_view.coffee")
17
+ template "templates/#{view}.jst", File.join(backbone_path, "templates", plural_name, "#{view}.jst.ejs")
18
+ end
19
+
20
+ template "views/model_view.coffee", File.join(backbone_path, "views", plural_name, "#{singular_name}_view.coffee")
21
+ template "templates/model.jst", File.join(backbone_path, "templates", plural_name, "#{singular_name}.jst.ejs")
22
+ end
23
+
24
+ protected
25
+ def available_views
26
+ %w(index show new edit)
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ class <%= controller_namespace %>Controller extends Backbone.Controller
2
+ initialize: (options) ->
3
+ @<%= plural_name %> = new <%= collection_namespace %>Collection()
4
+ @<%= plural_name %>.refresh options.<%= plural_name %>
5
+
6
+ routes:
7
+ "/new": "new<%= class_name %>"
8
+ "/index": "index"
9
+ "/:id/edit": "edit"
10
+ "/:id": "show"
11
+ ".*": "index"
12
+
13
+ new<%= class_name %>: ->
14
+ @view = new <%= "#{view_namespace}.NewView(collection: @#{plural_name})" %>
15
+ $("#<%= plural_name %>").html(@view.render().el)
16
+
17
+ index: ->
18
+ @view = new <%= "#{view_namespace}.IndexView(#{plural_name}: @#{plural_name})" %>
19
+ $("#<%= plural_name %>").html(@view.render().el)
20
+
21
+ show: (id) ->
22
+ <%= singular_name %> = @<%= plural_name %>.get(id)
23
+
24
+ @view = new <%= "#{view_namespace}.ShowView(model: #{singular_name})" %>
25
+ $("#<%= plural_name %>").html(@view.render().el)
26
+
27
+ edit: (id) ->
28
+ <%= singular_name %> = @<%= plural_name %>.get(id)
29
+
30
+ @view = new <%= "#{view_namespace}.EditView(model: #{singular_name})" %>
31
+ $("#<%= plural_name %>").html(@view.render().el)
32
+
@@ -0,0 +1,11 @@
1
+ class <%= model_namespace %> extends Backbone.Model
2
+ paramRoot: '<%= singular_table_name %>'
3
+
4
+ defaults:
5
+ <% attributes.each do |attribute| -%>
6
+ <%= attribute.name %>: null
7
+ <% end -%>
8
+
9
+ class <%= collection_namespace %>Collection extends Backbone.Collection
10
+ model: <%= model_namespace %>
11
+ url: '<%= route_url %>'
@@ -0,0 +1,17 @@
1
+ <h1>Edit <%= singular_table_name %></h1>
2
+
3
+ <form id="edit-<%= singular_table_name %>" name="<%= singular_table_name %>">
4
+ <% attributes.each do |attribute| -%>
5
+ <div class="field">
6
+ <label for="<%= attribute.name %>"> <%= attribute.name %>:</label>
7
+ <input type="text" name="<%= attribute.name %>" id="<%= attribute.name %>" value=<%%= <%= attribute.name %> %> >
8
+ </div>
9
+
10
+ <% end -%>
11
+ <div class="actions">
12
+ <input type="submit" value="Update <%= human_name %>" />
13
+ </div>
14
+
15
+ </form>
16
+
17
+ <a href="#/index">Back</a>
@@ -0,0 +1,16 @@
1
+ <h1>Listing <%= plural_table_name %></h1>
2
+
3
+ <table id="<%= plural_name %>-table">
4
+ <tr>
5
+ <% attributes.each do |attribute| -%>
6
+ <th><%= attribute.human_name %></th>
7
+ <% end -%>
8
+ <th></th>
9
+ <th></th>
10
+ <th></th>
11
+ </tr>
12
+ </table>
13
+
14
+ <br/>
15
+
16
+ <a href="#/new">New <%= human_name %></a>
@@ -0,0 +1,7 @@
1
+ <% attributes.each do |attribute| -%>
2
+ <td><%%= <%= attribute.name %> %></td>
3
+ <% end -%>
4
+
5
+ <td><a href="#/<%%= id %>">Show</td>
6
+ <td><a href="#/<%%= id %>/edit">Edit</td>
7
+ <td><a href="#/<%%= id %>/destroy" class="destroy">Destroy</a></td>