Skip to content

Commit

Permalink
Cleanup the duplicated fields on subscription creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattia Roccoberton committed Feb 12, 2021
1 parent e526705 commit 9cd46dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/solidus_subscriptions/subscription_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def activate(subscription_line_items)

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

Expand All @@ -54,6 +56,15 @@ def group(subscription_line_items)

private

def cleanup_subscription_line_items(subscription_line_items)
ids = subscription_line_items.pluck :id
SolidusSubscriptions::LineItem.where(id: ids).update_all(
interval_length: nil,
interval_units: nil,
end_date: nil
)
end

def subscription_configuration(subscription_line_item)
SubscriptionConfiguration.new(
subscription_line_item.interval_length,
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/solidus_subscriptions/subscription_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
payment_source: payment_source,
)
end

it 'cleanups the subscription line items fields duplicated on the subscription' do
subscription_line_item = build(:subscription_line_item)

described_class.activate([subscription_line_item])

attrs = %i[interval_length interval_units end_date]
expect(subscription_line_item.reload.slice(attrs).values).to eq [nil, nil, nil]
end
end

describe '.group' do
Expand Down

0 comments on commit 9cd46dc

Please sign in to comment.