Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Braintree to the installer as a payment method option #4961

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ jobs:
cd /tmp/my_app
bundle list | grep 'solidus_paypal_commerce_platform (1.'

- install_solidus: { flags: "--sample=false --frontend=starter --payment-method=braintree" }
- test_page: { expected_text: "The only eCommerce platform you’ll ever need." }
- run:
name: Ensure the correct Braintree is installed for SSF
command: |
cd /tmp/my_app
bundle list | grep 'solidus_braintree (3.'

- install_solidus: { flags: "--sample=false --frontend=none --authentication=none" }
- test_page: { expected_text: "<title>Ruby on Rails" }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
unless Bundler.locked_gems.dependencies['solidus_braintree']
bundle_command 'add solidus_braintree --version "~> 3.0"'
end

generate 'solidus_braintree:install'
54 changes: 36 additions & 18 deletions core/lib/generators/solidus/install/install_generator.rb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a much needed cleanup! 🙌
It should be applied equally to all enum options like payment

Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,31 @@ class InstallGenerator < Rails::Generators::AppBase
none
]

PAYMENT_METHODS = %w[
paypal
bolt
none
PAYMENT_METHODS = [
waiting-for-dev marked this conversation as resolved.
Show resolved Hide resolved
{
name: 'paypal',
frontends: %w[none classic starter],
description: 'Install `solidus_paypal_commerce_platform`',
default: true,
},
{
name: 'bolt',
frontends: %w[classic],
description: 'Install `solidus_bolt`',
default: false,
},
{
name: 'braintree',
frontends: %w[none starter],
description: 'Install `solidus_braintree`',
default: false,
},
{
name: 'none',
frontends: %w[none classic starter],
description: 'Skip installing a payment method',
default: false,
},
]

class_option :migrate, type: :boolean, default: true, banner: 'Run Solidus migrations'
Expand All @@ -48,7 +69,7 @@ class InstallGenerator < Rails::Generators::AppBase

class_option :frontend, type: :string, enum: FRONTENDS + LEGACY_FRONTENDS, default: nil, desc: "Indicates which frontend to install."
class_option :authentication, type: :string, enum: AUTHENTICATIONS, default: nil, desc: "Indicates which authentication system to install."
class_option :payment_method, type: :string, enum: PAYMENT_METHODS, default: nil, desc: "Indicates which payment method to install."
class_option :payment_method, type: :string, enum: PAYMENT_METHODS.map { |payment_method| payment_method[:name] }, default: nil, desc: "Indicates which payment method to install."

# DEPRECATED
class_option :with_authentication, type: :boolean, hide: true, default: nil
Expand Down Expand Up @@ -323,29 +344,26 @@ def detect_payment_method_to_install
return 'paypal' if Bundler.locked_gems.dependencies['solidus_paypal_commerce_platform']
return 'bolt' if Bundler.locked_gems.dependencies['solidus_bolt']

descriptions = {
paypal: "- [#{set_color 'paypal', :bold}] Install `solidus_paypal_commerce_platform` (#{set_color :default, :bold}).",
bolt: "- [#{set_color 'bolt', :bold}] Install `solidus_bolt`.",
none: "- [#{set_color 'none', :bold}] Skip installing a payment method.",
}

payment_methods = PAYMENT_METHODS

if @selected_frontend != 'classic'
payment_methods -= ['bolt']
descriptions.delete(:bolt)
selected_frontend_payment_methods = PAYMENT_METHODS.select do |payment_method|
payment_method[:frontends].include?(@selected_frontend)
end

selected = options[:payment_method] || (options[:auto_accept] && 'paypal') ||
ask_with_description(
default: 'paypal',
limited_to: payment_methods,
limited_to: selected_frontend_payment_methods.map { |payment_method| payment_method[:name] },
desc: <<~TEXT
Which payment method would you like to use?

#{descriptions.values.join("\n")}
#{selected_frontend_payment_methods.map { |payment_method| formatted_payment_method_description(payment_method) }.join("\n")}
TEXT
)
end

def formatted_payment_method_description(payment_method)
gsmendoza marked this conversation as resolved.
Show resolved Hide resolved
default_label = " (#{set_color :default, :bold})" if payment_method[:default]

"- [#{set_color payment_method[:name], :bold}] #{payment_method[:description]}#{default_label}."
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,23 @@
expect(questions.first[:default]).to eq('paypal')
expect(strip_ansi questions.first[:desc]).to include('[paypal]')
expect(strip_ansi questions.first[:desc]).to include('[bolt]')
expect(strip_ansi questions.first[:desc]).to_not include('[braintree]')
expect(strip_ansi questions.first[:desc]).to include('[none]')
end

it 'presents different options for the "classic"' do
it 'presents different options for the "starter"' do
gsmendoza marked this conversation as resolved.
Show resolved Hide resolved
questions = []
generator = described_class.new([], ['--frontend=starter', '--authentication=devise'])
allow(generator).to receive(:ask_with_description) { |**args| questions << args }

generator.prepare_options

expect(questions.size).to eq(1)
expect(questions.first[:limited_to]).to eq(['paypal', 'none'])
expect(questions.first[:limited_to]).to eq(['paypal', 'braintree', 'none'])
expect(questions.first[:default]).to eq('paypal')
expect(strip_ansi questions.first[:desc]).to include('[paypal]')
expect(strip_ansi questions.first[:desc]).not_to include('[bolt]')
expect(strip_ansi questions.first[:desc]).to include('[braintree]')
expect(strip_ansi questions.first[:desc]).to include('[none]')
end
end
Expand Down