Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Payment Method Store Credit fixes (#12242)

Damian Legawiec61749236f204
diff --git a/admin/app/views/spree/admin/payment_methods/_form.html.erb b/admin/app/views/spree/admin/payment_methods/_form.html.erb
index 060aa34a3f..bbdb514231 100644
--- a/admin/app/views/spree/admin/payment_methods/_form.html.erb
+++ b/admin/app/views/spree/admin/payment_methods/_form.html.erb
@@ -40,7 +40,12 @@
<% if can?(:manage, Spree::Store) && available_stores.count > 1 %>
<div class="form-group">
<%= label_tag :payment_method_stores, Spree.t(:stores) %>
- <%= collection_select(:payment_method, :store_ids, available_stores, :id, :unique_name, {}, { multiple: true, class: 'select2' }) %>
+ <%= f.collection_check_boxes :store_ids, available_stores, :id, :name do |b| %>
+ <div class="custom-control custom-checkbox mb-1">
+ <%= b.check_box(class: 'custom-control-input') %>
+ <%= b.label(class: 'custom-control-label') %>
+ </div>
+ <% end %>
</div>
<% end %>
diff --git a/core/app/models/spree/gateway.rb b/core/app/models/spree/gateway.rb
index b2fe8a959f..c7bf5643dc 100644
--- a/core/app/models/spree/gateway.rb
+++ b/core/app/models/spree/gateway.rb
@@ -4,8 +4,6 @@ module Spree
delegate :authorize, :purchase, :capture, :void, :credit, to: :provider
- after_initialize :set_name, if: :new_record?
-
validates :type, presence: true, inclusion: { in: :valid_providers_list }
def payment_source_class
@@ -79,10 +77,6 @@ module Spree
private
- def set_name
- self.name ||= self.class.name.demodulize.titleize.gsub(/Gateway/, '').strip
- end
-
def valid_providers_list
Spree::PaymentMethod.providers.map(&:to_s)
end
diff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rb
index 5dcc887faa..f8ed27f748 100644
--- a/core/app/models/spree/payment_method.rb
+++ b/core/app/models/spree/payment_method.rb
@@ -15,6 +15,8 @@ module Spree
scope :available_on_front_end, -> { active.where(display_on: [:front_end, :both]) }
scope :available_on_back_end, -> { active.where(display_on: [:back_end, :both]) }
+ after_initialize :set_name, if: :new_record?
+
validates :name, presence: true
auto_strip_attributes :name
@@ -112,5 +114,9 @@ module Spree
def public_preference_keys
[]
end
+
+ def set_name
+ self.name ||= self.class.name.demodulize.titleize.gsub(/Gateway/, '').strip
+ end
end
end
diff --git a/core/app/models/spree/payment_method/store_credit.rb b/core/app/models/spree/payment_method/store_credit.rb
index 69ba536c30..8e1a09c3ba 100644
--- a/core/app/models/spree/payment_method/store_credit.rb
+++ b/core/app/models/spree/payment_method/store_credit.rb
@@ -16,14 +16,6 @@ module Spree
payment.pending?
end
- def editable?
- amount_used.zero? && amount_authorized.zero?
- end
-
- def can_be_deleted?
- amount_used.zero? && amount_authorized.zero?
- end
-
def authorize(amount_in_cents, store_credit, gateway_options = {})
if store_credit.nil?
ActiveMerchant::Billing::Response.new(false, Spree.t('store_credit_payment_method.unable_to_find'), {}, {})
diff --git a/core/app/models/spree/promotion_handler/promotion_duplicator.rb b/core/app/models/spree/promotion_handler/promotion_duplicator.rb
index e88c34bb96..f9652bb191 100644
--- a/core/app/models/spree/promotion_handler/promotion_duplicator.rb
+++ b/core/app/models/spree/promotion_handler/promotion_duplicator.rb
@@ -2,7 +2,7 @@ module Spree
module PromotionHandler
class PromotionDuplicator
def initialize(promotion, random_string: nil)
- @promotion = promotion
+ @promotion = promotion.reload
@random_string = random_string || generate_random_string(4)
end
@@ -19,7 +19,7 @@ module Spree
copy_actions
end
- @new_promotion
+ @new_promotion.reload
end
private
@@ -27,7 +27,8 @@ module Spree
def copy_rules
@promotion.promotion_rules.each do |rule|
new_rule = rule.dup
- @new_promotion.promotion_rules << new_rule
+ new_rule.promotion = @new_promotion
+ new_rule.save!
new_rule.users = rule.users if rule.try(:users)
new_rule.taxons = rule.taxons if rule.try(:taxons)
@@ -43,9 +44,9 @@ module Spree
def copy_actions
@promotion.promotion_actions.each do |action|
new_action = action.dup
+ new_action.promotion = @new_promotion
new_action.calculator = action.calculator.dup if action.try(:calculator)
-
- @new_promotion.promotion_actions << new_action
+ new_action.save!
next unless action.try(:promotion_action_line_items)
diff --git a/core/app/models/spree/store_credit.rb b/core/app/models/spree/store_credit.rb
index a1f272ba06..ed4b27be46 100644
--- a/core/app/models/spree/store_credit.rb
+++ b/core/app/models/spree/store_credit.rb
@@ -170,6 +170,14 @@ module Spree
payment.completed? && payment.credit_allowed > 0
end
+ def editable?
+ amount_used.zero? && amount_authorized.zero?
+ end
+
+ def can_be_deleted?
+ amount_used.zero? && amount_authorized.zero?
+ end
+
def generate_authorization_code
"#{id}-SC-#{Time.now.utc.strftime('%Y%m%d%H%M%S%6N')}"
end
diff --git a/core/lib/spree/testing_support/common_rake.rb b/core/lib/spree/testing_support/common_rake.rb
index 9875cdad9a..879ebf6f09 100644
--- a/core/lib/spree/testing_support/common_rake.rb
+++ b/core/lib/spree/testing_support/common_rake.rb
@@ -38,10 +38,10 @@ namespace :common do
]
puts 'Setting up dummy database...'
- system('bin/rails db:environment:set RAILS_ENV=test')
- system('bundle exec rake db:drop db:create')
+ system('bin/rails db:environment:set RAILS_ENV=test > /dev/null 2>&1')
+ system('bundle exec rake db:drop db:create > /dev/null 2>&1')
Spree::DummyModelGenerator.start
- system('bundle exec rake db:migrate')
+ system('bundle exec rake db:migrate > /dev/null 2>&1')
begin
require "generators/#{ENV['LIB_NAME']}/install/install_generator"
@@ -52,13 +52,12 @@ namespace :common do
end
unless ['spree/api', 'spree/core', 'spree/sample'].include?(ENV['LIB_NAME'])
- puts 'Precompiling assets...'
- system('bundle exec rake assets:precompile')
+ system('bundle exec rake assets:precompile > /dev/null 2>&1')
end
end
task :seed do |_t|
puts 'Seeding ...'
- system('bundle exec rake db:seed RAILS_ENV=test')
+ system('bundle exec rake db:seed RAILS_ENV=test > /dev/null 2>&1')
end
end
diff --git a/core/lib/spree/testing_support/factories/promotion_factory.rb b/core/lib/spree/testing_support/factories/promotion_factory.rb
index 6a9c509782..bf312f3bcc 100644
--- a/core/lib/spree/testing_support/factories/promotion_factory.rb
+++ b/core/lib/spree/testing_support/factories/promotion_factory.rb
@@ -5,14 +5,13 @@ FactoryBot.define do
end
after(:create) do |promotion, evaluator|
- rule = Spree::Promotion::Rules::ItemTotal.create!(
+ Spree::Promotion::Rules::ItemTotal.create!(
+ promotion: promotion,
preferred_operator_min: 'gte',
preferred_operator_max: 'lte',
preferred_amount_min: evaluator.item_total_threshold_amount,
preferred_amount_max: evaluator.item_total_threshold_amount + 100
)
- promotion.rules << rule
- promotion.save!
end
end
@@ -61,9 +60,7 @@ FactoryBot.define do
after(:create) do |promotion, evaluator|
calculator = Spree::Calculator::FlatRate.new
calculator.preferred_amount = evaluator.weighted_order_adjustment_amount
- action = Spree::Promotion::Actions::CreateAdjustment.create!(calculator: calculator)
- promotion.actions << action
- promotion.save!
+ Spree::Promotion::Actions::CreateAdjustment.create!(calculator: calculator, promotion: promotion)
end
end
@@ -74,9 +71,7 @@ FactoryBot.define do
name { 'Free Shipping Promotion' }
after(:create) do |promotion|
- action = Spree::Promotion::Actions::FreeShipping.new
- action.promotion = promotion
- action.save
+ Spree::Promotion::Actions::FreeShipping.create!(promotion: promotion)
end
factory :free_shipping_promotion_with_item_total_rule, traits: [:with_item_total_rule]
diff --git a/core/spec/models/spree/adjustable/adjuster/promotion_spec.rb b/core/spec/models/spree/adjustable/adjuster/promotion_spec.rb
index b9e7c24512..f68ff9c2d7 100644
--- a/core/spec/models/spree/adjustable/adjuster/promotion_spec.rb
+++ b/core/spec/models/spree/adjustable/adjuster/promotion_spec.rb
@@ -12,7 +12,8 @@ describe Spree::Adjustable::Adjuster::Promotion, type: :model do
context 'best promotion is always applied' do
let(:calculator) { Spree::Calculator::FlatRate.new(preferred_amount: 10) }
- let(:source) { Spree::Promotion::Actions::CreateItemAdjustments.create calculator: calculator }
+ let(:promotion) { create(:promotion) }
+ let(:source) { Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator: calculator, promotion: promotion) }
def create_adjustment(label, amount)
create(:adjustment,
diff --git a/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb b/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb
index 1784f757fa..e45022f413 100644
--- a/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb
+++ b/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe Spree::Promotion::Actions::CreateAdjustment, type: :model do
let(:order) { create(:order_with_line_items, line_items_count: 1) }
let(:promotion) { create(:promotion) }
- let(:action) { Spree::Promotion::Actions::CreateAdjustment.new }
+ let(:action) { Spree::Promotion::Actions::CreateAdjustment.new(promotion: promotion) }
let(:payload) { { order: order } }
it_behaves_like 'an adjustment source'
diff --git a/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb b/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb
index f3ca1283c8..6f3181d6a3 100644
--- a/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb
+++ b/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb
@@ -128,8 +128,9 @@ module Spree
end
context '#destroy' do
- let!(:action) { CreateItemAdjustments.create! }
- let(:other_action) { CreateItemAdjustments.create! }
+ let(:other_promotion) { create(:promotion) }
+ let!(:action) { CreateItemAdjustments.create!(promotion: promotion) }
+ let!(:other_action) { CreateItemAdjustments.create!(promotion: other_promotion) }
before { promotion.promotion_actions = [other_action] }
diff --git a/core/spec/models/spree/promotion/actions/create_line_items_spec.rb b/core/spec/models/spree/promotion/actions/create_line_items_spec.rb
index b6aa3e2325..326bfc4042 100644
--- a/core/spec/models/spree/promotion/actions/create_line_items_spec.rb
+++ b/core/spec/models/spree/promotion/actions/create_line_items_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'
describe Spree::Promotion::Actions::CreateLineItems, type: :model do
let(:order) { create(:order) }
- let(:action) { Spree::Promotion::Actions::CreateLineItems.create }
+ let(:action) { Spree::Promotion::Actions::CreateLineItems.create!(promotion: promotion) }
let(:promotion) { create(:promotion) }
let(:shirt) { create(:variant) }
let(:mug) { create(:variant) }
diff --git a/core/spec/models/spree/promotion/actions/free_shipping_spec.rb b/core/spec/models/spree/promotion/actions/free_shipping_spec.rb
index 8b8227d778..48c1baf4ca 100644
--- a/core/spec/models/spree/promotion/actions/free_shipping_spec.rb
+++ b/core/spec/models/spree/promotion/actions/free_shipping_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe Spree::Promotion::Actions::FreeShipping, type: :model do
let(:order) { create(:completed_order_with_totals) }
let(:promotion) { create(:promotion) }
- let(:action) { Spree::Promotion::Actions::FreeShipping.create }
+ let(:action) { Spree::Promotion::Actions::FreeShipping.create!(promotion: promotion) }
let(:payload) { { order: order } }
it_behaves_like 'an adjustment source'
diff --git a/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb b/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb
index 3516efd698..12854d05de 100644
--- a/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb
+++ b/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb
@@ -27,7 +27,7 @@ describe Spree::PromotionHandler::PromotionDuplicator do
end
describe '#duplicate' do
- let(:new_promotion) { subject.duplicate }
+ let!(:new_promotion) { subject.duplicate }
context 'model fields' do
let(:excluded_fields) { ['code', 'name', 'path', 'id', 'created_at', 'updated_at', 'deleted_at'] }
@@ -54,7 +54,7 @@ describe Spree::PromotionHandler::PromotionDuplicator do
it "promotion rule's fields (except promotion_id) are the same" do
old_rule = promotion.promotion_rules.first
- new_rule = new_promotion.promotion_rules.first
+ new_rule = new_promotion.reload.promotion_rules.first
old_rule.attributes.each_key do |key|
expect(old_rule.send(key)).to eq new_rule.send(key) unless excluded_fields.include?(key)
diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb
index 2c986b2be8..9dcd58de88 100644
--- a/core/spec/models/spree/promotion_spec.rb
+++ b/core/spec/models/spree/promotion_spec.rb
@@ -234,8 +234,8 @@ describe Spree::Promotion, type: :model do
end
describe '#activate' do
- let(:action1) { Spree::Promotion::Actions::CreateAdjustment.create! }
- let(:action2) { Spree::Promotion::Actions::CreateAdjustment.create! }
+ let(:action1) { Spree::Promotion::Actions::CreateAdjustment.create!(promotion: promotion) }
+ let(:action2) { Spree::Promotion::Actions::CreateAdjustment.create!(promotion: promotion) }
let(:user) { create(:user) }
let(:order) { create(:order, user: user) }
let(:payload) { { order: order, user: user } }
@@ -244,7 +244,6 @@ describe Spree::Promotion, type: :model do
allow(action1).to receive_messages perform: true
allow(action2).to receive_messages perform: true
- promotion.promotion_actions = [action1, action2]
promotion.created_at = 2.days.ago
end
@@ -546,8 +545,8 @@ describe Spree::Promotion, type: :model do
end
context "with 'all' match policy" do
- let(:promo1) { Spree::PromotionRule.create! }
- let(:promo2) { Spree::PromotionRule.create! }
+ let(:promo1) { Spree::PromotionRule.create!(promotion: promotion) }
+ let(:promo2) { Spree::PromotionRule.create!(promotion: promotion) }
before { promotion.match_policy = 'all' }
@@ -596,7 +595,7 @@ describe Spree::Promotion, type: :model do
it 'has eligible rules if any of the rules are eligible' do
allow_any_instance_of(Spree::PromotionRule).to receive_messages(applicable?: true)
- true_rule = Spree::PromotionRule.create(promotion: promotion)
+ true_rule = Spree::PromotionRule.create!(promotion: promotion)
allow(true_rule).to receive_messages(eligible?: true)
allow(promotion).to receive_messages(rules: [true_rule])
allow(promotion).to receive_message_chain(:rules, :for).and_return([true_rule])
@@ -604,7 +603,7 @@ describe Spree::Promotion, type: :model do
end
context 'when none of the rules are eligible' do
- let(:promo) { Spree::PromotionRule.create! }
+ let(:promo) { Spree::PromotionRule.create!(promotion: promotion) }
let(:errors) { double ActiveModel::Errors, empty?: false }
before do
diff --git a/core/spec/support/concerns/adjustment_source.rb b/core/spec/support/concerns/adjustment_source.rb
index 6dfb6bd31d..5549af68ef 100644
--- a/core/spec/support/concerns/adjustment_source.rb
+++ b/core/spec/support/concerns/adjustment_source.rb
@@ -1,5 +1,11 @@
shared_examples_for 'an adjustment source' do
- subject(:source) { described_class.create }
+ subject(:source) do
+ if defined?(promotion) && described_class.reflect_on_association(:promotion)
+ described_class.create!(promotion: promotion)
+ else
+ described_class.create
+ end
+ end
before do
allow(Spree::Adjustable::AdjustmentsUpdater).to receive(:update)
diff --git a/sample/db/samples/promotions.rb b/sample/db/samples/promotions.rb
index ecc0fe4233..26a70262ee 100644
--- a/sample/db/samples/promotions.rb
+++ b/sample/db/samples/promotions.rb
@@ -12,15 +12,16 @@ promotion = Spree::Promotion.where(
usage_limit: nil,
path: nil,
match_policy: 'any',
- description: ''
-).first_or_create do |promo|
+ description: '',
+ code: 'FREESHIP'
+).first_or_create! do |promo|
promo.stores = Spree::Store.all
end
Spree::PromotionRule.where(
- promotion_id: promotion.id,
+ promotion: promotion,
type: 'Spree::Promotion::Rules::OptionValue',
preferences: { match_policy: 'any', eligible_values: { product.id.to_s => eligible_values } }
).first_or_create!
-Spree::Promotion::Actions::FreeShipping.where(promotion_id: promotion.id).first_or_create!
+Spree::Promotion::Actions::FreeShipping.where(promotion: promotion).first_or_create!