Skip to content

Commit

Permalink
creating scaffold generator, implemented index and show actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Fitzgerald committed Jun 10, 2011
1 parent 810c633 commit 4d65d09
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ test/dummy/log/*.log
test/dummy/tmp/ test/dummy/tmp/
test/dummy/.sass-cache/ test/dummy/.sass-cache/
test/tmp/**/* test/tmp/**/*
.idea/
47 changes: 47 additions & 0 deletions .rvmrc
Original file line number Original file line Diff line number Diff line change
@@ -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'
#

18 changes: 9 additions & 9 deletions Gemfile.lock
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT GIT
remote: git://github.com/rails/rails.git remote: git://github.com/rails/rails.git
revision: f064664de72a34d24568eadf7340f41876df4b20 revision: 7d537b2106ab21a28e901e6e98c74df1f761243a
specs: specs:
actionmailer (3.1.0.rc1) actionmailer (3.1.0.rc1)
actionpack (= 3.1.0.rc1) actionpack (= 3.1.0.rc1)
Expand All @@ -15,7 +15,7 @@ GIT
rack-cache (~> 1.0.1) rack-cache (~> 1.0.1)
rack-mount (~> 0.8.1) rack-mount (~> 0.8.1)
rack-test (~> 0.6.0) rack-test (~> 0.6.0)
sprockets (~> 2.0.0.beta.8) sprockets (~> 2.0.0.beta.10)
tzinfo (~> 0.3.27) tzinfo (~> 0.3.27)
activemodel (3.1.0.rc1) activemodel (3.1.0.rc1)
activesupport (= 3.1.0.rc1) activesupport (= 3.1.0.rc1)
Expand Down Expand Up @@ -50,25 +50,25 @@ GIT


GIT GIT
remote: git://github.com/rtomayko/tilt.git remote: git://github.com/rtomayko/tilt.git
revision: 5cacf2aa460eadbcf8b7f2501d1e18502cdaff73 revision: e7b7a26fd6f183eeb9f4c43cda0e0a9f0728bd5d
specs: specs:
tilt (1.3.2) tilt (1.3.2)


GIT GIT
remote: git://github.com/sstephenson/execjs.git remote: git://github.com/sstephenson/execjs.git
revision: e9e1b97b26101f7c18084e1a3faa2bd2478e6ddb revision: 8c64ffb2476d244cd9fae9cbd803d588a8497796
specs: specs:
execjs (1.0.0) execjs (1.1.1)
multi_json (~> 1.0) multi_json (~> 1.0)


GIT GIT
remote: git://github.com/sstephenson/sprockets.git remote: git://github.com/sstephenson/sprockets.git
revision: 890be4ef4baeed8dcc5b39c73a8b18d25b998854 revision: f34395cdc4db915a8710a73fe676bb68075610e7
specs: specs:
sprockets (2.0.0.beta.9) sprockets (2.0.0.beta.10)
hike (~> 1.0) hike (~> 1.0)
rack (~> 1.0) rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1) tilt (~> 1.1, != 1.3.0)


GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
Expand Down Expand Up @@ -107,7 +107,7 @@ GEM
rack rack
rack-test (0.6.0) rack-test (0.6.0)
rack (>= 1.0) rack (>= 1.0)
rake (0.9.1) rake (0.9.2)
ruby-debug-base19 (0.11.25) ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1) columnize (>= 0.3.1)
linecache19 (>= 0.5.11) linecache19 (>= 0.5.11)
Expand Down
7 changes: 4 additions & 3 deletions Rakefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks' puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end 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.rdoc_dir = 'rdoc'
rdoc.title = 'BackboneRails' rdoc.title = 'BackboneRails'
rdoc.options << '--line-numbers' << '--inline-source' rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README.rdoc') rdoc.rdoc_files.include('README.md')
rdoc.rdoc_files.include('lib/**/*.rb') rdoc.rdoc_files.include('lib/**/*.rb')
end end


Expand Down
9 changes: 5 additions & 4 deletions backbone-rails.gemspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
# project in your rails apps through git. # project in your rails apps through git.
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "backbone-rails" s.name = "backbone-rails"
s.version = "0.0.1"
s.authors = ["Ryan Fitzgerald", "Code Brew Studios"] s.authors = ["Ryan Fitzgerald", "Code Brew Studios"]
s.email = ["ryan@codebrewstudios.com"] 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.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.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 "bundler", "~> 1.0.0"
s.add_development_dependency "rails", "~> 3.1"


s.require_paths = ['lib'] s.require_paths = ['lib']
end end
12 changes: 5 additions & 7 deletions lib/generators/backbone/controller/controller_generator.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,25 +24,23 @@ def validate_no_reserved_words
end end
end end


def create_controller_files def create_controller_files
template 'controller.coffee', File.join('app/assets/javascripts/backbone/controllers', class_path, "#{file_name}_controller.coffee") template 'controller.coffee', File.join(backbone_path, "controllers", class_path, "#{file_name}_controller.coffee")
end end


def create_view_files def create_view_files
actions.each do |action| actions.each do |action|
@action = action @action = action
@view_path = File.join('app/assets/javascripts/backbone/views', plural_name, "#{action}_view.coffee") @view_path = File.join(backbone_path, "views", plural_name, "#{action}_view.coffee")
@jst_path = File.join('app/assets/javascripts/backbone/templates', plural_name, "#{action}.jst.ejs") @jst_path = File.join(backbone_path,"templates", plural_name, "#{action}.jst.ejs")


template "view.coffee", @view_path template "view.coffee", @view_path
template "template.jst", @jst_path template "template.jst", @jst_path
end end
end end


protected protected
def jst(action)
"backbone/templates/#{plural_name}/#{action}"
end


end end
end end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/backbone/model/model_generator.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ModelGenerator < Rails::Generators::NamedBase
desc "This generator creates a backbone model" desc "This generator creates a backbone model"


def create_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


end end
Expand Down
12 changes: 10 additions & 2 deletions lib/generators/backbone/resource_helpers.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Backbone
module Generators module Generators
module ResourceHelpers module ResourceHelpers


def backbone_path
"app/assets/javascripts/backbone"
end

def model_namespace def model_namespace
[application_name.capitalize, "Models", class_name].join(".") [application_name.capitalize, "Models", class_name].join(".")
end end
Expand All @@ -11,11 +15,15 @@ def collection_namespace
end end


def view_namespace def view_namespace
[application_name.capitalize, "Views", class_name].join(".") [application_name.capitalize, "Views", plural_name.capitalize].join(".")
end end


def controller_namespace 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


end end
Expand Down
28 changes: 28 additions & 0 deletions lib/generators/backbone/scaffold/scaffold_generator.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions lib/generators/backbone/scaffold/templates/controller.coffee
Original file line number Original file line Diff line number Diff line change
@@ -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)

22 changes: 22 additions & 0 deletions lib/generators/backbone/scaffold/templates/index_view.coffee
Original file line number Original file line Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/generators/backbone/scaffold/templates/show_view.coffee
Original file line number Original file line Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/generators/generators_test_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ def self.included(base)
setup :prepare_destination setup :prepare_destination
end end
end end

def backbone_path
"app/assets/javascripts/backbone"
end

end end
38 changes: 38 additions & 0 deletions test/generators/scaffold_generator_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -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.