From 5244e8583dc8969e56149958505432722382f28a Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Tue, 17 Dec 2024 14:44:29 +0100 Subject: [PATCH] Move patches to app/patches This should make the patches in line with the Flickwerk conventions. --- .../admin/variants_controller_decorator.rb | 42 ------------------- .../spree/base_helper_decorator.rb | 31 -------------- .../spree/line_item_decorator.rb | 18 -------- .../spree/variant_decorator.rb | 23 ---------- .../spree_admin_variants_controller_patch.rb | 38 +++++++++++++++++ .../spree_base_helper_patch.rb | 29 +++++++++++++ .../spree_line_item_patch.rb | 16 +++++++ .../spree_variant_patch.rb | 21 ++++++++++ 8 files changed, 104 insertions(+), 114 deletions(-) delete mode 100644 app/decorators/controllers/solidus_volume_pricing/spree/admin/variants_controller_decorator.rb delete mode 100644 app/decorators/helpers/solidus_volume_pricing/spree/base_helper_decorator.rb delete mode 100644 app/decorators/models/solidus_volume_pricing/spree/line_item_decorator.rb delete mode 100644 app/decorators/models/solidus_volume_pricing/spree/variant_decorator.rb create mode 100644 app/patches/controllers/solidus_volume_pricing/spree_admin_variants_controller_patch.rb create mode 100644 app/patches/helpers/solidus_volume_pricing/spree_base_helper_patch.rb create mode 100644 app/patches/models/solidus_volume_pricing/spree_line_item_patch.rb create mode 100644 app/patches/models/solidus_volume_pricing/spree_variant_patch.rb diff --git a/app/decorators/controllers/solidus_volume_pricing/spree/admin/variants_controller_decorator.rb b/app/decorators/controllers/solidus_volume_pricing/spree/admin/variants_controller_decorator.rb deleted file mode 100644 index bfafc6d..0000000 --- a/app/decorators/controllers/solidus_volume_pricing/spree/admin/variants_controller_decorator.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -module SolidusVolumePricing - module Spree - module Admin - module VariantsControllerDecorator - 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 - end -end diff --git a/app/decorators/helpers/solidus_volume_pricing/spree/base_helper_decorator.rb b/app/decorators/helpers/solidus_volume_pricing/spree/base_helper_decorator.rb deleted file mode 100644 index 64fca5f..0000000 --- a/app/decorators/helpers/solidus_volume_pricing/spree/base_helper_decorator.rb +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -module SolidusVolumePricing - module Spree - module BaseHelperDecorator - 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 -end diff --git a/app/decorators/models/solidus_volume_pricing/spree/line_item_decorator.rb b/app/decorators/models/solidus_volume_pricing/spree/line_item_decorator.rb deleted file mode 100644 index d0d75ab..0000000 --- a/app/decorators/models/solidus_volume_pricing/spree/line_item_decorator.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module SolidusVolumePricing - module Spree - module LineItemDecorator - 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 -end diff --git a/app/decorators/models/solidus_volume_pricing/spree/variant_decorator.rb b/app/decorators/models/solidus_volume_pricing/spree/variant_decorator.rb deleted file mode 100644 index 75d42dd..0000000 --- a/app/decorators/models/solidus_volume_pricing/spree/variant_decorator.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module SolidusVolumePricing - module Spree - module VariantDecorator - 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 -end diff --git a/app/patches/controllers/solidus_volume_pricing/spree_admin_variants_controller_patch.rb b/app/patches/controllers/solidus_volume_pricing/spree_admin_variants_controller_patch.rb new file mode 100644 index 0000000..ad885e8 --- /dev/null +++ b/app/patches/controllers/solidus_volume_pricing/spree_admin_variants_controller_patch.rb @@ -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 diff --git a/app/patches/helpers/solidus_volume_pricing/spree_base_helper_patch.rb b/app/patches/helpers/solidus_volume_pricing/spree_base_helper_patch.rb new file mode 100644 index 0000000..bd89fca --- /dev/null +++ b/app/patches/helpers/solidus_volume_pricing/spree_base_helper_patch.rb @@ -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 diff --git a/app/patches/models/solidus_volume_pricing/spree_line_item_patch.rb b/app/patches/models/solidus_volume_pricing/spree_line_item_patch.rb new file mode 100644 index 0000000..b8e2566 --- /dev/null +++ b/app/patches/models/solidus_volume_pricing/spree_line_item_patch.rb @@ -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 diff --git a/app/patches/models/solidus_volume_pricing/spree_variant_patch.rb b/app/patches/models/solidus_volume_pricing/spree_variant_patch.rb new file mode 100644 index 0000000..14dd1da --- /dev/null +++ b/app/patches/models/solidus_volume_pricing/spree_variant_patch.rb @@ -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