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`
* `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
@name.gsub('::', '')
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
desc 'model Namespaced::Name', "Create a model"
def model(name)
@name = name
template('model.js.erb', "public/javascripts/models/#{underscore_name}.js")
template('model_spec.js.erb', "spec/javascripts/models/#{underscore_name}_spec.js")
generate_model
end
desc 'view Namespaced::Name', "Create a view"
def view(name)
@name = name
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")
generate_view
end
desc 'collection Namespaced::Name', "Create a collection"
def collection(name)
@name = name
template('collection.js.erb', "public/javascripts/collections/#{underscore_name}s.js")
template('collection_spec.js.erb', "spec/javascripts/collections/#{underscore_name}s_spec.js")
generate_collection
end
desc 'collection-view Namespaced::Name', "Create a collection view"
def collection_view(name)
@name = name
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")
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

View File

@ -11,15 +11,51 @@ describe 'backbone-generator' do
before { 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
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?(spec = 'spec/javascripts/models/section/model_spec.js').should be_true
File.read(model).should match(/SectionModel/)
File.read(spec).should match(/SectionModel/)
should_generate_model
end
end
@ -27,14 +63,7 @@ describe 'backbone-generator' do
it "should generate the view files" do
system %{bin/backbone-generator view Section::Model}
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/)
should_generate_view
end
end
@ -42,15 +71,7 @@ describe 'backbone-generator' do
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?(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/)
should_generate_collection_view
end
end
@ -58,12 +79,18 @@ describe 'backbone-generator' do
it "should generate the collection files" do
system %{bin/backbone-generator collection Section::Model}
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
should_generate_collection
end
end
File.read(collection).should match(/SectionModels/)
File.read(collection).should match(%r{section/model})
File.read(spec).should match(/SectionModels/)
describe 'scaffold' do
it "should generate everything!" do
system %{bin/backbone-generator scaffold Section::Model}
should_generate_model
should_generate_view
should_generate_collection
should_generate_collection_view
end
end
end