Sha256: 987ad1a5e3a992cbe73486f4e8372eb26baf1b30cf8bdc44ab3ac17c8d05f281
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 KB
Contents
require "rails/generators/migration" require "active_record/migration" # rails g migration:from post link blog photo # rails g migration:from post --add name address module Migration class FromGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) include Rails::Generators::Migration class << self def next_migration_number(dirname) #:nodoc: next_migration_number = current_migration_number(dirname) + 1 ActiveRecord::Migration.next_migration_number(next_migration_number) end end argument :model_names, :type => :array, :default => [], :banner => "model model" class_option :add, :type => :array, :default => [], :banner => "attrname attrname" def preload_models ActiveRecordSchema.autoload_paths.each do |p| load(p) end end def create_migrations models.each do |current_model| @current_model = current_model migration_file_name = "#{migration_prefix}_#{current_model.name.underscore.gsub('::', '_').pluralize}" migration_template "migration_from_model.rb.erb", "db/migrate/#{migration_file_name}.rb" end end protected def model @current_model end def inherits? model.superclass < ActiveRecord::Base end def models @models ||= model_names.map {|name| name.camelize.constantize } end def migration_prefix if options[:add].any? "add_#{options[:add].join('_and_')}_to" else "create" end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
active_record_schema-0.5.5 | lib/generators/migration/from_generator.rb |