Pizza Generators
A collection of useful Rails generator scripts.
NOTE This is a fork of Ryan Bates’ Nifty Generators. It has been altered so it fits the plugins I use. Use at your own risk!
Install
gem install iain-pizza-generators
Why Pizza?
Here is what is Pizza compared to Nifty:
Done
Global
-
Haml is default for pizza_scaffold, use
--erb
to switch back to erb -
usage of the pizza form builder (from github.com/iain/pizza-form_builder)
-
Using Shoulda matchers in RSpec (dev.thoughtbot.com/shoulda/)
-
Using RR as mocking framework (github.com/btakita/rr)
pizza_scaffold
-
definition_list_for (from github.com/iain/model_based_html)
-
table_for (from github.com/iain/model_based_html)
-
Rails i18n and supposes Mr. T (from github.com/iain/mr.-t)
-
WillPaginate (from github.com/mislav/will_paginate)
-
Turn of XML respond to blocks by using the
--no-xml
option -
Scaffolded RSpec actions are Heckle proof. A lot of tests, using the Shoulda matchers.
pizza_layout
-
reset.sass for layout will keep your view tidy
-
A main menu was added in app/views/shared/_menu.html.haml
-
Added WillPaginate.translate! to support i18n (see below)
pizza_authentication
-
AuthLogic usage with pizza_authentication (from github.com/binarylogic/authlogic)
-
Added gem dependencies to pizza-generators (my first try, so hopefully that works)
Todo
-
Add an internet only stylesheet and iepngfix.htc to pizza_layout
-
Add message_block gem (from github.com/railsgarden/message_block)
-
Use a FactoryGirl instead of fixtures
-
Cucumber stories for RESTful controller in scaffold
-
Update ERB, Test::Unit and Shoulda all over the place (I hardly touched those)
-
Do something with Rails shiny new feature: Templates
-
Make the required plugins gems so they can be a gem dependency (not sure though)
No longer in it
-
ResourceController (from github.com/giraffesoft/resource_controller)
-
Provides a cancel_link helper
-
A cancel link for every form
Setting up
Before doing anything, add these lines to config/environment.rb
config.gem "haml"
config.gem "authlogic"
config.gem "mislav-will_paginate", :lib => "will_paginate", :source => "http://gems.github.com"
And in config/environments/test.rb
config.gem "rspec", :lib => false, :version => ">= 1.2.2"
config.gem "rspec-rails", :lib => false, :version => ">= 1.2.2"
config.gem "rr", :lib => false, :version => ">= 0.8.1"
config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :version => ">= 1.2.0", :source => "http://gems.github.com"
config.gem "thoughtbot-shoulda", :lib => "shoulda", :version => ">= 2.10.1", :source => "http://gems.github.com"
Then run these commands:
sudo rake gems:install # this should have already been done via the gem dependencies
sudo rake gems:install RAILS_ENV=test
rake gems:unpack
./script/generate rspec
Install plugins:
./script/plugin install git://github.com/iain/fieldset_helper.git
./script/plugin install git://github.com/iain/mr.-t.git
./script/plugin install git://github.com/iain/model_based_html.git
./script/plugin install git://github.com/iain/i18n_label.git
Configure rspec to use rr, in spec/spec_helper.rb
config.mock_with :rr
Generate layout (required for all other generators):
./script/generate pizza_layout
Add the layout helper to be included every where, in app/controllers/application.rb
helper :layout
Enable the cancel_link helper by adding code described below.
Generate authentication (optional):
./script/generate pizza_authentication
Edit config/routes.rb
to singularize the user_session resource:
map.resource :user_session
And add a root route:
map.root :login # for example
Edit spec/spec_helper.rb
for authentication testing:
require 'authlogic/testing/test_unit_helpers'
Inspect the generated migration for creating the users table, to enable or disable any attributes that you do or don’t want.
Run rake db:migrate
And see if the specs run: rake spec
Usage
Once you install the gem, the generators will be available to all Rails applications on your system. If you run script/generate without any additional arguments you should see the available generators listed.
To run the generator, go to your rails project directory and call it using the script/generate or script/destroy command.
script/generate pizza_scaffold Recipe name:string index new
Included Generators
-
pizza_layout: generates generic layout, stylesheet, and helper files.
-
pizza_scaffold: generates a controller and optional model/migration.
-
pizza_config: generates a config YAML file and loader.
-
pizza_authentication: generates user model with sign up and log in.
Run the command with the --help
option to learn more.
script/generate pizza_layout --help
Troubleshooting
Namespaced controllers howto
Just scaffold a RescourceController. Move the controller and views to it’s desired subfolders and add the namespace to the name of the class, e.g. Admin::PostsController. Configure your routes and you’re done!
I get “undefined method ‘title’” error.
Try running pizza_layout, that will generate this helper method. Or you can just change the templates to whatever approach you prefer for setting the title.
I get “undefined method ‘root_url’” error.
Some generators default redirecting to the root_url. Set this in your routes.rb file like this (substituting your controller name).
map.root :controller => 'foo'
I get a missing database error.
Run rake db:migrate
.
Forms don’t work.
Try restarting your development server. Sometimes it doesn’t detect the change in the routing.
I can’t set new attributes in my User model.
Add the attribute to the attr_accessible line in the model.
The tests/specs don’t work.
Make sure you have mocha installed and require it in your spec/test helper.
gem install mocha
# in spec_helper.rb
config.mock_with :mocha
# in test_helper.rb
require 'mocha'
Also, make sure you’re using Rails 2.2.2 or greater.
How to use the cancel_link?
Add this code to your ApplicationController:
before_filter :remember_back
private
def remember_back
session[:back] = request.referrer if request.get?
end
Translating will_paginate
Pizza_layout adds an easy way to translate will_paginate. It will automatically be called when Rails loads.
Your translations should look something like this:
en:
pagination:
class: "pagination"
previous_label: "« Previous"
next_label: "Next »"
inner_window: 4 # links around the current_page
outer_window: 1 # links around beginning and end
separator: ' '
param_name: :page
params: ~
renderer: "WillPaginate::LinkRenderer"
page_links: true
container: true
The initializer will use the properties from the default_locale. Any other language just needs to define their own exceptions to this. So if :en is the default locale and you would switch to :nl, you’d only need to do:
nl:
pagination:
previous_label: "« Vorige"
next_label: "Volgende »"
When you support switching locales, you need to call WillPaginate.translate! everytime you switch locales. This can be done by adding to your ApplicationController:
before_filter :set_locale
private
def set_locale
I18n.locale = params[:locale]
WillPaginate.translate!
end
For more information, read mislav’s code on github: github.com/mislav/will_paginate/blob/master/lib/will_paginate/view_helpers.rb
Development
This project can be found on github at the following URL.
github.com/iain/pizza-generators and github.com/ryanb/nifty-generators
If you wish the generators behaved differently, please consider forking the project and modifying to your heart’s content. (I did that! - Iain)