-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Rake task to rename Bogus gateways
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
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |