add scaffold option

This commit is contained in:
John Bintz 2011-04-15 20:43:48 -04:00
parent bd6707f683
commit d2dcd4569c
3 changed files with 87 additions and 38 deletions

View File

@ -32,5 +32,5 @@ Currently only works with Rails-ish projects and with Jasmine & Jammit. I'm self
* `spec/javascripts/views/admin/users_view_spec.js` * `spec/javascripts/views/admin/users_view_spec.js`
* `app/views/admin/users.jst` * `app/views/admin/users.jst`
Please add more and make it more friendly with things that are not Rails, Jasmine, and Jammit! Generate everything with `backbone-generate scaffold Admin::User`.

View File

@ -19,37 +19,59 @@ class BackboneGenerator < Thor
def object_name def object_name
@name.gsub('::', '') @name.gsub('::', '')
end end
end
desc 'model Namespaced::Name', "Create a model" def generate_model
def model(name)
@name = name
template('model.js.erb', "public/javascripts/models/#{underscore_name}.js") template('model.js.erb', "public/javascripts/models/#{underscore_name}.js")
template('model_spec.js.erb', "spec/javascripts/models/#{underscore_name}_spec.js") template('model_spec.js.erb', "spec/javascripts/models/#{underscore_name}_spec.js")
end end
desc 'view Namespaced::Name', "Create a view" def generate_view
def view(name)
@name = name
template('view.js.erb', "public/javascripts/views/#{underscore_name}_view.js") template('view.js.erb', "public/javascripts/views/#{underscore_name}_view.js")
template('view.jst.erb', "app/views/#{underscore_name}.jst") template('view.jst.erb', "app/views/#{underscore_name}.jst")
template('view_spec.js.erb', "spec/javascripts/views/#{underscore_name}_view_spec.js") template('view_spec.js.erb', "spec/javascripts/views/#{underscore_name}_view_spec.js")
end end
desc 'collection Namespaced::Name', "Create a collection" def generate_collection
def collection(name)
@name = name
template('collection.js.erb', "public/javascripts/collections/#{underscore_name}s.js") template('collection.js.erb', "public/javascripts/collections/#{underscore_name}s.js")
template('collection_spec.js.erb', "spec/javascripts/collections/#{underscore_name}s_spec.js") template('collection_spec.js.erb', "spec/javascripts/collections/#{underscore_name}s_spec.js")
end end
desc 'collection-view Namespaced::Name', "Create a collection view" def generate_collection_view
def collection_view(name)
@name = name
template('collection_view.js.erb', "public/javascripts/views/#{underscore_name}s_view.js") template('collection_view.js.erb', "public/javascripts/views/#{underscore_name}s_view.js")
template('collection_view.jst.erb', "app/views/#{underscore_name}s.jst") template('collection_view.jst.erb', "app/views/#{underscore_name}s.jst")
template('collection_view_spec.js.erb', "spec/javascripts/views/#{underscore_name}s_view_spec.js") template('collection_view_spec.js.erb', "spec/javascripts/views/#{underscore_name}s_view_spec.js")
end end
end end
desc 'model Namespaced::Name', "Create a model"
def model(name)
@name = name
generate_model
end
desc 'view Namespaced::Name', "Create a view"
def view(name)
@name = name
generate_view
end
desc 'collection Namespaced::Name', "Create a collection"
def collection(name)
@name = name
generate_collection
end
desc 'collection-view Namespaced::Name', "Create a collection view"
def collection_view(name)
@name = name
generate_collection_view
end
desc 'scaffold Namespaced::Name', "Generate everything for this object"
def scaffold(name)
@name = name
%w{model view collection collection_view}.each { |which| send("generate_#{which}") }
end
end
BackboneGenerator.start BackboneGenerator.start

View File

@ -11,22 +11,15 @@ describe 'backbone-generator' do
before { clean! } before { clean! }
after { clean! } after { clean! }
describe 'model' do def should_generate_model
it "should generate the model files" do
system %{bin/backbone-generator model Section::Model}
File.file?(model = 'public/javascripts/models/section/model.js').should be_true File.file?(model = 'public/javascripts/models/section/model.js').should be_true
File.file?(spec = 'spec/javascripts/models/section/model_spec.js').should be_true File.file?(spec = 'spec/javascripts/models/section/model_spec.js').should be_true
File.read(model).should match(/SectionModel/) File.read(model).should match(/SectionModel/)
File.read(spec).should match(/SectionModel/) File.read(spec).should match(/SectionModel/)
end end
end
describe 'view' do
it "should generate the view files" do
system %{bin/backbone-generator view Section::Model}
def should_generate_view
File.file?(view = 'public/javascripts/views/section/model_view.js').should be_true File.file?(view = 'public/javascripts/views/section/model_view.js').should be_true
File.file?(spec = 'spec/javascripts/views/section/model_view_spec.js').should be_true File.file?(spec = 'spec/javascripts/views/section/model_view_spec.js').should be_true
File.file?(template = 'app/views/section/model.jst').should be_true File.file?(template = 'app/views/section/model.jst').should be_true
@ -36,12 +29,17 @@ describe 'backbone-generator' do
File.read(view).should match(%r{template: JST\['section/model'\]}) File.read(view).should match(%r{template: JST\['section/model'\]})
File.read(spec).should match(/SectionModel/) File.read(spec).should match(/SectionModel/)
end end
def should_generate_collection
File.file?(collection = 'public/javascripts/collections/section/models.js').should be_true
File.file?(spec = 'spec/javascripts/collections/section/models_spec.js').should be_true
File.read(collection).should match(/SectionModels/)
File.read(collection).should match(%r{section/model})
File.read(spec).should match(/SectionModels/)
end end
describe 'collection view' do def should_generate_collection_view
it "should generate the collection view files" do
system %{bin/backbone-generator collection-view Section::Model}
File.file?(view = 'public/javascripts/views/section/models_view.js').should be_true File.file?(view = 'public/javascripts/views/section/models_view.js').should be_true
File.file?(spec = 'spec/javascripts/views/section/models_view_spec.js').should be_true File.file?(spec = 'spec/javascripts/views/section/models_view_spec.js').should be_true
File.file?(template = 'app/views/section/models.jst').should be_true File.file?(template = 'app/views/section/models.jst').should be_true
@ -52,18 +50,47 @@ describe 'backbone-generator' do
File.read(view).should match(%r{template: JST\['section/models'\]}) File.read(view).should match(%r{template: JST\['section/models'\]})
File.read(spec).should match(/SectionModelsView/) File.read(spec).should match(/SectionModelsView/)
end end
describe 'model' do
it "should generate the model files" do
system %{bin/backbone-generator model Section::Model}
should_generate_model
end
end
describe 'view' do
it "should generate the view files" do
system %{bin/backbone-generator view Section::Model}
should_generate_view
end
end
describe 'collection view' do
it "should generate the collection view files" do
system %{bin/backbone-generator collection-view Section::Model}
should_generate_collection_view
end
end end
describe 'collection' do describe 'collection' do
it "should generate the collection files" do it "should generate the collection files" do
system %{bin/backbone-generator collection Section::Model} system %{bin/backbone-generator collection Section::Model}
File.file?(collection = 'public/javascripts/collections/section/models.js').should be_true should_generate_collection
File.file?(spec = 'spec/javascripts/collections/section/models_spec.js').should be_true end
end
File.read(collection).should match(/SectionModels/) describe 'scaffold' do
File.read(collection).should match(%r{section/model}) it "should generate everything!" do
File.read(spec).should match(/SectionModels/) system %{bin/backbone-generator scaffold Section::Model}
should_generate_model
should_generate_view
should_generate_collection
should_generate_collection_view
end end
end end
end end