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.

Simplify gift card code

Damian Legawiec018f3b27a27c
diff --git a/admin/app/views/spree/admin/gift_card_batches/_form.html.erb b/admin/app/views/spree/admin/gift_card_batches/_form.html.erb
index 2d6978d246..c9c8d75092 100644
--- a/admin/app/views/spree/admin/gift_card_batches/_form.html.erb
+++ b/admin/app/views/spree/admin/gift_card_batches/_form.html.erb
@@ -1,5 +1,5 @@
<div class="row">
- <div class="col-12">
+ <div class="col-lg-6 offset-lg-3">
<div class="card mb-4">
<div class="card-header">
<h5 class="card-title">
@@ -13,30 +13,16 @@
<%= f.text_field :prefix, class: 'form-control', disabled: f.object.persisted? %>
<%= f.error_message_on :prefix %>
</div>
-
- <div class="row mb-3">
- <div class="col-6">
- <div class="form-group">
- <%= f.label :amount, raw(Spree.t(:amount) + required_span_tag) %>
- <%= f.number_field :amount, class: 'form-control', disabled: f.object.persisted?, required: true %>
- <%= f.error_message_on :amount %>
- </div>
- </div>
- <div class="col-6">
- <div class="form-group">
- <%= f.label :minimum_order_amount, raw(Spree.t(:minimum_order_amount) + required_span_tag) %>
- <%= f.number_field :minimum_order_amount, class: 'form-control', min: 0, step: 0.01 %>
- <%= f.error_message_on :minimum_order_amount %>
- </div>
- </div>
+ <div class="form-group">
+ <%= f.label :amount, raw(Spree.t(:amount) + required_span_tag) %>
+ <%= f.number_field :amount, class: 'form-control', disabled: f.object.persisted?, required: true %>
+ <%= f.error_message_on :amount %>
</div>
-
<div class="form-group">
<%= f.label :codes_count, raw(Spree.t('admin.gift_card_batches.codes_count') + required_span_tag) %>
- <%= f.number_field :codes_count, class: 'form-control', disabled: f.object.persisted?, required: true %>
+ <%= f.number_field :codes_count, class: 'form-control', disabled: f.object.persisted?, required: true, min: 1, max: Spree::Config[:gift_card_batch_limit] %>
<%= f.error_message_on :codes_count %>
</div>
-
<div class="form-group">
<%= f.label :expires_at, Spree.t(:expires_at) %>
<%= f.date_field :expires_at, class: 'form-control', disabled: f.object.persisted? %>
diff --git a/core/app/jobs/spree/gift_cards/bulk_generate_job.rb b/core/app/jobs/spree/gift_cards/bulk_generate_job.rb
index e754177474..35d4e2142e 100644
--- a/core/app/jobs/spree/gift_cards/bulk_generate_job.rb
+++ b/core/app/jobs/spree/gift_cards/bulk_generate_job.rb
@@ -6,19 +6,7 @@ module Spree
def perform(id)
gift_cards_batch = Spree::GiftCardBatch.find(id)
- with_optional_tenant(gift_cards_batch) do
- gift_cards_batch.create_gift_cards
- end
- end
-
- private
-
- def with_optional_tenant(gift_cards_batch, &block)
- if defined?(ActsAsTenant)
- ActsAsTenant.with_tenant(gift_cards_batch.tenant, &block)
- else
- block.call
- end
+ gift_cards_batch.create_gift_cards
end
end
end
diff --git a/core/app/models/spree/gift_card_batch.rb b/core/app/models/spree/gift_card_batch.rb
index d40e018246..7715f4fb24 100644
--- a/core/app/models/spree/gift_card_batch.rb
+++ b/core/app/models/spree/gift_card_batch.rb
@@ -4,7 +4,7 @@ module Spree
include Spree::SingleStoreResource
validates :codes_count, :amount, :prefix, presence: true
- validates :codes_count, numericality: { greater_than: 0, less_than_or_equal_to: Spree::Config[:gift_card_batch_limit] }
+ validates :codes_count, numericality: { greater_than: 0, less_than_or_equal_to: Spree::Config[:gift_card_batch_limit].to_i }
validates :amount, numericality: { greater_than: 0 }
validates :minimum_order_amount, numericality: { greater_than_or_equal_to: 0 }
@@ -58,9 +58,7 @@ module Spree
store_id: store_id,
expires_at: expires_at,
minimum_order_amount: minimum_order_amount
- }.tap do |hash|
- hash[:tenant_id] = tenant_id if defined?(ActsAsTenant)
- end
+ }
end
end
end
diff --git a/core/app/services/spree/gift_cards/apply.rb b/core/app/services/spree/gift_cards/apply.rb
index a30f844dff..e8c2b07604 100644
--- a/core/app/services/spree/gift_cards/apply.rb
+++ b/core/app/services/spree/gift_cards/apply.rb
@@ -4,47 +4,37 @@ module Spree
prepend Spree::ServiceModule::Base
def call(gift_card:, order:)
- with_optional_tenant(order) do
- return failure(:gift_card_using_store_credit_error) if order.using_store_credit?
+ return failure(:gift_card_using_store_credit_error) if order.using_store_credit?
- if order_total_below_minimum?(order.total, gift_card)
- minimum_order_amount = gift_card.display_minimum_order_amount
- return failure(:gift_card_minimum_order_value_error, minimum_order_amount: minimum_order_amount)
- end
+ if order_total_below_minimum?(order.total, gift_card)
+ minimum_order_amount = gift_card.display_minimum_order_amount
+ return failure(:gift_card_minimum_order_value_error, minimum_order_amount: minimum_order_amount)
+ end
- amount_applied = [gift_card.amount_remaining, order.total].min
- store = order.store
+ amount_applied = [gift_card.amount_remaining, order.total].min
+ store = order.store
- gift_card.transaction do
- payment_method = ensure_store_credit_payment_method!(store)
- store_credit = gift_card.apply!(amount: amount_applied, user: order.user, currency: order.currency)
+ gift_card.transaction do
+ payment_method = ensure_store_credit_payment_method!(store)
+ store_credit = gift_card.apply!(amount: amount_applied, user: order.user, currency: order.currency)
- return failure(:gift_card_no_amount_remaining) unless store_credit.present?
+ return failure(:gift_card_no_amount_remaining) unless store_credit.present?
- order.update!(gift_card: gift_card)
- order.payments.create!(
- source: store_credit,
- payment_method: payment_method,
- amount: amount_applied,
- state: 'checkout',
- response_code: store_credit.generate_authorization_code
- )
+ order.update!(gift_card: gift_card)
+ order.payments.create!(
+ source: store_credit,
+ payment_method: payment_method,
+ amount: amount_applied,
+ state: 'checkout',
+ response_code: store_credit.generate_authorization_code
+ )
- success(true)
- end
+ success(true)
end
end
private
- def with_optional_tenant(order, &block)
- if defined?(ActsAsTenant)
- ActsAsTenant.with_tenant(order.tenant, &block)
- else
- block.call
- end
- end
-
def can_apply_gift_card?(order)
!order.using_store_credit?
end
diff --git a/core/spec/models/spree/gift_card_batch_spec.rb b/core/spec/models/spree/gift_card_batch_spec.rb
index d20750b896..c2e3800e47 100644
--- a/core/spec/models/spree/gift_card_batch_spec.rb
+++ b/core/spec/models/spree/gift_card_batch_spec.rb
@@ -1,17 +1,3 @@
-# == Schema Information
-#
-# Table name: spree_gift_card_batches
-#
-# id :bigint not null, primary key
-# amount :decimal(10, 2) default(10.0), not null
-# codes_count :integer default(1), not null
-# expires_at :date
-# minimum_order_amount :decimal(10, 2) default(0.0)
-# prefix :string
-# created_at :datetime not null
-# updated_at :datetime not null
-# tenant_id :bigint not null
-#
require 'spec_helper'
RSpec.describe Spree::GiftCardBatch, type: :model do
diff --git a/core/spec/models/spree/gift_card_spec.rb b/core/spec/models/spree/gift_card_spec.rb
index eaebc5b815..45fa081051 100644
--- a/core/spec/models/spree/gift_card_spec.rb
+++ b/core/spec/models/spree/gift_card_spec.rb
@@ -1,22 +1,3 @@
-# == Schema Information
-#
-# Table name: spree_gift_cards
-#
-# id :bigint not null, primary key
-# amount :decimal(10, 2) not null
-# amount_remaining :decimal(10, 2) default(0.0), not null
-# code :string not null
-# expires_at :date
-# minimum_order_amount :decimal(10, 2) default(0.0)
-# state :integer default("active"), not null
-# created_at :datetime not null
-# updated_at :datetime not null
-# gift_card_batch_id :bigint
-# store_credit_id :bigint
-# store_id :bigint not null
-# tenant_id :bigint not null
-# user_id :bigint
-#
require 'spec_helper'
RSpec.describe Spree::GiftCard, type: :model do
diff --git a/core/spec/models/spree/promotion_handler/coupon_with_gift_card_spec.rb b/core/spec/models/spree/promotion_handler/coupon_with_gift_card_spec.rb
index 00d6363068..8c0476de20 100644
--- a/core/spec/models/spree/promotion_handler/coupon_with_gift_card_spec.rb
+++ b/core/spec/models/spree/promotion_handler/coupon_with_gift_card_spec.rb
@@ -49,7 +49,7 @@ describe Spree::PromotionHandler::Coupon, type: :model do
describe '#remove' do
context 'when gift card is applied' do
let(:store) { Spree::Store.default }
- let(:order) { create(:order_with_line_items, store: store, gift_card: gift_card) }
+ let(:order) { create(:order_with_line_items, store: store) }
let(:gift_card) { create(:gift_card, store: store, amount: 10) }
subject { Spree::PromotionHandler::Coupon.new(order) }
diff --git a/core/spec/services/spree/gift_cards/remove_spec.rb b/core/spec/services/spree/gift_cards/remove_spec.rb
index 21302db003..2c84ac4e3e 100644
--- a/core/spec/services/spree/gift_cards/remove_spec.rb
+++ b/core/spec/services/spree/gift_cards/remove_spec.rb
@@ -29,13 +29,13 @@ RSpec.describe Spree::GiftCards::Remove do
expect { subject }.to change(Spree::StoreCredit, :count).by(-1)
expect(subject).to be_success
- expect(order.reload.gift_card).to be(nil)
+ expect(order.reload.gift_card).to be_nil
expect(gift_card.reload).to be_active
expect(gift_card.amount_remaining).to eq(50)
expect(store_credit_payment).to be_present
- expect(store_credit_payment).to be_invalid
+ expect(store_credit_payment.state).to eq('invalid')
expect(store_credit_payment.source).to be_deleted
end