The base generator for named components: models, controllers, mailers, etc. The target name is taken as the first argument and inflected to singular, plural, class, file, and table forms for your convenience. The remaining arguments are aliased to actions as an array for controller and mailer convenience.
Several useful local variables and methods are populated in the initialize method. See below for a list of Attributes and External Aliases available to both the manifest and to all templates.
If no name is provided, the generator raises a usage error with content optionally read from the USAGE file in the generator‘s base path.
For example, the controller generator takes the first argument as the name of the class and subsequent arguments as the names of actions to be generated:
./script/generate controller Article index new create
See Rails::Generator::Base for a discussion of manifests, Rails::Generator::Commands::Create for methods available to the manifest, and Rails::Generator for a general discussion of generators.
[R] | class_name | |
[R] | class_nesting | |
[R] | class_nesting_depth | |
[R] | class_path | |
[R] | file_path | |
[R] | name | |
[R] | plural_name | |
[R] | singular_name | |
[R] | table_name |
[ show source ]
# File railties/lib/rails_generator/base.rb, line 206 206: def initialize(runtime_args, runtime_options = {}) 207: super 208: 209: # Name argument is required. 210: usage if runtime_args.empty? 211: 212: @args = runtime_args.dup 213: base_name = @args.shift 214: assign_names!(base_name) 215: end
[ show source ]
# File railties/lib/rails_generator/base.rb, line 223 223: def attributes 224: @attributes ||= @args.collect do |attribute| 225: Rails::Generator::GeneratedAttribute.new(*attribute.split(":")) 226: end 227: end
Override with your own usage banner.
[ show source ]
# File railties/lib/rails_generator/base.rb, line 219 219: def banner 220: "Usage: #{$0} #{spec.name} #{spec.name.camelize}Name [options]" 221: end