add a totally improper set of inflection rules for plurals

This commit is contained in:
John Bintz 2011-04-18 11:36:46 -04:00
parent 77210490df
commit 2d6a3b81c9
6 changed files with 63 additions and 22 deletions

View File

@ -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

View File

@ -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

View File

@ -1,4 +1,4 @@
var <%= object_name %>s = Backbone.Collection.extend({
var <%= plural_object_name %> = Backbone.Collection.extend({
url: '/<%= underscore_name %>',
model: <%= object_name %>
});

View File

@ -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();

View File

@ -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');

View File

@ -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');