Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add command recorder for our migrations

This is for Rails 3.1+
  • Loading branch information...
commit 77ee8a51144fe4885d5a0f22ce49b1aae25dc879 1 parent 76cbfb8
@sikachu sikachu authored
View
24 features/migration.feature
@@ -68,3 +68,27 @@ Feature: Migration
When I rollback a migration
Then I should not have attachment columns for "avatar"
+
+ Scenario: Rails 3.2 change method
+ Given I am using Rails newer than 3.1
+ When I write to "db/migrate/01_create_users.rb" with:
+ """
+ class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table :users
+ end
+ end
+ """
+ When I write to "db/migrate/02_add_attachment_to_users.rb" with:
+ """
+ class AddAttachmentToUsers < ActiveRecord::Migration
+ def change
+ add_attachment :users, :avatar
+ end
+ end
+ """
+ And I run a migration
+ Then I should have attachment columns for "avatar"
+
+ When I rollback a migration
+ Then I should not have attachment columns for "avatar"
View
6 features/step_definitions/rails_steps.rb
@@ -130,3 +130,9 @@
When /^I comment out the gem "([^"]*)" from the Gemfile$/ do |gemname|
comment_out_gem_in_gemfile gemname
end
+
+Given /^I am using Rails newer than ([\d\.]+)$/ do |version|
+ if framework_version < version
+ pending "Not supported in Rails < #{version}"
+ end
+end
View
16 lib/paperclip/schema.rb
@@ -12,6 +12,10 @@ def self.included(base)
ActiveRecord::ConnectionAdapters::Table.send :include, TableDefinition
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, TableDefinition
ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, Statements
+
+ if defined?(ActiveRecord::Migration::CommandRecorder) # Rails 3.1+
+ ActiveRecord::Migration::CommandRecorder.send :include, CommandRecorder
+ end
end
module Statements
@@ -55,5 +59,17 @@ def has_attached_file(*attachment_names)
attachment(*attachment_names)
end
end
+
+ module CommandRecorder
+ def add_attachment(*args)
+ record(:add_attachment, args)
+ end
+
+ private
+
+ def invert_add_attachment(args)
+ [:remove_attachment, args]
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.