Skip to content

Commit

Permalink
Move patches to app/patches
Browse files Browse the repository at this point in the history
This should make the patches in line with the Flickwerk conventions.
  • Loading branch information
mamhoff committed Dec 17, 2024
1 parent c7cdca3 commit 5244e85
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 114 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module SolidusVolumePricing
module SpreeAdminVariantsControllerPatch
def edit
@variant.volume_prices.build if @variant.volume_prices.empty?
super
end

def volume_prices
@product = @variant.product
@variant.volume_prices.build if @variant.volume_prices.empty?
end

private

# this loads the variant for the master variant volume price editing
def load_resource_instance
parent

if new_actions.include?(params[:action].to_sym)
build_resource
elsif params[:id]
::Spree::Variant.find(params[:id])
end
end

def location_after_save
if @product.master.id == @variant.id && params[:variant].key?(:volume_prices_attributes)
return volume_prices_admin_product_variant_url(@product, @variant)
end

super
end

::Spree::Admin::VariantsController.prepend self
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module SolidusVolumePricing
module SpreeBaseHelperPatch
def self.prepended(base)
base.module_eval do
def display_volume_price(variant, quantity = 1, user = nil)
price_display(variant, quantity: quantity, user: user).price_string
end

def display_volume_price_earning_percent(variant, quantity = 1, user = nil)
price_display(variant, quantity: quantity, user: user).earning_percent_string
end

def display_volume_price_earning_amount(variant, quantity = 1, user = nil)
price_display(variant, quantity: quantity, user: user).earning_amount_string
end

private

def price_display(variant, quantity:, user:)
SolidusVolumePricing::PriceDisplay.new(variant, quantity: quantity, user: user)
end
end
end

::Spree::BaseHelper.prepend self
end
end
16 changes: 16 additions & 0 deletions app/patches/models/solidus_volume_pricing/spree_line_item_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module SolidusVolumePricing
module SpreeLineItemPatch
def set_pricing_attributes
if quantity_changed?
options = SolidusVolumePricing::PricingOptions.from_line_item(self)
self.money_price = SolidusVolumePricing::Pricer.new(variant).price_for(options)
end

super
end

::Spree::LineItem.prepend self
end
end
21 changes: 21 additions & 0 deletions app/patches/models/solidus_volume_pricing/spree_variant_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module SolidusVolumePricing
module SpreeVariantPatch
def self.prepended(base)
base.class_eval do
has_and_belongs_to_many :volume_price_models
has_many :volume_prices, -> { order(position: :asc) }, dependent: :destroy
has_many :model_volume_prices, -> {
order(position: :asc)
}, class_name: '::Spree::VolumePrice', through: :volume_price_models, source: :volume_prices
accepts_nested_attributes_for :volume_prices, allow_destroy: true,
reject_if: proc { |volume_price|
volume_price[:amount].blank? && volume_price[:range].blank?
}
end
end

::Spree::Variant.prepend self
end
end

0 comments on commit 5244e85

Please sign in to comment.