Permalink
Browse files

Use Paperclip sexy migration in the generator

  • Loading branch information...
sikachu committed Apr 12, 2012
1 parent b09bb31 commit ef646f3bdf64efc3111ee7afc357def028e59413
Showing with 30 additions and 32 deletions.
  1. +4 −8 lib/generators/paperclip/templates/paperclip_migration.rb.erb
  2. +26 −24 test/generator_test.rb
@@ -1,19 +1,15 @@
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
View
@@ -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

0 comments on commit ef646f3

Please sign in to comment.