Skip to content

Commit

Permalink
SubscriptionGenerator#group
Browse files Browse the repository at this point in the history
Add a method to group subscription line items by their subscription
configuration attributes:

- interval_length
- interval_units
- max_installments

This method should be used to group an order's subscription_line_items
into groups which can be fulfilled by a single subscription.
  • Loading branch information
Brendan Deere committed Feb 3, 2017
1 parent b01c9d0 commit 9fba316
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
31 changes: 30 additions & 1 deletion app/models/solidus_subscriptions/subscription_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
# objects and creating SolidusSubscriptions::Subscription Objects
module SolidusSubscriptions
module SubscriptionGenerator
extend self

SubscriptionConfiguration = Struct.new(:interval_length, :interval_units, :end_date)

# Create and persist a subscription for a collection of subscription
# line items
#
# @param subscription_line_items [Array<SolidusSubscriptions::LineItem>] The
# subscription_line_items to be activated
#
# @return [SolidusSubscriptions::Subscription]
def self.activate(subscription_line_items)
def activate(subscription_line_items)
return if subscription_line_items.empty?

order = subscription_line_items.first.order
Expand All @@ -25,5 +29,30 @@ def self.activate(subscription_line_items)
sub.actionable_date = sub.next_actionable_date
end
end

# Group a collection of line items by common subscription configuration
# options. Grouped subscription_line_items can belong to a single
# subscription.
#
# @param subscription_line_items [Array<SolidusSubscriptions::LineItem>] The
# subscription_line_items to be grouped.
#
# @return [Array<Array<SolidusSubscriptions::LineItem>>]
def group(subscription_line_items)
subscription_line_items.group_by do |li|
subscription_configuration(li)
end.
values
end

private

def subscription_configuration(subscription_line_item)
SubscriptionConfiguration.new(
subscription_line_item.interval_length,
subscription_line_item.interval_units,
subscription_line_item.end_date
)
end
end
end
17 changes: 17 additions & 0 deletions spec/models/solidus_subscriptions/subscription_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,21 @@
)
end
end

describe '.group' do
subject { described_class.group(subscription_line_items) }

let(:monthly_subscriptions) { build_stubbed_list :subscription_line_item, 2 }
let(:bimonthly_subscriptions) { build_stubbed_list :subscription_line_item, 2, interval_length: 2 }
let(:weekly_subscriptions) { build_stubbed_list :subscription_line_item, 2, interval_units: :week }
let(:expiring_subscriptions) { build_stubbed_list :subscription_line_item, 2, end_date: DateTime.current.tomorrow }

let(:subscription_line_items) { monthly_subscriptions + bimonthly_subscriptions + weekly_subscriptions + expiring_subscriptions }

it { is_expected.to be_a Array }
it { is_expected.to include monthly_subscriptions }
it { is_expected.to include bimonthly_subscriptions }
it { is_expected.to include weekly_subscriptions }
it { is_expected.to include expiring_subscriptions }
end
end

0 comments on commit 9fba316

Please sign in to comment.