Skip to content

Commit

Permalink
Add command recorder for our migrations
Browse files Browse the repository at this point in the history
This is for Rails 3.1+
  • Loading branch information
sikachu committed Apr 23, 2012
1 parent 0a31c85 commit c438a48
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
24 changes: 24 additions & 0 deletions features/migration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 6 additions & 0 deletions features/step_definitions/rails_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions lib/paperclip/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.