diff --git a/bin/backbone-generator b/bin/backbone-generator index 3cbb1d1..5d4d426 100755 --- a/bin/backbone-generator +++ b/bin/backbone-generator @@ -13,11 +13,19 @@ class BackboneGenerator < Thor no_tasks do def underscore_name - Thor::Util.snake_case(@name.gsub("::", "/")) + singularize(Thor::Util.snake_case(@name.gsub("::", "/"))) + end + + def plural_underscore_name + pluralize(underscore_name) end def object_name - @name.gsub('::', '') + singularize(@name.gsub('::', '')) + end + + def plural_object_name + pluralize(object_name) end def generate_model @@ -32,14 +40,14 @@ class BackboneGenerator < Thor 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") + template('collection.js.erb', "public/javascripts/collections/#{plural_underscore_name}.js") + template('collection_spec.js.erb', "spec/javascripts/collections/#{plural_underscore_name}_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/list.jst") - template('collection_view_spec.js.erb', "spec/javascripts/views/#{underscore_name}s_view_spec.js") + template('collection_view.js.erb', "public/javascripts/views/#{plural_underscore_name}_view.js") + template('collection_view.jst.erb', "app/views/#{plural_underscore_name}/list.jst") + template('collection_view_spec.js.erb', "spec/javascripts/views/#{plural_underscore_name}_view_spec.js") end end @@ -77,6 +85,15 @@ class BackboneGenerator < Thor def spec_helper template('spec_helper.js.erb', 'spec/javascripts/helpers/backbone_spec_helper.js') end + + private + def pluralize(string) + singularize(string) + 's' + end + + def singularize(string) + string.gsub(%r{s$}, '') + end end BackboneGenerator.start diff --git a/spec/bin/backbone-generator_spec.rb b/spec/bin/backbone-generator_spec.rb index ee0d15b..210613d 100644 --- a/spec/bin/backbone-generator_spec.rb +++ b/spec/bin/backbone-generator_spec.rb @@ -35,8 +35,10 @@ describe 'backbone-generator' do File.file?(spec = 'spec/javascripts/collections/section/models_spec.js').should be_true File.read(collection).should match(/SectionModels/) + File.read(collection).should_not match(/SectionModelss/) File.read(collection).should match(%r{section/model}) File.read(spec).should match(/SectionModels/) + File.read(spec).should_not match(/SectionModelss/) end def should_generate_collection_view @@ -45,10 +47,12 @@ describe 'backbone-generator' do File.file?(template = 'app/views/section/models/list.jst').should be_true File.read(view).should match(/SectionModelsView/) + File.read(view).should_not match(/SectionModelssView/) File.read(view).should match(/SectionModelView/) File.read(view).should match(/return this/) File.read(view).should match(%r{template: JST\['section/models/list'\]}) File.read(spec).should match(/SectionModelsView/) + File.read(spec).should_not match(/SectionModelssView/) end describe 'model' do @@ -68,18 +72,38 @@ describe 'backbone-generator' do end describe 'collection view' do - it "should generate the collection view files" do - system %{bin/backbone-generator collection-view Section::Model} + context 'without trailing s' do + it "should generate the collection view files" do + system %{bin/backbone-generator collection-view Section::Model} - should_generate_collection_view + should_generate_collection_view + end + end + + context 'with trailing s' do + it "should generate the collection view files" do + system %{bin/backbone-generator collection-view Section::Models} + + should_generate_collection_view + end end end describe 'collection' do - it "should generate the collection files" do - system %{bin/backbone-generator collection Section::Model} + context 'without trailing s' do + it "should generate the collection files" do + system %{bin/backbone-generator collection Section::Model} - should_generate_collection + should_generate_collection + end + end + + context 'with trailing s' do + it "should generate the collection files" do + system %{bin/backbone-generator collection Section::Models} + + should_generate_collection + end end end diff --git a/templates/collection.js.erb b/templates/collection.js.erb index 1ed3bbf..8aa5c10 100644 --- a/templates/collection.js.erb +++ b/templates/collection.js.erb @@ -1,4 +1,4 @@ -var <%= object_name %>s = Backbone.Collection.extend({ +var <%= plural_object_name %> = Backbone.Collection.extend({ url: '/<%= underscore_name %>', model: <%= object_name %> }); diff --git a/templates/collection_spec.js.erb b/templates/collection_spec.js.erb index e58d77c..bc3379c 100644 --- a/templates/collection_spec.js.erb +++ b/templates/collection_spec.js.erb @@ -1,12 +1,12 @@ -describe('<%= object_name %>s', function() { +describe('<%= plural_object_name %>', function() { var collection; withServer(); it('should fetch records from the API', function() { - collection = new <%= object_name %>s(); + collection = new <%= plural_object_name %>(); - this.server.respondWith('GET', '<%= underscore_name %>', this.validJSONResponse([{id: 1}])); + this.server.respondWith('GET', '<%= plural_underscore_name %>', this.validJSONResponse([{id: 1}])); collection.fetch() this.server.respond(); diff --git a/templates/collection_view.js.erb b/templates/collection_view.js.erb index aadedb9..43e0b47 100644 --- a/templates/collection_view.js.erb +++ b/templates/collection_view.js.erb @@ -1,5 +1,5 @@ -var <%= object_name %>sView = Backbone.View.extend({ - template: JST['<%= underscore_name %>s/list'], +var <%= plural_object_name %>View = Backbone.View.extend({ + template: JST['<%= plural_underscore_name %>/list'], initialize: function(collection) { _.bindAll(this, 'render', 'addOne', 'addAll'); diff --git a/templates/collection_view_spec.js.erb b/templates/collection_view_spec.js.erb index 5469bf6..b625ef9 100644 --- a/templates/collection_view_spec.js.erb +++ b/templates/collection_view_spec.js.erb @@ -1,12 +1,12 @@ -describe('<%= object_name %>sView', function() { +describe('<%= plural_object_name %>View', function() { var view, collection; beforeEach(function() { - collection = new <%= object_name %>s(); + collection = new <%= plural_object_name %>(); }); it('should render', function() { - view = new <%= object_name %>sView(collection); + view = new <%= plural_object_name %>View(collection); view.render(); expect($(view.el)).toContain('.list');