Browse files

creating scaffold generator, implemented index and show actions

  • Loading branch information...
1 parent 810c633 commit 4d65d092be11714a516ba5fb5b310d7ad61356f0 Ryan Fitzgerald committed Jun 10, 2011
View
1 .gitignore
@@ -6,3 +6,4 @@ test/dummy/log/*.log
test/dummy/tmp/
test/dummy/.sass-cache/
test/tmp/**/*
+.idea/
View
47 .rvmrc
@@ -0,0 +1,47 @@
+#!/usr/bin/env bash
+
+# This is an RVM Project .rvmrc file, used to automatically load the ruby
+# development environment upon cd'ing into the directory
+
+# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
+environment_id="ruby-1.9.2-p180@backbone-rails"
+
+#
+# First we attempt to load the desired environment directly from the environment
+# file, this is very fast and efficicent compared to running through the entire
+# CLI and selector. If you want feedback on which environment was used then
+# insert the word 'use' after --create as this triggers verbose mode.
+#
+if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]] ; then
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
+else
+ # If the environment file has not yet been created, use the RVM CLI to select.
+ rvm --create "$environment_id"
+fi
+
+#
+# If you use an RVM gemset file to install a list of gems (*.gems), you can have
+# it be automatically loaded, uncomment the following and adjust the filename if
+# necessary.
+#
+# filename=".gems"
+# if [[ -s "$filename" ]] ; then
+# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
+# fi
+
+#
+# If you use bundler and would like to run bundle each time you enter the
+# directory you can uncomment the following code.
+#
+# Ensure that Bundler is installed, install it if it is not.
+if ! command -v bundle ; then
+ printf "The rubygem 'bundler' is not installed, installing it now.\n"
+ gem install bundler
+fi
+#
+# # Bundle while redcing excess noise.
+# printf "Bundling your gems this may take a few minutes on a fresh clone.\n"
+# bundle | grep -v 'Using' | grep -v 'complete' | sed '/^$/d'
+#
+
View
18 Gemfile.lock
@@ -1,6 +1,6 @@
GIT
remote: git://github.com/rails/rails.git
- revision: f064664de72a34d24568eadf7340f41876df4b20
+ revision: 7d537b2106ab21a28e901e6e98c74df1f761243a
specs:
actionmailer (3.1.0.rc1)
actionpack (= 3.1.0.rc1)
@@ -15,7 +15,7 @@ GIT
rack-cache (~> 1.0.1)
rack-mount (~> 0.8.1)
rack-test (~> 0.6.0)
- sprockets (~> 2.0.0.beta.8)
+ sprockets (~> 2.0.0.beta.10)
tzinfo (~> 0.3.27)
activemodel (3.1.0.rc1)
activesupport (= 3.1.0.rc1)
@@ -50,25 +50,25 @@ GIT
GIT
remote: git://github.com/rtomayko/tilt.git
- revision: 5cacf2aa460eadbcf8b7f2501d1e18502cdaff73
+ revision: e7b7a26fd6f183eeb9f4c43cda0e0a9f0728bd5d
specs:
tilt (1.3.2)
GIT
remote: git://github.com/sstephenson/execjs.git
- revision: e9e1b97b26101f7c18084e1a3faa2bd2478e6ddb
+ revision: 8c64ffb2476d244cd9fae9cbd803d588a8497796
specs:
- execjs (1.0.0)
+ execjs (1.1.1)
multi_json (~> 1.0)
GIT
remote: git://github.com/sstephenson/sprockets.git
- revision: 890be4ef4baeed8dcc5b39c73a8b18d25b998854
+ revision: f34395cdc4db915a8710a73fe676bb68075610e7
specs:
- sprockets (2.0.0.beta.9)
+ sprockets (2.0.0.beta.10)
hike (~> 1.0)
rack (~> 1.0)
- tilt (!= 1.3.0, ~> 1.1)
+ tilt (~> 1.1, != 1.3.0)
GEM
remote: http://rubygems.org/
@@ -107,7 +107,7 @@ GEM
rack
rack-test (0.6.0)
rack (>= 1.0)
- rake (0.9.1)
+ rake (0.9.2)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
View
7 Rakefile
@@ -5,13 +5,14 @@ rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
-require 'rake/rdoctask'
+require 'bundler/gem_tasks'
+require 'rdoc/task'
-Rake::RDocTask.new(:rdoc) do |rdoc|
+RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'BackboneRails'
rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('README.md')
rdoc.rdoc_files.include('lib/**/*.rb')
end
View
9 backbone-rails.gemspec
@@ -2,17 +2,18 @@
# project in your rails apps through git.
Gem::Specification.new do |s|
s.name = "backbone-rails"
+ s.version = "0.0.1"
s.authors = ["Ryan Fitzgerald", "Code Brew Studios"]
s.email = ["ryan@codebrewstudios.com"]
- s.homepage = "http://rubygems.org/gems/backbone-rails"
+ s.homepage = "http://github.com/codebrew/backbone-rails"
s.summary = "Use backbone.js with rails 3.1"
- s.description = "quickly setup backbone.js for use with rails 3.1"
+ s.description = "Quickly setup backbone.js for use with rails 3.1. Generators are provided to quickly get started."
s.files = Dir["lib/**/*"] + Dir["vendor/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
- s.version = "0.0.1"
+
+ s.add_dependency('rails', '~> 3.1.0.beta1')
s.add_development_dependency "bundler", "~> 1.0.0"
- s.add_development_dependency "rails", "~> 3.1"
s.require_paths = ['lib']
end
View
12 lib/generators/backbone/controller/controller_generator.rb
@@ -24,25 +24,23 @@ def validate_no_reserved_words
end
end
- def create_controller_files
- template 'controller.coffee', File.join('app/assets/javascripts/backbone/controllers', class_path, "#{file_name}_controller.coffee")
+ def create_controller_files
+ template 'controller.coffee', File.join(backbone_path, "controllers", class_path, "#{file_name}_controller.coffee")
end
def create_view_files
actions.each do |action|
@action = action
- @view_path = File.join('app/assets/javascripts/backbone/views', plural_name, "#{action}_view.coffee")
- @jst_path = File.join('app/assets/javascripts/backbone/templates', plural_name, "#{action}.jst.ejs")
+ @view_path = File.join(backbone_path, "views", plural_name, "#{action}_view.coffee")
+ @jst_path = File.join(backbone_path,"templates", plural_name, "#{action}.jst.ejs")
template "view.coffee", @view_path
template "template.jst", @jst_path
end
end
protected
- def jst(action)
- "backbone/templates/#{plural_name}/#{action}"
- end
+
end
end
View
2 lib/generators/backbone/model/model_generator.rb
@@ -9,7 +9,7 @@ class ModelGenerator < Rails::Generators::NamedBase
desc "This generator creates a backbone model"
def create_backbone_model
- template "model.coffee", "app/assets/javascripts/backbone/models/#{file_name}.coffee"
+ template "model.coffee", "#{backbone_path}/models/#{file_name}.coffee"
end
end
View
12 lib/generators/backbone/resource_helpers.rb
@@ -2,6 +2,10 @@ module Backbone
module Generators
module ResourceHelpers
+ def backbone_path
+ "app/assets/javascripts/backbone"
+ end
+
def model_namespace
[application_name.capitalize, "Models", class_name].join(".")
end
@@ -11,11 +15,15 @@ def collection_namespace
end
def view_namespace
- [application_name.capitalize, "Views", class_name].join(".")
+ [application_name.capitalize, "Views", plural_name.capitalize].join(".")
end
def controller_namespace
- [application_name.capitalize, "Controllers", class_name].join(".")
+ [application_name.capitalize, "Controllers", plural_name.capitalize].join(".")
+ end
+
+ def jst(action)
+ "backbone/templates/#{plural_name}/#{action}"
end
end
View
28 lib/generators/backbone/scaffold/scaffold_generator.rb
@@ -0,0 +1,28 @@
+require 'generators/backbone/resource_helpers'
+
+module Backbone
+ module Generators
+ class ScaffoldGenerator < Rails::Generators::NamedBase
+ include Backbone::Generators::ResourceHelpers
+
+ source_root File.expand_path("../templates", __FILE__)
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
+ def create_controller_files
+ template 'controller.coffee', File.join(backbone_path, "controllers", class_path, "#{plural_name}_controller.coffee")
+ end
+
+ def create_view_files
+ available_views.each do |view|
+ template "#{view}_view.coffee", File.join(backbone_path, "views", plural_name, "#{view}_view.coffee")
+ end
+ end
+
+ protected
+ def available_views
+ %w(index show)
+ end
+
+ end
+ end
+end
View
32 lib/generators/backbone/scaffold/templates/controller.coffee
@@ -0,0 +1,32 @@
+class <%= controller_namespace %>Controller extends Backbone.Controller
+ initialize: (options) ->
+ @<%= plural_name %> = new <%= collection_namespace %>Collection()
+ @<%= plural_name %>.refresh options.<%= plural_name %>
+
+ routes:
+ "/new": "new<%= class_name %>"
+ "/index": "index"
+ "/:id/edit": "edit"
+ "/:id": "show"
+ ".*": "index"
+
+ newProject: ->
+ @view = new <%= "#{view_namespace}.NewView(model: new @#{plural_name}.model())" %>
+ $("#<%= plural_name %>").html(@view.render().el)
+
+ index: ->
+ @view = new <%= "#{view_namespace}.IndexView(#{plural_name}: @#{plural_name})" %>
+ $("#<%= plural_name %>").html(@view.render().el)
+
+ show: (id) ->
+ <%= singular_name %> = @<%= plural_name %>.get(id)
+
+ @view = new <%= "#{view_namespace}.ShowView(model: #{singular_name})" %>
+ $("#<%= plural_name %>").html(@view.render().el)
+
+ edit: (id) ->
+ <%= singular_name %> = @<%= plural_name %>.get(id)
+
+ @view = new <%= "#{view_namespace}.EditView(model: #{singular_name})" %>
+ $("#<%= plural_name %>").html(@view.render().el)
+
View
22 lib/generators/backbone/scaffold/templates/index_view.coffee
@@ -0,0 +1,22 @@
+<%= view_namespace %> ||= {}
+
+class <%= view_namespace %>.IndexView extends Backbone.View
+ template: JST["<%= jst 'index' %>"]
+
+ initialize: () ->
+ _.bindAll(this, 'addOne', 'addAll', 'render');
+
+ @options.<%= plural_name %>.bind('refresh', this.addAll);
+
+ addAll: () ->
+ @options.<%= plural_name %>.each(this.addOne)
+
+ addOne: (<%= singular_name %>) ->
+ view = new <%= view_namespace %>.<%= singular_name.capitalize %>View({model : <%= singular_name %>})
+ this.$("tbody").append(view.render().el)
+
+ render: ->
+ $(this.el).html(this.template()(<%= plural_name %>: this.options.<%= plural_name %>.toJSON() ))
+ @addAll()
+
+ return this
View
8 lib/generators/backbone/scaffold/templates/show_view.coffee
@@ -0,0 +1,8 @@
+<%= view_namespace %> ||= {}
+
+class <%= view_namespace %>.ShowView extends Backbone.View
+ template: JST["<%= jst 'show' %>"]
+
+ render: ->
+ $(this.el).html(this.template()(this.options.model.toJSON() ))
+ return this
View
5 test/generators/generators_test_helper.rb
@@ -5,4 +5,9 @@ def self.included(base)
setup :prepare_destination
end
end
+
+ def backbone_path
+ "app/assets/javascripts/backbone"
+ end
+
end
View
38 test/generators/scaffold_generator_test.rb
@@ -0,0 +1,38 @@
+require 'test_helper'
+require 'generators/generators_test_helper'
+require "generators/backbone/scaffold/scaffold_generator"
+
+class ScaffoldGeneratorTest < Rails::Generators::TestCase
+ include GeneratorsTestHelper
+ tests Backbone::Generators::ScaffoldGenerator
+ arguments %w(Post title:string content:string)
+
+ test "generate controller scaffolding" do
+ run_generator
+
+ assert_file "#{backbone_path}/controllers/posts_controller.coffee" do |controller|
+ assert_match /class Dummy.Controllers.PostsController extends Backbone.Controller/, controller
+
+ %w(NewView IndexView ShowView EditView).each do |view|
+ assert_match /new Dummy.Views.Posts.#{view}/, controller
+ end
+
+ end
+ end
+
+ test "generate view files" do
+ run_generator
+
+ assert_file "#{backbone_path}/views/posts/index_view.coffee" do |view|
+ assert_match /#{Regexp.escape('JST["backbone/templates/posts/index"]')}/, view
+ assert_match /#{Regexp.escape("new Dummy.Views.Posts.PostView({model : post})")}/, view
+ end
+
+ assert_file "#{backbone_path}/views/posts/show_view.coffee" do |view|
+ assert_match /class Dummy.Views.Posts.ShowView extends Backbone.View/, view
+ assert_match /#{Regexp.escape('template: JST["backbone/templates/posts/show"]')}/, view
+ end
+
+ end
+
+end

0 comments on commit 4d65d09

Please sign in to comment.