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,36 +19,58 @@ class BackboneGenerator < Thor
def object_name def object_name
@name.gsub('::', '') @name.gsub('::', '')
end end
def generate_model
template('model.js.erb', "public/javascripts/models/#{underscore_name}.js")
template('model_spec.js.erb', "spec/javascripts/models/#{underscore_name}_spec.js")
end
def generate_view
template('view.js.erb', "public/javascripts/views/#{underscore_name}_view.js")
template('view.jst.erb', "app/views/#{underscore_name}.jst")
template('view_spec.js.erb', "spec/javascripts/views/#{underscore_name}_view_spec.js")
end
def generate_collection
template('collection.js.erb', "public/javascripts/collections/#{underscore_name}s.js")
template('collection_spec.js.erb', "spec/javascripts/collections/#{underscore_name}s_spec.js")
end
def generate_collection_view
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_spec.js.erb', "spec/javascripts/views/#{underscore_name}s_view_spec.js")
end
end end
desc 'model Namespaced::Name', "Create a model" desc 'model Namespaced::Name', "Create a model"
def model(name) def model(name)
@name = name @name = name
template('model.js.erb', "public/javascripts/models/#{underscore_name}.js") generate_model
template('model_spec.js.erb', "spec/javascripts/models/#{underscore_name}_spec.js")
end end
desc 'view Namespaced::Name', "Create a view" desc 'view Namespaced::Name', "Create a view"
def view(name) def view(name)
@name = name @name = name
template('view.js.erb', "public/javascripts/views/#{underscore_name}_view.js") generate_view
template('view.jst.erb', "app/views/#{underscore_name}.jst")
template('view_spec.js.erb', "spec/javascripts/views/#{underscore_name}_view_spec.js")
end end
desc 'collection Namespaced::Name', "Create a collection" desc 'collection Namespaced::Name', "Create a collection"
def collection(name) def collection(name)
@name = name @name = name
template('collection.js.erb', "public/javascripts/collections/#{underscore_name}s.js") generate_collection
template('collection_spec.js.erb', "spec/javascripts/collections/#{underscore_name}s_spec.js")
end end
desc 'collection-view Namespaced::Name', "Create a collection view" desc 'collection-view Namespaced::Name', "Create a collection view"
def collection_view(name) def collection_view(name)
@name = name @name = name
template('collection_view.js.erb', "public/javascripts/views/#{underscore_name}s_view.js") generate_collection_view
template('collection_view.jst.erb', "app/views/#{underscore_name}s.jst") end
template('collection_view_spec.js.erb', "spec/javascripts/views/#{underscore_name}s_view_spec.js")
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
end end

View File

@ -11,15 +11,51 @@ describe 'backbone-generator' do
before { clean! } before { clean! }
after { clean! } after { clean! }
def should_generate_model
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.read(model).should match(/SectionModel/)
File.read(spec).should match(/SectionModel/)
end
def should_generate_view
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?(template = 'app/views/section/model.jst').should be_true
File.read(view).should match(/SectionModel/)
File.read(view).should match(/return this/)
File.read(view).should match(%r{template: JST\['section/model'\]})
File.read(spec).should match(/SectionModel/)
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
def should_generate_collection_view
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?(template = 'app/views/section/models.jst').should be_true
File.read(view).should match(/SectionModelsView/)
File.read(view).should match(/SectionModelView/)
File.read(view).should match(/return this/)
File.read(view).should match(%r{template: JST\['section/models'\]})
File.read(spec).should match(/SectionModelsView/)
end
describe 'model' do describe 'model' do
it "should generate the model files" do it "should generate the model files" do
system %{bin/backbone-generator model Section::Model} system %{bin/backbone-generator model Section::Model}
File.file?(model = 'public/javascripts/models/section/model.js').should be_true should_generate_model
File.file?(spec = 'spec/javascripts/models/section/model_spec.js').should be_true
File.read(model).should match(/SectionModel/)
File.read(spec).should match(/SectionModel/)
end end
end end
@ -27,14 +63,7 @@ describe 'backbone-generator' do
it "should generate the view files" do it "should generate the view files" do
system %{bin/backbone-generator view Section::Model} system %{bin/backbone-generator view Section::Model}
File.file?(view = 'public/javascripts/views/section/model_view.js').should be_true should_generate_view
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.read(view).should match(/SectionModel/)
File.read(view).should match(/return this/)
File.read(view).should match(%r{template: JST\['section/model'\]})
File.read(spec).should match(/SectionModel/)
end end
end end
@ -42,15 +71,7 @@ describe 'backbone-generator' do
it "should generate the collection view files" do it "should generate the collection view files" do
system %{bin/backbone-generator collection-view Section::Model} system %{bin/backbone-generator collection-view Section::Model}
File.file?(view = 'public/javascripts/views/section/models_view.js').should be_true should_generate_collection_view
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.read(view).should match(/SectionModelsView/)
File.read(view).should match(/SectionModelView/)
File.read(view).should match(/return this/)
File.read(view).should match(%r{template: JST\['section/models'\]})
File.read(spec).should match(/SectionModelsView/)
end end
end end
@ -58,12 +79,18 @@ describe 'backbone-generator' 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