Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

properly generate slug migration

  • Loading branch information...
commit b8dd6285052c02b61e686e2deb5c4ba294a96370 1 parent b0f2c3c
Brent Vatne authored
View
25 lib/generators/banyan/banyan_generator.rb
@@ -14,8 +14,27 @@ def self.next_migration_number(path)
ActiveRecord::Generators::Base.next_migration_number(path)
end
- def generate_migration
- migration_template 'migration.rb', 'db/migrate/create_banyan_tables'
- migration_template 'slug_migration.rb', 'db/migrate/add_banyan_slugs'
+ # Public: Creates the migration files from the templates in /templates
+ #
+ # This, along with any other public instance method on this class, will be
+ # called when `rails g banyan` is executed. Order matters in migrations! Tables
+ # need to be created before slugs can be added to them; keep this in mind
+ # if you are going to add any.
+ def create_migrations
+ migrations = ['create_banyan_tables', 'add_banyan_slugs']
+
+ migrations.each do |migration_name|
+ create_migration_unless_it_exists(migration_name)
+ end
+ end
+
+ private
+
+ def create_migration_unless_it_exists(migration_name)
+ begin
+ migration_template "#{migration_name}.rb", "db/migrate/#{migration_name}"
+ rescue StandardError => e
+ puts e.message
+ end
end
end
View
3  lib/generators/banyan/templates/slug_migration.rb → lib/generators/banyan/templates/add_banyan_slugs.rb
@@ -5,6 +5,9 @@ def self.up
add_column :banyan_categories, :slug, :string
add_index :banyan_categories, :slug, :unique => true
+
+ Banyan::CategoryGroup.find_each(&:save)
+ Banyan::Category.find_each(&:save)
end
def self.down
View
0  lib/generators/banyan/templates/migration.rb → ...nerators/banyan/templates/create_banyan_tables.rb
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.