Commit ef646f3b by Prem Sichanugrist

Use Paperclip sexy migration in the generator

parent b09bb31b
class <%= migration_class_name %> < ActiveRecord::Migration
def self.up
change_table :<%= table_name %> do |t|
<% attachment_names.each do |attachment| -%>
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string
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
t.has_attached_file :<%= attachment %>
<% end -%>
end
end
def self.down
<% attachment_names.each do |attachment| -%>
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name
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
drop_attached_file :<%= table_name %>, :<%= attachment %>
<% end -%>
end
end
......@@ -18,17 +18,21 @@ class GeneratorTest < Rails::Generators::TestCase
assert_match /class AddAttachmentAvatarToUsers/, migration
assert_class_method :up, migration do |up|
assert_match /add_column :users, :avatar_file_name, :string/, up
assert_match /add_column :users, :avatar_content_type, :string/, up
assert_match /add_column :users, :avatar_file_size, :integer/, up
assert_match /add_column :users, :avatar_updated_at, :datetime/, up
expected = <<-migration
change_table :users do |t|
t.has_attached_file :avatar
end
migration
assert_equal expected.squish, up.squish
end
assert_class_method :down, migration do |down|
assert_match /remove_column :users, :avatar_file_name/, down
assert_match /remove_column :users, :avatar_content_type/, down
assert_match /remove_column :users, :avatar_file_size/, down
assert_match /remove_column :users, :avatar_updated_at/, down
expected = <<-migration
drop_attached_file :users, :avatar
migration
assert_equal expected.squish, down.squish
end
end
end
......@@ -44,25 +48,23 @@ class GeneratorTest < Rails::Generators::TestCase
assert_match /class AddAttachmentAvatarPhotoToUsers/, migration
assert_class_method :up, migration do |up|
assert_match /add_column :users, :avatar_file_name, :string/, up
assert_match /add_column :users, :avatar_content_type, :string/, up
assert_match /add_column :users, :avatar_file_size, :integer/, up
assert_match /add_column :users, :avatar_updated_at, :datetime/, up
assert_match /add_column :users, :photo_file_name, :string/, up
assert_match /add_column :users, :photo_content_type, :string/, up
assert_match /add_column :users, :photo_file_size, :integer/, up
assert_match /add_column :users, :photo_updated_at, :datetime/, up
expected = <<-migration
change_table :users do |t|
t.has_attached_file :avatar
t.has_attached_file :photo
end
migration
assert_equal expected.squish, up.squish
end
assert_class_method :down, migration do |down|
assert_match /remove_column :users, :avatar_file_name/, down
assert_match /remove_column :users, :avatar_content_type/, down
assert_match /remove_column :users, :avatar_file_size/, down
assert_match /remove_column :users, :avatar_updated_at/, down
assert_match /remove_column :users, :photo_file_name/, down
assert_match /remove_column :users, :photo_content_type/, down
assert_match /remove_column :users, :photo_file_size/, down
assert_match /remove_column :users, :photo_updated_at/, down
expected = <<-migration
drop_attached_file :users, :avatar
drop_attached_file :users, :photo
migration
assert_equal expected.squish, down.squish
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