summaryrefslogtreecommitdiff
path: root/lib/decorators/api/controllers/solidus_subscriptions/spree/api/line_items_controller/create_subscription_line_items.rb
blob: 7872a2a15a71848b80754713a72fa24de69e4d5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true

# 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)