Creating Rails Generator For Model/View/Controller/Mailer - ruby-on-rails

I have an app that's heavily form-based. Many of the forms are constructed exactly alike so it seems like a natural fit for a generator. What I want to do is create one that works like this (fictitious example):
rails g request_form name:string phone:string date_of_birth:date
In any case, the standard, empty controllers, helpers, models, and so on won't quite do. I've read the Rails code but it's, frankly, not been a heck of a lot of help in puzzling this out. What I want to do specifically is:
Create a model and migration given the fields specified on the command line
Create a controller and helper, based on my template
Create views based on my templates
Create empty specs
Create a mailer based on my template
Create mailer views based on my templates
I'm getting stalled at square 1: How the heck do I get the ARGV part of the rails g command -- that is, the field names? Then there's square 2: How do I hook into the built-in generators, where appropriate and fill in my own stuff where not?
This is analogous to
rails g scaffold blah:type blah1:type
so I don't think this is biting off more than I can chew...
Any help mucho appreciated!

All inspiration needed in this great gem: https://github.com/ryanb/nifty-generators

The awesome Ryan Bates has a screencast on writing generators in Rails 3, have you watched that?

Related

there are some way to create a custom scaffold or generator on ruby on rails

I wish to create my custom generator, some like run the command "rails generator myScaffold entity field1:String field2:String
and the generator behavior like a normal scaffold, except it will not create the stylesshets, views, and the class of the controller would be customized.
Is it posible with rails? And is it a good and correct thing to do?
thanks!!!
All you need is available here http://guides.rubyonrails.org/generators.html

Having default Rails generators call a custom generator

To be clear, here's NOT what I'm trying to:
Have my custom generator call a default Rails generator
Replace a default Rails generator with my own
What I want to do is have my generator be invoked automatically when I call:
rails generate scaffold User name age:integer
I'm not writing a test replacement or anything, it's completely custom. All information I find about generators out there involve one of those first two cases but not what I want to do. As soon as I found hook_for I immediately thought that was exactly what I needed, but it appears to do the opposite -- invokes another Rails generator from inside of my custom one (if I wanted a test file created for my custom generator I'd call hook_for :test_framework and then define a TestUnit::MyCustomGenerator class somewhere).
I suppose I could monkey patch the default scaffold generator to call mine but that feels dirty. I've looked into some gems that do something similar like https://github.com/Skalar/i18n-yaml-generator but trying to convert that to use an initializer and lib/generators isn't working for me. The scaffold_generator runs but mine never gets called.
for me it works from lib/generators/
$ rails g generator scaffold
create lib/generators/scaffold
create lib/generators/scaffold/scaffold_generator.rb
create lib/generators/scaffold/USAGE
create lib/generators/scaffold/templates
$ rails g scaffold
Usage:
rails generate scaffold NAME [options]
....
what/will/it/create
http://guides.rubyonrails.org/generators.html#generators-lookup
another way may be :)
fork railties gem
override Rails::Generators::ScaffoldGenerator class to your liking
install locally or specify source path in Gemfile
also if 'its completely custom', it is just fair to call it a different name, no?

Rails 3 generate scaffold for specific actions only

My setup: Rails 3.0.9, Ruby 1.9.2
I wish to generate a scaffold only for the create action, what's the syntax for that? I'm guessing it's something I append to
rails g scaffold Project name:string ...
I don't believe you can. I'm looking at the generator and I see nothing related to using an option to limit the template.
Generator
Template
Rails scaffolding is a very basic tool, it's not meant to be relied on, once you get a handle on the way rails works. However, you can use Ryan Bates' nifty-generator gem to generate scaffolds with greater control. Example:
rails g nifty:scaffold post name:string index new edit
Learn more here. https://github.com/ryanb/nifty-generators

rails g scaffold series name:string - is this a naming convention error or something else

On my blog I'm have posts that belong to a series. I've tried to scaffold series but there are some problems with routes.
The pluralization engine doesn't get it right so I had to manually change Sery, #series, and #sery which is not a big deal.
The routing seems to be ok with resources :series. But then when I try to create a series the form_for helper complains about the route.
And then when I create it with console it works but rails is still complaining about routes.
Please create a simple app and see what the problem is.
rails new test_series_app
And then run the scaffold generator:
rails g scaffold series name:string
And see how the routes are getting mixed up and help me out please!
For the record, I put the singularize code into the scaffold generator (yes, my one contribution to Rails). All it does is check if record_name == record_name.pluralize. If it does and you haven't passed in --force-plural, it calls record_name = record_name.singularize.
In this case "series".pluralize is the same as "series".singularize so I assume it won't do anything.
So if you're having problems w/ it, you need to write an inflector for the word.
(I wrote it after Jeremy Kemper's 2008 RailsConf keynote in which he accidentally passed in a plural model name causing himself all sorts of grief in the middle of his talk.)

Rails: What is the convention for controllers with many words

Say I have the model CourseGroup. What would be the controller's name?
The controller name would be course_groups_controller.
http://itsignals.cascadia.com.au/?p=7
To find the name for any model, you can open up a rails console and do "ModelName".tableize. Then just add "_controller" to the end. This would result in model_names_controller.
Here's an easy way to find out the naming conventions: Just create a throw-away Rails app in a temp directory, with a scaffolded model:
rails blog
cd blog
./script/generate scaffold post subject:string content:text
You can then browse through the files and directories to see how things are named. I like to keep one of these around just to refer to from time to time. And by the way, running the generators without any parameters gives help output which includes examples of naming conventions:
./script/generate scaffold

Resources