Module: SolidusSubscriptions::SubscriptionGenerator

Extended by:
SubscriptionGenerator
Included in:
SubscriptionGenerator
Defined in:
lib/solidus_subscriptions/subscription_generator.rb

Defined Under Namespace

Classes: SubscriptionConfiguration

Instance Method Summary collapse

Instance Method Details

#activate(subscription_line_items) ⇒ SolidusSubscriptions::Subscription

Create and persist a subscription for a collection of subscription

line items

Parameters:

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/solidus_subscriptions/subscription_generator.rb', line 24

def activate(subscription_line_items)
  return if subscription_line_items.empty?

  order = subscription_line_items.first.order
  configuration = subscription_configuration(subscription_line_items.first)

  subscription_attributes = {
    user: order.user,
    line_items: subscription_line_items,
    store: order.store,
    shipping_address: order.ship_address,
    billing_address: order.bill_address,
    payment_source: order.payments.valid.last&.payment_source,
    payment_method: order.payments.valid.last&.payment_method,
    currency: order.currency,
    **configuration.to_h
  }

  Subscription.create!(subscription_attributes) do |sub|
    sub.actionable_date = sub.next_actionable_date
  end.tap do |_subscription|
    cleanup_subscription_line_items(subscription_line_items)
  end
end

#call(order) ⇒ Object



11
12
13
14
15
# File 'lib/solidus_subscriptions/subscription_generator.rb', line 11

def call(order)
  group(order.subscription_line_items).each { |line_items| activate(line_items) }

  order.reload
end

#group(subscription_line_items) ⇒ Array<Array<SolidusSubscriptions::LineItem>>

Group a collection of line items by common subscription configuration options. Grouped subscription_line_items can belong to a single subscription.

Parameters:

Returns:



57
58
59
60
61
62
# File 'lib/solidus_subscriptions/subscription_generator.rb', line 57

def group(subscription_line_items)
  subscription_line_items.group_by do |li|
    subscription_configuration(li)
  end.
    values
end