Skip to content

Instantly share code, notes, and snippets.

@jhirn
Last active December 18, 2015 16:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhirn/5811467 to your computer and use it in GitHub Desktop.
Save jhirn/5811467 to your computer and use it in GitHub Desktop.
Generator for making a backbone model. Original author @daytonn
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
App.Models.<%= name.camelize %> = Backbone.Model.extend({
urlRoot: '/<%= url %>'
});
App.Collections.<%= name.pluralize.camelize %> = Backbone.Collection.extend({
url: '/<%= url %>',
model: App.Models.<%= name.camelize %>
});
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