Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module ActiveRecord
module Generators
class ModelGenerator < Base
# Line below is optional. If the generator name would be :model, the name
# below could be guessed.
#
# Generators are also public by default. If they are private it can be
# invoked only by other generators.
#
name :activerecord, :private => false
# This argument is required and it can be given as a positional argument
# the generator could be invoked like this:
#
# script/generate model --name User
# script/generate model User
#
argument :name do |a|
a.description = "Name of the generator"
a.validate {|name| name !~ /\s/ }
a.filter {|name| name.camelize! }
end
# This argument can be given as a hash:
#
# script/generate model User name:string email:string
#
# No more required arguments can be given after it.
#
argument :attributes, :hash => true, :description => "Model attributes"
before :check_class_collisions
recipe :model do
directory File.join("app/models", class_path)
template 'model.rb', File.join("app/models", class_path, "#{file_name}.rb")
end
recipe :migration do
directory File.join("db", "migrate")
migration class_underscore_name, File.join("db", "migrate"), :up => up_content_for_migration, :down => down_content_for_migration
end
protected
def up_content_for_migration
# ...
end
def down_content_for_migration
# ...
end
end
# Register this generator. It can be invoked in two ways:
#
# ActiveRecord::Generators::ModelGenerator.call(options)
#
# Or with the alias name, using templater interface:
#
# Templater::Generators[:activerecord].call(options)
#
add :activerecord, ModelGenerator
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.