rails-generators
rails-generators copied to clipboard
Rails Application Templates for Marionette Projects and Generators
Rails Application Templates / Generators for Marionette
After developing conventions for working with Backbone/Marionette, I wrote my own rails generators and application template. These remove a lot of unnecessary files, and sets up sensible defaults for working with Backbone/Marionette projects.
Using the Application Template
The marionette-template.rb is an application template used when creating a new rails application. To use this application template, in the terminal type:
rails new APPNAME -m https://raw.github.com/brian-mann/rails-generators/master/marionette-template.rb
What this template does
Ruby Version
- Sets Ruby 1.9.3 in Gemfile
Miscellaneous File Cleanup
- Removes default
app/assets/images/rails.pngimage - Removes default
public/index.htmlfile - Renames
README.rdoctoREADME.md
File Permissions
- chmod's log directory to 0777
- TODO: add db directory
Routing
- Sets default route to:
application#index
App Views
- Creates
app/views/applicationdirectory - Creates
app/views/application/index.html.erbfile
App Stylesheets
- Creates
app/assets/stylesheets/appsdirectory - Creates
app/assets/stylesheets/app/_base.css.scssfile - Renames
app/assets/stylesheets/application.csstoapplication.css.scss - Removes
require selfandrequire tree .fromapplication.css.scssfile - Imports
twitter/bootstrapandapps/_baseSCSS files
APP Javascripts
- Removes
jquery_ujsandrequire_tree .fromapp/assets/javascripts/application.jsfile
Included Gems
- thin
- rabl
- oj
- gon
- js-routes
- eco
- compass-rails
- bootstrap-sass-rails
TODO:
- Document additional template actions
Using the Marionette Generators
To access these generators, copy the generators folder into your lib directory. Now run the install command below:
Install all the preliminaries
rails g marionette:install
Installation will do the following:
- Configure the client side application name
- Pull in the latest versions of Backbone / Underscore / Marionette
- Create the initial directory structure for apps / views / components / controllers / mixins / config
- Require the load order and directories via the Asset Pipeline
- Create the Marionette Application instance file:
app.js.coffee - Invokes
rails g marionette:configby default
Configure Marionette
rails g marionette:config
Configuration will do the follow:
- Override Marionette Renderer for JST Eco files
- Create Base Entities for Models and Collections
- Create a Base Controller for automating regions and cleanup
- Create Base Views for view / itemview / layout / collectionview / compositeview
config/initializers/marionette.rb still store your answers for use when other generators run.
Create Marionette Modules
rails g marionette:module MODULENAME [controllers]
This will create a module_app file and initial directory with corresponding AppRoutes for the passed in controller actions. This will also automatically generate the controllers, views, and initial layout template. Use this command to scaffold out your initial app modules.
Example: rails g marionette:module Users list show would create:
- /users
- users_app.js.coffee
- /list
- list_controller.js.coffee
- list_view.js.coffee
- /templates
- list_layout.jst.eco
- /show
- show_controller.js.coffee
- show_view.js.coffee
- /templates
- show_layout.jst.eco
Create Marionette Controllers
rails g marionette:controller MODULENAME [controllers]
This creates the same controller/view/template combination as above without creating the initial module_app file. Use this to add controllers to an existing app module.
Create Model / Collection Entities
rails g marionette:entity MODELNAME
This will create a model, and ask to also create the corresponding collection along with the App.request handlers for returning the instances.