Module: Rails::Generators::Migration
- Defined in:
- lib/rails/generators/migration.rb
Overview
Holds common methods for migrations. It assumes that migrations has the [0-9]*_name format and can be used by another frameworks (like Sequel) just by implementing the next migration version method.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#migration_class_name ⇒ Object
readonly
Returns the value of attribute migration_class_name.
-
#migration_file_name ⇒ Object
readonly
Returns the value of attribute migration_file_name.
-
#migration_number ⇒ Object
readonly
Returns the value of attribute migration_number.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #create_migration(destination, data, config = {}, &block) ⇒ Object
-
#migration_template(source, destination, config = {}) ⇒ Object
Creates a migration template at the given destination.
- #set_migration_assigns!(destination) ⇒ Object
Instance Attribute Details
#migration_class_name ⇒ Object (readonly)
Returns the value of attribute migration_class_name.
9 10 11 |
# File 'lib/rails/generators/migration.rb', line 9 def migration_class_name @migration_class_name end |
#migration_file_name ⇒ Object (readonly)
Returns the value of attribute migration_file_name.
9 10 11 |
# File 'lib/rails/generators/migration.rb', line 9 def migration_file_name @migration_file_name end |
#migration_number ⇒ Object (readonly)
Returns the value of attribute migration_number.
9 10 11 |
# File 'lib/rails/generators/migration.rb', line 9 def migration_number @migration_number end |
Class Method Details
.included(base) ⇒ Object
:nodoc:
11 12 13 |
# File 'lib/rails/generators/migration.rb', line 11 def self.included(base) #:nodoc: base.extend ClassMethods end |
Instance Method Details
#create_migration(destination, data, config = {}, &block) ⇒ Object
35 36 37 |
# File 'lib/rails/generators/migration.rb', line 35 def create_migration(destination, data, config = {}, &block) action Rails::Generators::Actions::CreateMigration.new(self, destination, block || data.to_s, config) end |
#migration_template(source, destination, config = {}) ⇒ Object
Creates a migration template at the given destination. The difference to the default template method is that the migration version is appended to the destination file name.
The migration version, migration file name, migration class name are available as instance variables in the template to be rendered.
migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb"
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rails/generators/migration.rb', line 56 def migration_template(source, destination, config = {}) source = File.(find_in_source_paths(source.to_s)) set_migration_assigns!(destination) context = instance_eval('binding') dir, base = File.split(destination) numbered_destination = File.join(dir, ["%migration_number%", base].join('_')) create_migration numbered_destination, nil, config do ERB.new(::File.binread(source), nil, '-', '@output_buffer').result(context) end end |
#set_migration_assigns!(destination) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/rails/generators/migration.rb', line 39 def set_migration_assigns!(destination) destination = File.(destination, self.destination_root) migration_dir = File.dirname(destination) @migration_number = self.class.next_migration_number(migration_dir) @migration_file_name = File.basename(destination, '.rb') @migration_class_name = @migration_file_name.camelize end |