Commit ef646f3b by Prem Sichanugrist

Use Paperclip sexy migration in the generator

parent b09bb31b
class <%= migration_class_name %> < ActiveRecord::Migration class <%= migration_class_name %> < ActiveRecord::Migration
def self.up def self.up
change_table :<%= table_name %> do |t|
<% attachment_names.each do |attachment| -%> <% attachment_names.each do |attachment| -%>
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string t.has_attached_file :<%= attachment %>
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_size, :integer
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at, :datetime
<% end -%> <% end -%>
end end
end
def self.down def self.down
<% attachment_names.each do |attachment| -%> <% attachment_names.each do |attachment| -%>
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name drop_attached_file :<%= table_name %>, :<%= attachment %>
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_size
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at
<% end -%> <% end -%>
end end
end end
...@@ -18,17 +18,21 @@ class GeneratorTest < Rails::Generators::TestCase ...@@ -18,17 +18,21 @@ class GeneratorTest < Rails::Generators::TestCase
assert_match /class AddAttachmentAvatarToUsers/, migration assert_match /class AddAttachmentAvatarToUsers/, migration
assert_class_method :up, migration do |up| assert_class_method :up, migration do |up|
assert_match /add_column :users, :avatar_file_name, :string/, up expected = <<-migration
assert_match /add_column :users, :avatar_content_type, :string/, up change_table :users do |t|
assert_match /add_column :users, :avatar_file_size, :integer/, up t.has_attached_file :avatar
assert_match /add_column :users, :avatar_updated_at, :datetime/, up end
migration
assert_equal expected.squish, up.squish
end end
assert_class_method :down, migration do |down| assert_class_method :down, migration do |down|
assert_match /remove_column :users, :avatar_file_name/, down expected = <<-migration
assert_match /remove_column :users, :avatar_content_type/, down drop_attached_file :users, :avatar
assert_match /remove_column :users, :avatar_file_size/, down migration
assert_match /remove_column :users, :avatar_updated_at/, down
assert_equal expected.squish, down.squish
end end
end end
end end
...@@ -44,25 +48,23 @@ class GeneratorTest < Rails::Generators::TestCase ...@@ -44,25 +48,23 @@ class GeneratorTest < Rails::Generators::TestCase
assert_match /class AddAttachmentAvatarPhotoToUsers/, migration assert_match /class AddAttachmentAvatarPhotoToUsers/, migration
assert_class_method :up, migration do |up| assert_class_method :up, migration do |up|
assert_match /add_column :users, :avatar_file_name, :string/, up expected = <<-migration
assert_match /add_column :users, :avatar_content_type, :string/, up change_table :users do |t|
assert_match /add_column :users, :avatar_file_size, :integer/, up t.has_attached_file :avatar
assert_match /add_column :users, :avatar_updated_at, :datetime/, up t.has_attached_file :photo
assert_match /add_column :users, :photo_file_name, :string/, up end
assert_match /add_column :users, :photo_content_type, :string/, up migration
assert_match /add_column :users, :photo_file_size, :integer/, up
assert_match /add_column :users, :photo_updated_at, :datetime/, up assert_equal expected.squish, up.squish
end end
assert_class_method :down, migration do |down| assert_class_method :down, migration do |down|
assert_match /remove_column :users, :avatar_file_name/, down expected = <<-migration
assert_match /remove_column :users, :avatar_content_type/, down drop_attached_file :users, :avatar
assert_match /remove_column :users, :avatar_file_size/, down drop_attached_file :users, :photo
assert_match /remove_column :users, :avatar_updated_at/, down migration
assert_match /remove_column :users, :photo_file_name/, down
assert_match /remove_column :users, :photo_content_type/, down assert_equal expected.squish, down.squish
assert_match /remove_column :users, :photo_file_size/, down
assert_match /remove_column :users, :photo_updated_at/, down
end end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment