Last active
December 18, 2015 16:29
-
-
Save jhirn/5811467 to your computer and use it in GitHub Desktop.
Generator for making a backbone model. Original author @daytonn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BmodelGenerator < Rails::Generators::NamedBase | |
source_root File.expand_path('../templates', __FILE__) | |
argument :url, type: :string, default: 'url-goes-here' | |
def generate_view | |
template "model.js.erb", File.join('app', 'assets', 'javascripts', 'models', "#{file_name}.js") | |
%x{echo //= require models/#{file_name} | pbcopy} | |
%x{#{ENV['EDITOR']} #{File.join('app', 'assets', 'javascripts', 'application.js')}} | |
end | |
private | |
def file_name | |
name.underscore | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Models.<%= name.camelize %> = Backbone.Model.extend({ | |
urlRoot: '/<%= url %>' | |
}); | |
App.Collections.<%= name.pluralize.camelize %> = Backbone.Collection.extend({ | |
url: '/<%= url %>', | |
model: App.Models.<%= name.camelize %> | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Description: | |
Creates a backbone view file with the model and collection defined within. | |
Also will open application.js manifest in EDITOR with the require statement ready in the clipboard. | |
Example: | |
rails generate bmodel Modelname | |
This will create: | |
app/asssets/javascripts/models/thing.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment