Skip to content
Browse files

Add command recorder for our migrations

This is for Rails 3.1+
  • Loading branch information...
1 parent 0a31c85 commit c438a484f3d8a7ebfe54e32836d3d4f310c69382 @sikachu sikachu committed Apr 23, 2012
Showing with 46 additions and 0 deletions.
  1. +24 −0 features/migration.feature
  2. +6 −0 features/step_definitions/rails_steps.rb
  3. +16 −0 lib/paperclip/schema.rb
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
@@ -122,3 +122,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

0 comments on commit c438a48

Please sign in to comment.
Something went wrong with that request. Please try again.