From ac97de15b4afc29498e94fa05ae3eced1899eed6 Mon Sep 17 00:00:00 2001 From: Alessandro Desantis Date: Sat, 13 Jun 2020 18:13:36 +0200 Subject: Update decorator namespaces to be compliant with Zeitwerk --- .../create_subscription_line_items.rb | 34 ++++++++++ .../create_subscription_line_items.rb | 33 +++++++++ .../subscription_line_items_association.rb | 24 +++++++ .../spree/order/after_create.rb | 17 +++++ .../spree/order/finalize_creates_subscriptions.rb | 21 ++++++ .../order/subscription_line_items_association.rb | 17 +++++ .../spree/user/have_many_subscriptions.rb | 20 ++++++ .../spree/variant/variant_pretty_name.rb | 15 +++++ .../line_items/create_subscription_line_items.rb | 28 -------- .../orders/create_subscription_line_items.rb | 33 --------- .../subscription_line_items_association.rb | 22 ------ app/decorators/spree/orders/after_create.rb | 15 ----- .../spree/orders/finalize_creates_subscriptions.rb | 19 ------ .../orders/subscription_line_items_association.rb | 15 ----- .../spree/users/have_many_subscriptions.rb | 18 ----- .../spree/variants/variant_pretty_name.rb | 13 ---- app/models/solidus_subscriptions/checkout.rb | 8 +-- .../solidus_subscriptions/installment_detail.rb | 2 +- app/models/solidus_subscriptions/line_item.rb | 6 +- .../solidus_subscriptions/line_item_builder.rb | 4 +- app/models/solidus_subscriptions/subscription.rb | 6 +- .../subscription_line_item_builder.rb | 4 +- .../subscription_order_promotion_rule.rb | 4 +- .../subscription_promotion_rule.rb | 4 +- lib/solidus_subscriptions/engine.rb | 6 +- lib/solidus_subscriptions/permitted_attributes.rb | 4 +- lib/solidus_subscriptions/processor.rb | 2 +- .../orders/create_subscription_line_items_spec.rb | 78 ---------------------- .../orders/create_subscription_line_items_spec.rb | 78 ++++++++++++++++++++++ .../subscription_line_items_association_spec.rb | 7 ++ .../order/finalize_creates_subscrptions_spec.rb | 30 +++++++++ .../subscription_line_items_association_spec.rb | 7 ++ .../spree/user/have_many_subscriptions.rb | 8 +++ .../subscription_line_items_association_spec.rb | 7 -- .../orders/finalize_creates_subscrptions_spec.rb | 30 --------- .../subscription_line_items_association_spec.rb | 7 -- .../spree/users/have_many_subscriptions.rb | 8 --- 37 files changed, 336 insertions(+), 318 deletions(-) create mode 100644 app/decorators/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb create mode 100644 app/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items.rb create mode 100644 app/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association.rb create mode 100644 app/decorators/models/solidus_subscriptions/spree/order/after_create.rb create mode 100644 app/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions.rb create mode 100644 app/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association.rb create mode 100644 app/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb create mode 100644 app/decorators/models/solidus_subscriptions/spree/variant/variant_pretty_name.rb delete mode 100644 app/decorators/spree/controllers/api/line_items/create_subscription_line_items.rb delete mode 100644 app/decorators/spree/controllers/orders/create_subscription_line_items.rb delete mode 100644 app/decorators/spree/line_items/subscription_line_items_association.rb delete mode 100644 app/decorators/spree/orders/after_create.rb delete mode 100644 app/decorators/spree/orders/finalize_creates_subscriptions.rb delete mode 100644 app/decorators/spree/orders/subscription_line_items_association.rb delete mode 100644 app/decorators/spree/users/have_many_subscriptions.rb delete mode 100644 app/decorators/spree/variants/variant_pretty_name.rb delete mode 100644 spec/controllers/orders/create_subscription_line_items_spec.rb create mode 100644 spec/decorators/controllers/solidus_subscriptions/spree/orders/create_subscription_line_items_spec.rb create mode 100644 spec/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association_spec.rb create mode 100644 spec/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscrptions_spec.rb create mode 100644 spec/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association_spec.rb create mode 100644 spec/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb delete mode 100644 spec/overrides/spree/line_items/subscription_line_items_association_spec.rb delete mode 100644 spec/overrides/spree/orders/finalize_creates_subscrptions_spec.rb delete mode 100644 spec/overrides/spree/orders/subscription_line_items_association_spec.rb delete mode 100644 spec/overrides/spree/users/have_many_subscriptions.rb diff --git a/app/decorators/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb b/app/decorators/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb new file mode 100644 index 0000000..d24de49 --- /dev/null +++ b/app/decorators/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb @@ -0,0 +1,34 @@ +# Create new subscription line items associated to the current order, when +# a line item is added to the cart which includes subscription_line_item +# params. +# +# The Subscriptions::LineItem acts as a line item place holder for a +# Subscription, indicating that it has been added to the order, but not +# yet purchased +module SolidusSubscriptions + module Spree + module Api + module LineItemsController + module CreateSubscriptionLineItems + include SolidusSubscriptions::SubscriptionLineItemBuilder + + def self.prepended(base) + base.after_action( + :handle_subscription_line_items, + only: [:create, :update], + if: ->{ params[:subscription_line_item] } + ) + end + + private + + def handle_subscription_line_items + create_subscription_line_item(@line_item) + end + end + end + end + end +end + +Spree::Api::LineItemsController.prepend(SolidusSubscriptions::Spree::Api::LineItemsController::CreateSubscriptionLineItems) diff --git a/app/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items.rb b/app/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items.rb new file mode 100644 index 0000000..5925089 --- /dev/null +++ b/app/decorators/controllers/solidus_subscriptions/spree/orders_controller/create_subscription_line_items.rb @@ -0,0 +1,33 @@ +# Create new subscription line items associated to the current order, when +# a line item is added to the cart which includes subscription_line_item +# params. +# +# The Subscriptions::LineItem acts as a line item place holder for a +# Subscription, indicating that it has been added to the order, but not +# yet purchased +module SolidusSubscriptions + module Spree + module OrdersController + module CreateSubscriptionLineItems + include SolidusSubscriptions::SubscriptionLineItemBuilder + + def self.prepended(base) + base.after_action( + :handle_subscription_line_items, + only: :populate, + if: ->{ params[:subscription_line_item] } + ) + end + + private + + def handle_subscription_line_items + line_item = @current_order.line_items.find_by(variant_id: params[:variant_id]) + create_subscription_line_item(line_item) + end + end + end + end +end + +Spree::OrdersController.prepend(SolidusSubscriptions::Spree::OrdersController::CreateSubscriptionLineItems) diff --git a/app/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association.rb b/app/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association.rb new file mode 100644 index 0000000..2faead3 --- /dev/null +++ b/app/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association.rb @@ -0,0 +1,24 @@ +# Each Spree::LineItem can have multiple subscription_line_items. This +# allows a cart to represent multiple subscriptions to the same item in +# the same order. +module SolidusSubscriptions + module Spree + module LineItem + module SubscriptionLineItemsAssociation + def self.prepended(base) + base.has_many( + :subscription_line_items, + class_name: 'SolidusSubscriptions::LineItem', + foreign_key: :spree_line_item_id, + inverse_of: :spree_line_item, + dependent: :destroy + ) + + base.accepts_nested_attributes_for :subscription_line_items + end + end + end + end +end + +Spree::LineItem.prepend(SolidusSubscriptions::Spree::LineItem::SubscriptionLineItemsAssociation) diff --git a/app/decorators/models/solidus_subscriptions/spree/order/after_create.rb b/app/decorators/models/solidus_subscriptions/spree/order/after_create.rb new file mode 100644 index 0000000..234929a --- /dev/null +++ b/app/decorators/models/solidus_subscriptions/spree/order/after_create.rb @@ -0,0 +1,17 @@ +module SolidusSubscriptions + module Spree + module Order + module AfterCreate + def ensure_line_items_present + super unless subscription_order? + end + + def send_cancel_email + super unless subscription_order? + end + end + end + end +end + +Spree::Order.prepend(SolidusSubscriptions::Spree::Order::AfterCreate) diff --git a/app/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions.rb b/app/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions.rb new file mode 100644 index 0000000..6117c35 --- /dev/null +++ b/app/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscriptions.rb @@ -0,0 +1,21 @@ +# Once an order is finalized its subscriptions line items should be converted +# into active subscriptions. This hooks into Spree::Order#finalize! and +# passes all subscription_line_items present on the order to the Subscription +# generator which will build and persist the subscriptions +module SolidusSubscriptions + module Spree + module Order + module FinalizeCreatesSubscriptions + def finalize! + SolidusSubscriptions::SubscriptionGenerator.group(subscription_line_items).each do |line_items| + SolidusSubscriptions::SubscriptionGenerator.activate(line_items) + end + + super + end + end + end + end +end + +Spree::Order.prepend(SolidusSubscriptions::Spree::Order::FinalizeCreatesSubscriptions) diff --git a/app/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association.rb b/app/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association.rb new file mode 100644 index 0000000..1d6fde4 --- /dev/null +++ b/app/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association.rb @@ -0,0 +1,17 @@ +# Spree::Orders may contain many subscription_line_items. When the order is +# finalized these subscription_line_items are converted into subscritpions. +# The order needs to be able to get a list of associated subscription_line_items +# to be able to populate the full subscriptions. +module SolidusSubscriptions + module Spree + module Order + module SubscriptionLineItemsAssociation + def self.prepended(base) + base.has_many :subscription_line_items, through: :line_items + end + end + end + end +end + +Spree::Order.prepend(SolidusSubscriptions::Spree::Order::SubscriptionLineItemsAssociation) diff --git a/app/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb b/app/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb new file mode 100644 index 0000000..1d5524f --- /dev/null +++ b/app/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb @@ -0,0 +1,20 @@ +# Spree::Users maintain a list of the subscriptions associated with them +module SolidusSubscriptions + module Spree + module User + module HaveManySubscriptions + def self.prepended(base) + base.has_many( + :subscriptions, + class_name: 'SolidusSubscriptions::Subscription', + foreign_key: 'user_id' + ) + + base.accepts_nested_attributes_for :subscriptions + end + end + end + end +end + +Spree.user_class.prepend(SolidusSubscriptions::Spree::User::HaveManySubscriptions) diff --git a/app/decorators/models/solidus_subscriptions/spree/variant/variant_pretty_name.rb b/app/decorators/models/solidus_subscriptions/spree/variant/variant_pretty_name.rb new file mode 100644 index 0000000..9b2a5f8 --- /dev/null +++ b/app/decorators/models/solidus_subscriptions/spree/variant/variant_pretty_name.rb @@ -0,0 +1,15 @@ +module SolidusSubscriptions + module Spree + module Variant + module VariantPrettyName + def pretty_name + name = product.name + name += " - #{options_text}" if options_text.present? + name + end + end + end + end +end + +Spree::Variant.prepend(SolidusSubscriptions::Spree::Variant::VariantPrettyName) diff --git a/app/decorators/spree/controllers/api/line_items/create_subscription_line_items.rb b/app/decorators/spree/controllers/api/line_items/create_subscription_line_items.rb deleted file mode 100644 index 541bd2e..0000000 --- a/app/decorators/spree/controllers/api/line_items/create_subscription_line_items.rb +++ /dev/null @@ -1,28 +0,0 @@ -# Create new subscription line items associated to the current order, when -# a line item is added to the cart which includes subscription_line_item -# params. -# -# The Subscriptions::LineItem acts as a line item place holder for a -# Subscription, indicating that it has been added to the order, but not -# yet purchased -module Spree - module Controllers::Api::LineItems::CreateSubscriptionLineItems - include SolidusSubscriptions::SubscriptionLineItemBuilder - - def self.prepended(base) - base.after_action( - :handle_subscription_line_items, - only: [:create, :update], - if: ->{ params[:subscription_line_item] } - ) - end - - private - - def handle_subscription_line_items - create_subscription_line_item(@line_item) - end - end -end - -Spree::Api::LineItemsController.prepend(Spree::Controllers::Api::LineItems::CreateSubscriptionLineItems) diff --git a/app/decorators/spree/controllers/orders/create_subscription_line_items.rb b/app/decorators/spree/controllers/orders/create_subscription_line_items.rb deleted file mode 100644 index 4f8c1d0..0000000 --- a/app/decorators/spree/controllers/orders/create_subscription_line_items.rb +++ /dev/null @@ -1,33 +0,0 @@ -# Create new subscription line items associated to the current order, when -# a line item is added to the cart which includes subscription_line_item -# params. -# -# The Subscriptions::LineItem acts as a line item place holder for a -# Subscription, indicating that it has been added to the order, but not -# yet purchased -module Spree - module Controllers - module Orders - module CreateSubscriptionLineItems - include SolidusSubscriptions::SubscriptionLineItemBuilder - - def self.prepended(base) - base.after_action( - :handle_subscription_line_items, - only: :populate, - if: ->{ params[:subscription_line_item] } - ) - end - - private - - def handle_subscription_line_items - line_item = @current_order.line_items.find_by(variant_id: params[:variant_id]) - create_subscription_line_item(line_item) - end - end - end - end -end - -Spree::OrdersController.prepend(Spree::Controllers::Orders::CreateSubscriptionLineItems) diff --git a/app/decorators/spree/line_items/subscription_line_items_association.rb b/app/decorators/spree/line_items/subscription_line_items_association.rb deleted file mode 100644 index 6e58d29..0000000 --- a/app/decorators/spree/line_items/subscription_line_items_association.rb +++ /dev/null @@ -1,22 +0,0 @@ -# Each Spree::LineItem can have multiple subscription_line_items. This -# allows a cart to represent multiple subscriptions to the same item in -# the same order. -module Spree - module LineItems - module SubscriptionLineItemsAssociation - def self.prepended(base) - base.has_many( - :subscription_line_items, - class_name: 'SolidusSubscriptions::LineItem', - foreign_key: :spree_line_item_id, - inverse_of: :spree_line_item, - dependent: :destroy - ) - - base.accepts_nested_attributes_for :subscription_line_items - end - end - end -end - -Spree::LineItem.prepend Spree::LineItems::SubscriptionLineItemsAssociation diff --git a/app/decorators/spree/orders/after_create.rb b/app/decorators/spree/orders/after_create.rb deleted file mode 100644 index 8bbd718..0000000 --- a/app/decorators/spree/orders/after_create.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Spree - module Orders - module AfterCreate - def ensure_line_items_present - super unless subscription_order? - end - - def send_cancel_email - super unless subscription_order? - end - end - - Order.prepend(AfterCreate) - end -end diff --git a/app/decorators/spree/orders/finalize_creates_subscriptions.rb b/app/decorators/spree/orders/finalize_creates_subscriptions.rb deleted file mode 100644 index a63c1a5..0000000 --- a/app/decorators/spree/orders/finalize_creates_subscriptions.rb +++ /dev/null @@ -1,19 +0,0 @@ -# Once an order is finalized its subscriptions line items should be converted -# into active subscriptions. This hooks into Spree::Order#finalize! and -# passes all subscription_line_items present on the order to the Subscription -# generator which will build and persist the subscriptions -module Spree - module Orders - module FinalizeCreatesSubscriptions - def finalize! - SolidusSubscriptions::SubscriptionGenerator.group(subscription_line_items).each do |line_items| - SolidusSubscriptions::SubscriptionGenerator.activate(line_items) - end - - super - end - end - end -end - -Spree::Order.prepend Spree::Orders::FinalizeCreatesSubscriptions diff --git a/app/decorators/spree/orders/subscription_line_items_association.rb b/app/decorators/spree/orders/subscription_line_items_association.rb deleted file mode 100644 index 609c19b..0000000 --- a/app/decorators/spree/orders/subscription_line_items_association.rb +++ /dev/null @@ -1,15 +0,0 @@ -# Spree::Orders may contain many subscription_line_items. When the order is -# finalized these subscription_line_items are converted into subscritpions. -# The order needs to be able to get a list of associated subscription_line_items -# to be able to populate the full subscriptions. -module Spree - module Orders - module SubscriptionLineItemsAssociation - def self.prepended(base) - base.has_many :subscription_line_items, through: :line_items - end - end - end -end - -Spree::Order.prepend Spree::Orders::SubscriptionLineItemsAssociation diff --git a/app/decorators/spree/users/have_many_subscriptions.rb b/app/decorators/spree/users/have_many_subscriptions.rb deleted file mode 100644 index 43b4cb2..0000000 --- a/app/decorators/spree/users/have_many_subscriptions.rb +++ /dev/null @@ -1,18 +0,0 @@ -# Spree::Users maintain a list of the subscriptions associated with them -module Spree - module Users - module HaveManySubscriptions - def self.prepended(base) - base.has_many( - :subscriptions, - class_name: 'SolidusSubscriptions::Subscription', - foreign_key: 'user_id' - ) - - base.accepts_nested_attributes_for :subscriptions - end - end - end -end - -Spree.user_class.prepend(Spree::Users::HaveManySubscriptions) diff --git a/app/decorators/spree/variants/variant_pretty_name.rb b/app/decorators/spree/variants/variant_pretty_name.rb deleted file mode 100644 index c9826bf..0000000 --- a/app/decorators/spree/variants/variant_pretty_name.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Spree - module Variants - module VariantPrettyName - def pretty_name - name = product.name - name += " - #{options_text}" if options_text.present? - name - end - end - end -end - -Spree::Variant.prepend Spree::Variants::VariantPrettyName diff --git a/app/models/solidus_subscriptions/checkout.rb b/app/models/solidus_subscriptions/checkout.rb index aa1f781..d7e144c 100644 --- a/app/models/solidus_subscriptions/checkout.rb +++ b/app/models/solidus_subscriptions/checkout.rb @@ -54,10 +54,10 @@ module SolidusSubscriptions # # @return [Spree::Order] def order - @order ||= Spree::Order.create( + @order ||= ::Spree::Order.create( user: user, email: user.email, - store: subscription.store || Spree::Store.default, + store: subscription.store || ::Spree::Store.default, subscription_order: true ) end @@ -65,7 +65,7 @@ module SolidusSubscriptions private def checkout - if Spree.solidus_gem_version >= Gem::Version.new('2.4.0') + if ::Spree.solidus_gem_version >= Gem::Version.new('2.4.0') order.recalculate else order.update! @@ -134,7 +134,7 @@ module SolidusSubscriptions end def apply_promotions - Spree::PromotionHandler::Cart.new(order).activate + ::Spree::PromotionHandler::Cart.new(order).activate order.updater.update # reload totals end diff --git a/app/models/solidus_subscriptions/installment_detail.rb b/app/models/solidus_subscriptions/installment_detail.rb index eec7e0f..cfd4e70 100644 --- a/app/models/solidus_subscriptions/installment_detail.rb +++ b/app/models/solidus_subscriptions/installment_detail.rb @@ -8,7 +8,7 @@ module SolidusSubscriptions inverse_of: :details ) - belongs_to(:order, class_name: 'Spree::Order', optional: true) + belongs_to(:order, class_name: '::Spree::Order', optional: true) validates :installment, presence: true alias_attribute :successful, :success diff --git a/app/models/solidus_subscriptions/line_item.rb b/app/models/solidus_subscriptions/line_item.rb index e5f8a2d..d5e8738 100644 --- a/app/models/solidus_subscriptions/line_item.rb +++ b/app/models/solidus_subscriptions/line_item.rb @@ -20,11 +20,11 @@ module SolidusSubscriptions belongs_to( :spree_line_item, - class_name: 'Spree::LineItem', + class_name: '::Spree::LineItem', inverse_of: :subscription_line_items, optional: true, ) - has_one :order, through: :spree_line_item, class_name: 'Spree::Order' + has_one :order, through: :spree_line_item, class_name: '::Spree::Order' belongs_to( :subscription, class_name: 'SolidusSubscriptions::Subscription', @@ -68,7 +68,7 @@ module SolidusSubscriptions # subscription orders. It is a frozen duplicate of the current order and # cannot be saved def dummy_order - order = spree_line_item ? spree_line_item.order.dup : Spree::Order.create + order = spree_line_item ? spree_line_item.order.dup : ::Spree::Order.create order.ship_address = subscription.shipping_address || subscription.user.ship_address if subscription order.freeze diff --git a/app/models/solidus_subscriptions/line_item_builder.rb b/app/models/solidus_subscriptions/line_item_builder.rb index d572a14..b288629 100644 --- a/app/models/solidus_subscriptions/line_item_builder.rb +++ b/app/models/solidus_subscriptions/line_item_builder.rb @@ -25,7 +25,7 @@ module SolidusSubscriptions raise UnsubscribableError.new(variant) unless variant.subscribable? next unless variant.can_supply?(subscription_line_item.quantity) - Spree::LineItem.new(variant: variant, quantity: subscription_line_item.quantity) + ::Spree::LineItem.new(variant: variant, quantity: subscription_line_item.quantity) end # Either all line items for an installment are fulfilled or none are @@ -38,7 +38,7 @@ module SolidusSubscriptions return @subscribables if @subscribables ids = subscription_line_items.map(&:subscribable_id) - @subscribables ||= Spree::Variant.find(ids).index_by(&:id) + @subscribables ||= ::Spree::Variant.find(ids).index_by(&:id) end end end diff --git a/app/models/solidus_subscriptions/subscription.rb b/app/models/solidus_subscriptions/subscription.rb index 2f0ac14..f10bf15 100644 --- a/app/models/solidus_subscriptions/subscription.rb +++ b/app/models/solidus_subscriptions/subscription.rb @@ -7,11 +7,11 @@ module SolidusSubscriptions PROCESSING_STATES = [:pending, :failed, :success] - belongs_to :user, class_name: Spree.user_class.to_s + belongs_to :user, class_name: "::#{::Spree.user_class}" has_many :line_items, class_name: 'SolidusSubscriptions::LineItem', inverse_of: :subscription has_many :installments, class_name: 'SolidusSubscriptions::Installment' - belongs_to :store, class_name: 'Spree::Store' - belongs_to :shipping_address, class_name: 'Spree::Address', optional: true + belongs_to :store, class_name: '::Spree::Store' + belongs_to :shipping_address, class_name: '::Spree::Address', optional: true validates :user, presence: :true validates :skip_count, :successive_skip_count, presence: true, numericality: { greater_than_or_equal_to: 0 } diff --git a/app/models/solidus_subscriptions/subscription_line_item_builder.rb b/app/models/solidus_subscriptions/subscription_line_item_builder.rb index df377ba..ab43c78 100644 --- a/app/models/solidus_subscriptions/subscription_line_item_builder.rb +++ b/app/models/solidus_subscriptions/subscription_line_item_builder.rb @@ -8,8 +8,8 @@ module SolidusSubscriptions ) # Rerun the promotion handler to pickup subscription promotions - Spree::PromotionHandler::Cart.new(line_item.order).activate - if Spree.solidus_gem_version >= Gem::Version.new('2.4.0') + ::Spree::PromotionHandler::Cart.new(line_item.order).activate + if ::Spree.solidus_gem_version >= Gem::Version.new('2.4.0') line_item.order.recalculate else line_item.order.update! diff --git a/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb b/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb index 6d383da..b78b96a 100644 --- a/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb +++ b/app/models/solidus_subscriptions/subscription_order_promotion_rule.rb @@ -1,5 +1,5 @@ module SolidusSubscriptions - class SubscriptionOrderPromotionRule < Spree::PromotionRule + class SubscriptionOrderPromotionRule < ::Spree::PromotionRule # Promotion can be applied to an entire order. Will only be true # for Spree::Order # @@ -8,7 +8,7 @@ module SolidusSubscriptions # # @return [Boolean] def applicable?(promotable) - promotable.is_a? Spree::Order + promotable.is_a? ::Spree::Order end # An order is eligible if it fulfills a subscription Installment. Will only diff --git a/app/models/solidus_subscriptions/subscription_promotion_rule.rb b/app/models/solidus_subscriptions/subscription_promotion_rule.rb index 7a8cc55..9a76136 100644 --- a/app/models/solidus_subscriptions/subscription_promotion_rule.rb +++ b/app/models/solidus_subscriptions/subscription_promotion_rule.rb @@ -1,5 +1,5 @@ module SolidusSubscriptions - class SubscriptionPromotionRule < Spree::PromotionRule + class SubscriptionPromotionRule < ::Spree::PromotionRule # Promotion can be applied to an entire order. Will only be true # for Spree::Order # @@ -8,7 +8,7 @@ module SolidusSubscriptions # # @return [Boolean] def applicable?(promotable) - promotable.is_a? Spree::Order + promotable.is_a? ::Spree::Order end # An order is eligible if it contains a line item with an associates diff --git a/lib/solidus_subscriptions/engine.rb b/lib/solidus_subscriptions/engine.rb index 7b9f336..39cb754 100644 --- a/lib/solidus_subscriptions/engine.rb +++ b/lib/solidus_subscriptions/engine.rb @@ -33,8 +33,8 @@ module SolidusSubscriptions end initializer 'subscriptions_backend' do - next unless Spree::Backend::Config.respond_to?(:menu_items) - Spree::Backend::Config.configure do |config| + next unless ::Spree::Backend::Config.respond_to?(:menu_items) + ::Spree::Backend::Config.configure do |config| config.menu_items << config.class::MenuItem.new( [:subscriptions], 'repeat', @@ -45,7 +45,7 @@ module SolidusSubscriptions end def self.activate - Spree::Ability.register_ability(SolidusSubscriptions::Ability) + ::Spree::Ability.register_ability(SolidusSubscriptions::Ability) end config.to_prepare(&method(:activate).to_proc) diff --git a/lib/solidus_subscriptions/permitted_attributes.rb b/lib/solidus_subscriptions/permitted_attributes.rb index 2f50414..c93b10b 100644 --- a/lib/solidus_subscriptions/permitted_attributes.rb +++ b/lib/solidus_subscriptions/permitted_attributes.rb @@ -5,13 +5,13 @@ module SolidusSubscriptions module PermittedAttributes class << self def update_spree_permiteed_attributes - Spree::PermittedAttributes.line_item_attributes << { + ::Spree::PermittedAttributes.line_item_attributes << { subscription_line_items_attributes: nested( subscription_line_item_attributes ) } - Spree::PermittedAttributes.user_attributes << { + ::Spree::PermittedAttributes.user_attributes << { subscriptions_attributes: nested(subscription_attributes) } end diff --git a/lib/solidus_subscriptions/processor.rb b/lib/solidus_subscriptions/processor.rb index 0e38708..ef3ec59 100644 --- a/lib/solidus_subscriptions/processor.rb +++ b/lib/solidus_subscriptions/processor.rb @@ -22,7 +22,7 @@ module SolidusSubscriptions subscriptions = SolidusSubscriptions::Subscription.arel_table installments = SolidusSubscriptions::Installment.arel_table - Spree::User. + ::Spree::User. joins(:subscriptions). joins( subscriptions. diff --git a/spec/controllers/orders/create_subscription_line_items_spec.rb b/spec/controllers/orders/create_subscription_line_items_spec.rb deleted file mode 100644 index eac1b69..0000000 --- a/spec/controllers/orders/create_subscription_line_items_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'spec_helper' - -RSpec.describe Spree::Controllers::Orders::CreateSubscriptionLineItems, type: :controller do - controller(Spree::OrdersController) {} - routes { Spree::Core::Engine.routes } - - let!(:user) { create :user } - let!(:store) { create :store } - - before do - allow(controller).to receive_messages(try_spree_current_user: user) - end - - describe 'POST /orders/populate' do - subject { post :populate, params: params } - - let!(:variant) { create :variant } - let(:params) { line_item_params } - let(:line_item_params) do - { - quantity: 1, - variant_id: variant.id - } - end - - shared_examples 'a new order line item' do - it { is_expected.to redirect_to cart_path } - - it 'creates an order' do - expect { subject }. - to change { Spree::Order.count }. - from(0).to(1) - end - - it 'creates a line item' do - expect { subject }. - to change { Spree::LineItem.count }. - from(0).to(1) - end - end - - context 'with subscription_line_item params' do - let(:params) { line_item_params.merge(subscription_line_item_params) } - let(:subscription_line_item_params) do - { - subscription_line_item: { - quantity: 2, - end_date: Date.parse('2000/10/11'), - subscribable_id: variant.id, - interval_length: 30, - interval_units: "day" - } - } - end - - it_behaves_like 'a new order line item' - - it 'creates a new subscription line item' do - expect { subject }. - to change { SolidusSubscriptions::LineItem.count }. - from(0).to(1) - end - - it 'creates a subscription line item with the correct values' do - subject - subscription_line_item = SolidusSubscriptions::LineItem.last - - expect(subscription_line_item).to have_attributes( - subscription_line_item_params[:subscription_line_item] - ) - end - end - - context 'without subscription_line_item params' do - it_behaves_like 'a new order line item' - end - end -end diff --git a/spec/decorators/controllers/solidus_subscriptions/spree/orders/create_subscription_line_items_spec.rb b/spec/decorators/controllers/solidus_subscriptions/spree/orders/create_subscription_line_items_spec.rb new file mode 100644 index 0000000..3018c0e --- /dev/null +++ b/spec/decorators/controllers/solidus_subscriptions/spree/orders/create_subscription_line_items_spec.rb @@ -0,0 +1,78 @@ +require 'spec_helper' + +RSpec.describe SolidusSubscriptions::Spree::OrdersController::CreateSubscriptionLineItems, type: :controller do + controller(Spree::OrdersController) {} + routes { Spree::Core::Engine.routes } + + let!(:user) { create :user } + let!(:store) { create :store } + + before do + allow(controller).to receive_messages(try_spree_current_user: user) + end + + describe 'POST /orders/populate' do + subject { post :populate, params: params } + + let!(:variant) { create :variant } + let(:params) { line_item_params } + let(:line_item_params) do + { + quantity: 1, + variant_id: variant.id + } + end + + shared_examples 'a new order line item' do + it { is_expected.to redirect_to cart_path } + + it 'creates an order' do + expect { subject }. + to change { Spree::Order.count }. + from(0).to(1) + end + + it 'creates a line item' do + expect { subject }. + to change { Spree::LineItem.count }. + from(0).to(1) + end + end + + context 'with subscription_line_item params' do + let(:params) { line_item_params.merge(subscription_line_item_params) } + let(:subscription_line_item_params) do + { + subscription_line_item: { + quantity: 2, + end_date: Date.parse('2000/10/11'), + subscribable_id: variant.id, + interval_length: 30, + interval_units: "day" + } + } + end + + it_behaves_like 'a new order line item' + + it 'creates a new subscription line item' do + expect { subject }. + to change { SolidusSubscriptions::LineItem.count }. + from(0).to(1) + end + + it 'creates a subscription line item with the correct values' do + subject + subscription_line_item = SolidusSubscriptions::LineItem.last + + expect(subscription_line_item).to have_attributes( + subscription_line_item_params[:subscription_line_item] + ) + end + end + + context 'without subscription_line_item params' do + it_behaves_like 'a new order line item' + end + end +end diff --git a/spec/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association_spec.rb b/spec/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association_spec.rb new file mode 100644 index 0000000..1674c5e --- /dev/null +++ b/spec/decorators/models/solidus_subscriptions/spree/line_item/subscription_line_items_association_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +RSpec.describe SolidusSubscriptions::Spree::LineItem::SubscriptionLineItemsAssociation, type: :model do + subject { Spree::LineItem.new } + it { is_expected.to have_many :subscription_line_items } + it { is_expected.to accept_nested_attributes_for :subscription_line_items } +end diff --git a/spec/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscrptions_spec.rb b/spec/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscrptions_spec.rb new file mode 100644 index 0000000..7c4fd8a --- /dev/null +++ b/spec/decorators/models/solidus_subscriptions/spree/order/finalize_creates_subscrptions_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +RSpec.describe SolidusSubscriptions::Spree::Order::FinalizeCreatesSubscriptions do + describe '#finalize!' do + subject { order.finalize! } + + let(:order) { create :order, :with_subscription_line_items } + let(:subscription_line_item) { order.subscription_line_items.last } + let(:expected_actionable_date) { (DateTime.current + subscription_line_item.interval).beginning_of_minute } + + around { |e| Timecop.freeze { e.run } } + + it 'creates new subscriptions' do + expect { subject }. + to change { SolidusSubscriptions::Subscription.count }. + by(order.subscription_line_items.count) + end + + it 'creates a subscription with the correct values' do + subject + subscription = SolidusSubscriptions::Subscription.last + + expect(subscription).to have_attributes( + user_id: order.user_id, + actionable_date: expected_actionable_date, + line_items: [subscription_line_item] + ) + end + end +end diff --git a/spec/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association_spec.rb b/spec/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association_spec.rb new file mode 100644 index 0000000..622e14c --- /dev/null +++ b/spec/decorators/models/solidus_subscriptions/spree/order/subscription_line_items_association_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +RSpec.describe SolidusSubscriptions::Spree::Order::SubscriptionLineItemsAssociation, type: :model do + subject { Spree::Order.new } + + it { is_expected.to have_many :subscription_line_items } +end diff --git a/spec/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb b/spec/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb new file mode 100644 index 0000000..4ef7491 --- /dev/null +++ b/spec/decorators/models/solidus_subscriptions/spree/user/have_many_subscriptions.rb @@ -0,0 +1,8 @@ +require 'spec_helper' + +RSpec.describe SolidusSubscriptions::Spree::User::HaveManySubscritptions, type: :model do + subject { Spree::User.new } + + it { is_expected.to have_many :subscriptions } + it { is_expected.to accept_nested_attributes_for :subscriptions } +end diff --git a/spec/overrides/spree/line_items/subscription_line_items_association_spec.rb b/spec/overrides/spree/line_items/subscription_line_items_association_spec.rb deleted file mode 100644 index 8d55605..0000000 --- a/spec/overrides/spree/line_items/subscription_line_items_association_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'spec_helper' - -RSpec.describe Spree::LineItems::SubscriptionLineItemsAssociation, type: :model do - subject { Spree::LineItem.new } - it { is_expected.to have_many :subscription_line_items } - it { is_expected.to accept_nested_attributes_for :subscription_line_items } -end diff --git a/spec/overrides/spree/orders/finalize_creates_subscrptions_spec.rb b/spec/overrides/spree/orders/finalize_creates_subscrptions_spec.rb deleted file mode 100644 index 76d6166..0000000 --- a/spec/overrides/spree/orders/finalize_creates_subscrptions_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'spec_helper' - -RSpec.describe Spree::Orders::FinalizeCreatesSubscriptions do - describe '#finalize!' do - subject { order.finalize! } - - let(:order) { create :order, :with_subscription_line_items } - let(:subscription_line_item) { order.subscription_line_items.last } - let(:expected_actionable_date) { (DateTime.current + subscription_line_item.interval).beginning_of_minute } - - around { |e| Timecop.freeze { e.run } } - - it 'creates new subscriptions' do - expect { subject }. - to change { SolidusSubscriptions::Subscription.count }. - by(order.subscription_line_items.count) - end - - it 'creates a subscription with the correct values' do - subject - subscription = SolidusSubscriptions::Subscription.last - - expect(subscription).to have_attributes( - user_id: order.user_id, - actionable_date: expected_actionable_date, - line_items: [subscription_line_item] - ) - end - end -end diff --git a/spec/overrides/spree/orders/subscription_line_items_association_spec.rb b/spec/overrides/spree/orders/subscription_line_items_association_spec.rb deleted file mode 100644 index 14fbf34..0000000 --- a/spec/overrides/spree/orders/subscription_line_items_association_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'spec_helper' - -RSpec.describe Spree::Orders::SubscriptionLineItemsAssociation, type: :model do - subject { Spree::Order.new } - - it { is_expected.to have_many :subscription_line_items } -end diff --git a/spec/overrides/spree/users/have_many_subscriptions.rb b/spec/overrides/spree/users/have_many_subscriptions.rb deleted file mode 100644 index 2deccc8..0000000 --- a/spec/overrides/spree/users/have_many_subscriptions.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'spec_helper' - -RSpec.describe Spree::Users::HaveManySubscritptions, type: :model do - subject { Spree::User.new } - - it { is_expected.to have_many :subscriptions } - it { is_expected.to accept_nested_attributes_for :subscriptions } -end -- cgit v1.2.3