From d2dcd4569cd04549beaed38985e35ad586cd3174 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 15 Apr 2011 20:43:48 -0400 Subject: [PATCH] add scaffold option --- README.md | 2 +- bin/backbone-generator | 42 +++++++++++---- spec/bin/backbone-generator_spec.rb | 81 +++++++++++++++++++---------- 3 files changed, 87 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 81c6ddc..a22488c 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/bin/backbone-generator b/bin/backbone-generator index 7584916..e7dd806 100755 --- a/bin/backbone-generator +++ b/bin/backbone-generator @@ -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 diff --git a/spec/bin/backbone-generator_spec.rb b/spec/bin/backbone-generator_spec.rb index 7ee81c6..10480c7 100644 --- a/spec/bin/backbone-generator_spec.rb +++ b/spec/bin/backbone-generator_spec.rb @@ -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