Skip to content

Commit

Permalink
Add Rake task to rename Bogus gateways
Browse files Browse the repository at this point in the history
Running

    rake solidus:migrations:rename_gateways:up

helps migrating your data to new bogus payment method class names.

    rake solidus:migrations:rename_gateways:down

reverts this.

Also includes a migration that invokes that task, so you don't need to care when deploying this change.
  • Loading branch information
tvdeyen committed Jun 8, 2017
1 parent 24521c4 commit 04dffbf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/db/migrate/20170608074534_rename_bogus_gateways.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class RenameBogusGateways < ActiveRecord::Migration[5.0]
def up
say_with_time 'Renaming bogus gateways into payment methods' do
Rake::Task['solidus:migrations:rename_gateways:up'].invoke
end
end

def down
say_with_time 'Renaming bogus payment methods into gateways' do
Rake::Task['solidus:migrations:rename_gateways:down'].invoke
end
end
end
38 changes: 38 additions & 0 deletions core/lib/solidus/migrations/rename_gateways.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module Solidus
module Migrations
class RenameGateways
DEFAULT_MAPPING = {
'Spree::Gateway::Bogus' => 'Spree::PaymentMethod::BogusCreditCard',
'Spree::Gateway::BogusSimple' => 'Spree::PaymentMethod::SimpleBogusCreditCard'
}

attr_reader :gateway_mapping

def initialize(gateway_mapping = DEFAULT_MAPPING)
@gateway_mapping = gateway_mapping
end

def up
gateway_mapping.inject(0) do |count, mapping|
count + update(from: mapping[0], to: mapping[1])
end
end

def down
gateway_mapping.inject(0) do |count, mapping|
count + update(from: mapping[1], to: mapping[0])
end
end

private

def update(from:, to:)
ActiveRecord::Base.connection.update <<-SQL.strip_heredoc
UPDATE spree_payment_methods SET type = '#{to}' WHERE type = '#{from}';
SQL
end
end
end
end
19 changes: 19 additions & 0 deletions core/lib/tasks/migrations/rename_gateways.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'solidus/migrations/rename_gateways'

namespace 'solidus:migrations:rename_gateways' do
task up: :environment do
count = Solidus::Migrations::RenameGateways.new.up

unless ENV['VERBOSE'] == 'false' || !verbose
puts "Renamed #{count} gateways into payment methods."
end
end

task down: :environment do
count = Solidus::Migrations::RenameGateways.new.down

unless ENV['VERBOSE'] == 'false' || !verbose
puts "Renamed #{count} payment methods into gateways."
end
end
end

0 comments on commit 04dffbf

Please sign in to comment.