Skip to content

Commit

Permalink
Prefix associations and factories with solidus_ instead of friendly_
Browse files Browse the repository at this point in the history
In the new solidus_promotions gem, we need some way of distinguishing
factories and associations from the legacy_promotion ones. In
`solidus_friendly_promotions`, that prefix was `friendly_`, in
`solidus_promotions`, it's `solidus_`.
  • Loading branch information
mamhoff committed Jun 26, 2024
1 parent d9e1408 commit 7b1c00e
Show file tree
Hide file tree
Showing 64 changed files with 240 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module SolidusPromotions
module AdjustmentDecorator
def self.prepended(base)
base.scope :friendly_promotion, -> { where(source_type: "SolidusPromotions::Benefit") }
base.scope :solidus_promotion, -> { where(source_type: "SolidusPromotions::Benefit") }
end

Spree::Adjustment.prepend self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ module SolidusPromotions
module OrderDecorator
module ClassMethods
def allowed_ransackable_associations
super + ["friendly_promotions", "friendly_order_promotions"]
super + ["solidus_promotions", "solidus_order_promotions"]
end
end

def self.prepended(base)
base.has_many :friendly_order_promotions,
base.has_many :solidus_order_promotions,
class_name: "SolidusPromotions::OrderPromotion",
dependent: :destroy,
inverse_of: :order
base.has_many :friendly_promotions, through: :friendly_order_promotions, source: :promotion
base.has_many :solidus_promotions, through: :solidus_order_promotions, source: :promotion
end

def discountable_item_total
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def preload(records:, associations:)
end

def connected_order_promotions
eligible_connected_promotion_ids = order.friendly_order_promotions.select do |order_promotion|
eligible_connected_promotion_ids = order.solidus_order_promotions.select do |order_promotion|
order_promotion.promotion.kept? && (order_promotion.promotion_code.nil? || !order_promotion.promotion_code.usage_limit_exceeded?(excluded_orders: [order]))
end.map(&:promotion_id)
order.friendly_promotions.active(reference_time).where(id: eligible_connected_promotion_ids).includes(promotion_includes)
order.solidus_promotions.active(reference_time).where(id: eligible_connected_promotion_ids).includes(promotion_includes)
end

def sale_promotions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ def initialize(order:)
end

def call
sync_spree_order_promotions_to_friendly_order_promotions
sync_friendly_order_promotions_to_spree_order_promotions
sync_spree_order_promotions_to_solidus_order_promotions
sync_solidus_order_promotions_to_spree_order_promotions
end

private

def sync_spree_order_promotions_to_friendly_order_promotions
def sync_spree_order_promotions_to_solidus_order_promotions
order.order_promotions.each do |spree_order_promotion|
friendly_promotion = SolidusPromotions::Promotion.find_by(
solidus_promotion = SolidusPromotions::Promotion.find_by(
original_promotion_id: spree_order_promotion.promotion.id
)
next unless friendly_promotion
next unless solidus_promotion
if spree_order_promotion.promotion_code
friendly_promotion_code = friendly_promotion.codes.find_by(
solidus_promotion_code = solidus_promotion.codes.find_by(
value: spree_order_promotion.promotion_code.value
)
end
order.friendly_order_promotions.find_or_create_by!(
promotion: friendly_promotion,
promotion_code: friendly_promotion_code
order.solidus_order_promotions.find_or_create_by!(
promotion: solidus_promotion,
promotion_code: solidus_promotion_code
)
end
end

def sync_friendly_order_promotions_to_spree_order_promotions
order.friendly_order_promotions.each do |friendly_order_promotion|
spree_promotion = friendly_order_promotion.promotion.original_promotion
def sync_solidus_order_promotions_to_spree_order_promotions
order.solidus_order_promotions.each do |solidus_order_promotion|
spree_promotion = solidus_order_promotion.promotion.original_promotion
next unless spree_promotion
if friendly_order_promotion.promotion_code
if solidus_order_promotion.promotion_code
spree_promotion_code = spree_promotion.promotion_codes.find_by(
value: friendly_order_promotion.promotion_code.value
value: solidus_order_promotion.promotion_code.value
)
end
order.order_promotions.find_or_create_by!(
Expand Down
2 changes: 1 addition & 1 deletion promotions/app/models/solidus_promotions/promotion_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def usage_count(excluded_orders: [])
.discounted_orders
.complete
.where.not(spree_orders: { state: :canceled })
.joins(:friendly_order_promotions)
.joins(:solidus_order_promotions)
.where(SolidusPromotions::OrderPromotion.table_name => { promotion_code_id: id })
.where.not(id: excluded_orders.map(&:id))
.count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def remove
elsif !promotion_exists_on_order?(order, promotion)
set_error_code :coupon_code_not_present
else
order.friendly_order_promotions.destroy_by(
order.solidus_order_promotions.destroy_by(
promotion: promotion
)
order.recalculate
Expand Down Expand Up @@ -77,7 +77,7 @@ def handle_present_promotion
Spree::Config.promotions.order_adjuster_class.new(order, dry_run_promotion: promotion).call

if promotion.eligibility_results.success?
order.friendly_order_promotions.create!(
order.solidus_order_promotions.create!(
promotion: promotion,
promotion_code: promotion_code
)
Expand Down Expand Up @@ -106,7 +106,7 @@ def promotion_applied
end

def promotion_exists_on_order?(order, promotion)
order.friendly_promotions.include? promotion
order.solidus_promotions.include? promotion
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def activate
if promotion
Spree::Config.promotions.order_adjuster_class.new(order, dry_run_promotion: promotion).call
if promotion.eligibility_results.success?
order.friendly_promotions << promotion
order.solidus_promotions << promotion
order.recalculate
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OrderPromotionSubscriber
# @param event [Omnes::UnstructuredEvent]
def clear_order_promotions(event)
order = event[:order]
order.friendly_order_promotions.destroy_all
order.solidus_order_promotions.destroy_all
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def filters
{
label: t(".filters.promotions"),
combinator: "or",
attribute: "friendly_promotions_id",
attribute: "solidus_promotions_id",
predicate: "in",
options: SolidusPromotions::Promotion.all.pluck(:name, :id)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# frozen_string_literal: true

FactoryBot.define do
factory :completed_order_with_friendly_promotion, parent: :order_with_line_items do
factory :completed_order_with_solidus_promotion, parent: :order_with_line_items do
transient do
completed_at { Time.current }
promotion { nil }
end

after(:create) do |order, evaluator|
promotion = evaluator.promotion || create(:friendly_promotion, code: "test")
promotion_code = promotion.codes.first || create(:friendly_promotion_code, promotion: promotion)
promotion = evaluator.promotion || create(:solidus_promotion, code: "test")
promotion_code = promotion.codes.first || create(:solidus_promotion_code, promotion: promotion)

order.friendly_order_promotions.create!(promotion: promotion, promotion_code: promotion_code)
order.solidus_order_promotions.create!(promotion: promotion, promotion_code: promotion_code)
order.recalculate
order.update_column(:completed_at, evaluator.completed_at)
order.update_column(:state, "complete")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

FactoryBot.define do
factory :friendly_order_promotion, class: "SolidusPromotions::OrderPromotion" do
factory :solidus_order_promotion, class: "SolidusPromotions::OrderPromotion" do
association :order, factory: :order
association :promotion, factory: :friendly_promotion
association :promotion, factory: :solidus_promotion
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :friendly_promotion_category, class: "SolidusPromotions::PromotionCategory" do
factory :solidus_promotion_category, class: "SolidusPromotions::PromotionCategory" do
name { "Promotion Category" }
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

FactoryBot.define do
factory :friendly_promotion_code, class: "SolidusPromotions::PromotionCode" do
association :promotion, factory: :friendly_promotion
factory :solidus_promotion_code, class: "SolidusPromotions::PromotionCode" do
association :promotion, factory: :solidus_promotion
sequence(:value) { |i| "code#{i}" }
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :friendly_promotion, class: "SolidusPromotions::Promotion" do
factory :solidus_promotion, class: "SolidusPromotions::Promotion" do
name { "Promo" }
customer_label { "Because we like you" }

Expand All @@ -10,7 +10,7 @@
end
before(:create) do |promotion, evaluator|
if evaluator.code
promotion.codes << build(:friendly_promotion_code, promotion: promotion, value: evaluator.code)
promotion.codes << build(:solidus_promotion_code, promotion: promotion, value: evaluator.code)
end
end

Expand All @@ -29,7 +29,7 @@
end
end

factory :friendly_promotion_with_benefit_adjustment, traits: [:with_adjustable_benefit]
factory :solidus_promotion_with_benefit_adjustment, traits: [:with_adjustable_benefit]

trait :with_line_item_adjustment do
transient do
Expand All @@ -40,7 +40,7 @@
preferred_amount { adjustment_rate }
end

factory :friendly_promotion_with_item_adjustment, traits: [:with_line_item_adjustment]
factory :solidus_promotion_with_item_adjustment, traits: [:with_line_item_adjustment]

trait :with_free_shipping do
after(:create) do |promotion|
Expand All @@ -60,6 +60,6 @@
calculator_class { SolidusPromotions::Calculators::DistributedAmount }
end

factory :friendly_promotion_with_order_adjustment, traits: [:with_order_adjustment]
factory :solidus_promotion_with_order_adjustment, traits: [:with_order_adjustment]
end
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

FactoryBot.define do
factory :friendly_shipping_rate_discount, class: "SolidusPromotions::ShippingRateDiscount" do
factory :solidus_shipping_rate_discount, class: "SolidusPromotions::ShippingRateDiscount" do
amount { BigDecimal("-4.00") }
shipping_rate
benefit do
promotion = create(:friendly_promotion, name: "10% off shipping!", customer_label: "10% off")
promotion = create(:solidus_promotion, name: "10% off shipping!", customer_label: "10% off")
ten_percent = SolidusPromotions::Calculators::Percent.new(preferred_percent: 10)
SolidusPromotions::Benefits::AdjustShipment.create!(promotion: promotion, calculator: ten_percent)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:email) { "[email protected]" }
let(:code_batch) do
SolidusPromotions::PromotionCodeBatch.create!(
promotion: create(:friendly_promotion),
promotion: create(:solidus_promotion),
base_code: "test",
number_of_codes: 10,
email: email
Expand Down Expand Up @@ -35,7 +35,7 @@ def codes
context "with a custom join character" do
let(:code_batch) do
SolidusPromotions::PromotionCodeBatch.create!(
promotion: create(:friendly_promotion),
promotion: create(:solidus_promotion),
base_code: "test",
number_of_codes: 10,
email: email,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@

it "migrates our adjustment" do
spree_promotion_action = Spree::PromotionAction.first
friendly_promotion_benefit = SolidusPromotions::Benefit.first
solidus_promotion_benefit = SolidusPromotions::Benefit.first
expect { subject }.to change {
Spree::Adjustment.promotion.first.source
}.from(spree_promotion_action).to(friendly_promotion_benefit)
}.from(spree_promotion_action).to(solidus_promotion_benefit)
end

it "will not touch tax adjustments" do
Expand All @@ -57,10 +57,10 @@

it "migrates our adjustment" do
spree_promotion_action = Spree::PromotionAction.first
friendly_promotion_benefit = SolidusPromotions::Benefit.first
solidus_promotion_benefit = SolidusPromotions::Benefit.first
expect { subject }.to change {
Spree::Adjustment.promotion.first.source
}.from(friendly_promotion_benefit).to(spree_promotion_action)
}.from(solidus_promotion_benefit).to(spree_promotion_action)
end

it "will not touch tax adjustments" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

context "if the order promotion already exists" do
before do
order.friendly_order_promotions.create!(
order.solidus_order_promotions.create!(
promotion: SolidusPromotions::Promotion.first,
promotion_code: nil
)
Expand All @@ -87,7 +87,7 @@

context "if the order promotion already exists" do
before do
order.friendly_order_promotions.create(
order.solidus_order_promotions.create(
promotion: SolidusPromotions::Promotion.first,
promotion_code: SolidusPromotions::PromotionCode.first
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
RSpec.describe "Friendly Factories" do
it "has a bunch of working factories" do
[
:friendly_promotion,
:friendly_promotion_with_benefit_adjustment,
:friendly_promotion_with_item_adjustment,
:friendly_promotion_with_order_adjustment,
:friendly_shipping_rate_discount
:solidus_promotion,
:solidus_promotion_with_benefit_adjustment,
:solidus_promotion_with_item_adjustment,
:solidus_promotion_with_order_adjustment,
:solidus_shipping_rate_discount
].each do |factory|
expect { FactoryBot.create(factory) }.not_to raise_exception
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "rails_helper"

RSpec.describe SolidusPromotions::PromotionCodeBatchMailer, type: :mailer do
let(:promotion) { create(:friendly_promotion, name: "Promotion Test") }
let(:promotion) { create(:solidus_promotion, name: "Promotion Test") }
let(:code_batch) do
SolidusPromotions::PromotionCodeBatch.create!(
promotion_id: promotion.id,
Expand Down
Loading

0 comments on commit 7b1c00e

Please sign in to comment.