blob: c9aedb9f611b6add162d07de6cd8908d223f4ad8 (
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
# This class is responsible for taking SubscriptionLineItems and building
# them into Spree::LineItems which can be added to an order
module SolidusSubscriptions
class LineItemBuilder
attr_reader :subscription_line_items
# Get a new instance of a LineItemBuilder
#
# @param subscription_line_items[Array<SolidusSubscriptions::LineItem>] The
# subscription line item to be converted into a Spree::LineItem
#
# @return [SolidusSubscriptions::LineItemBuilder]
def initialize(subscription_line_items)
@subscription_line_items = subscription_line_items
end
# Get a new (unpersisted) Spree::LineItem which matches the details of
# :subscription_line_item
#
# @return [Array<Spree::LineItem>]
def spree_line_items
line_items = subscription_line_items.map do |subscription_line_item|
variant = subscription_line_item.subscribable
next unless variant.can_supply?(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
line_items.all? ? line_items : []
end
end
end
|