diff options
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | app/models/solidus_subscriptions/installment.rb | 7 | ||||
-rw-r--r-- | app/models/solidus_subscriptions/line_item.rb | 29 | ||||
-rw-r--r-- | app/models/solidus_subscriptions/subscription.rb | 9 | ||||
-rw-r--r-- | lib/solidus_subscriptions.rb | 1 | ||||
-rw-r--r-- | lib/solidus_subscriptions/line_item_builder.rb | 36 | ||||
-rw-r--r-- | spec/lib/solidus_subscriptions/line_item_builder_spec.rb | 33 | ||||
-rw-r--r-- | spec/models/solidus_subscriptions/installment_spec.rb | 9 | ||||
-rw-r--r-- | spec/models/solidus_subscriptions/line_item_spec.rb | 86 | ||||
-rw-r--r-- | spec/models/solidus_subscriptions/subscription_spec.rb | 10 |
10 files changed, 1 insertions, 222 deletions
@@ -62,8 +62,7 @@ This will associate a `SolidusSubscriptions::LineItem` to the line item being ad The customer will not be charged for the subscription until it is processed. The subscription line items should be shown to the user on the cart page by looping over -`Spree::Order#subscription_line_items`. `SolidusSubscriptions::LineItem#dummy_line_item` may be -useful to help you display the subscription line item with your existing cart infrastructure. +`Spree::Order#subscription_line_items`. When the order is finalized, a `SolidusSubscriptions::Subscription` will be created for each group of subscription line items which can be fulfilled by a single subscription. diff --git a/app/models/solidus_subscriptions/installment.rb b/app/models/solidus_subscriptions/installment.rb index 0b5f627..aed1f62 100644 --- a/app/models/solidus_subscriptions/installment.rb +++ b/app/models/solidus_subscriptions/installment.rb @@ -31,13 +31,6 @@ module SolidusSubscriptions unfulfilled.where("#{table_name}.actionable_date <= ?", Time.zone.today) end) - # Get the builder for the subscription_line_item. This will be an - # object that can generate the appropriate line item for the subscribable - # object - # - # @return [SolidusSubscriptions::LineItemBuilder] - delegate :line_item_builder, to: :subscription - # Mark this installment as out of stock. # # @return [SolidusSubscriptions::InstallmentDetail] The record of the failed diff --git a/app/models/solidus_subscriptions/line_item.rb b/app/models/solidus_subscriptions/line_item.rb index 64e6fb0..2583ecc 100644 --- a/app/models/solidus_subscriptions/line_item.rb +++ b/app/models/solidus_subscriptions/line_item.rb @@ -38,34 +38,5 @@ module SolidusSubscriptions validates :subscribable_id, presence: true validates :quantity, numericality: { greater_than: 0 } validates :interval_length, numericality: { greater_than: 0 }, unless: -> { subscription } - - def as_json(**options) - options[:methods] ||= [:dummy_line_item] - super(options) - end - - # Get a placeholder line item for calculating the values of future - # subscription orders. It is frozen and cannot be saved - def dummy_line_item - li = LineItemBuilder.new([self]).spree_line_items.first - return unless li - - li.order = dummy_order - li.validate - li.freeze - end - - private - - # Get a placeholder order for calculating the values of future - # subscription orders. It is a frozen duplicate of the current order and - # cannot be saved - def dummy_order - order = spree_line_item ? spree_line_item.order.dup : ::Spree::Order.create - order.ship_address = subscription.shipping_address || subscription.user.ship_address if subscription - order.bill_address = subscription.billing_address || subscription.user.bill_address if subscription - - order.freeze - end end end diff --git a/app/models/solidus_subscriptions/subscription.rb b/app/models/solidus_subscriptions/subscription.rb index 4168973..15424d8 100644 --- a/app/models/solidus_subscriptions/subscription.rb +++ b/app/models/solidus_subscriptions/subscription.rb @@ -203,15 +203,6 @@ module SolidusSubscriptions actionable_date end - # Get the builder for the subscription_line_item. This will be an - # object that can generate the appropriate line item for the subscribable - # object - # - # @return [SolidusSubscriptions::LineItemBuilder] - def line_item_builder - LineItemBuilder.new(line_items) - end - # The state of the last attempt to process an installment associated to # this subscription # diff --git a/lib/solidus_subscriptions.rb b/lib/solidus_subscriptions.rb index a052504..d5e3d73 100644 --- a/lib/solidus_subscriptions.rb +++ b/lib/solidus_subscriptions.rb @@ -19,7 +19,6 @@ require 'solidus_subscriptions/churn_buster/subscription_payment_method_serializ require 'solidus_subscriptions/churn_buster/subscription_serializer' require 'solidus_subscriptions/churn_buster/order_serializer' require 'solidus_subscriptions/checkout' -require 'solidus_subscriptions/line_item_builder' require 'solidus_subscriptions/subscription_generator' require 'solidus_subscriptions/subscription_line_item_builder' require 'solidus_subscriptions/dispatcher/base' diff --git a/lib/solidus_subscriptions/line_item_builder.rb b/lib/solidus_subscriptions/line_item_builder.rb deleted file mode 100644 index c9aedb9..0000000 --- a/lib/solidus_subscriptions/line_item_builder.rb +++ /dev/null @@ -1,36 +0,0 @@ -# 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 diff --git a/spec/lib/solidus_subscriptions/line_item_builder_spec.rb b/spec/lib/solidus_subscriptions/line_item_builder_spec.rb deleted file mode 100644 index d1e041c..0000000 --- a/spec/lib/solidus_subscriptions/line_item_builder_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'spec_helper' - -RSpec.describe SolidusSubscriptions::LineItemBuilder do - let(:builder) { described_class.new subscription_line_items } - let(:variant) { create(:variant, subscribable: true) } - let(:subscription_line_item) { subscription_line_items.first } - let(:subscription_line_items) do - build_stubbed_list(:subscription_line_item, 1, subscribable_id: variant.id) - end - - describe '#spree_line_items' do - subject { builder.spree_line_items } - - let(:expected_attributes) do - { - variant_id: subscription_line_item.subscribable_id, - quantity: subscription_line_item.quantity - } - end - - it { is_expected.to be_a Array } - - it 'contains a line item with the correct attributes' do - expect(subject.first).to have_attributes expected_attributes - end - - context 'the variant is out of stock' do - before { create :stock_location, backorderable_default: false } - - it { is_expected.to be_empty } - end - end -end diff --git a/spec/models/solidus_subscriptions/installment_spec.rb b/spec/models/solidus_subscriptions/installment_spec.rb index 4e4831e..399a7e6 100644 --- a/spec/models/solidus_subscriptions/installment_spec.rb +++ b/spec/models/solidus_subscriptions/installment_spec.rb @@ -5,15 +5,6 @@ RSpec.describe SolidusSubscriptions::Installment, type: :model do it { is_expected.to validate_presence_of :subscription } - describe '#line_item_builder' do - subject { installment.line_item_builder } - - let(:line_items) { installment.subscription.line_items } - - it { is_expected.to be_a SolidusSubscriptions::LineItemBuilder } - it { is_expected.to have_attributes(subscription_line_items: line_items) } - end - describe '#out_of_stock' do subject { installment.out_of_stock } diff --git a/spec/models/solidus_subscriptions/line_item_spec.rb b/spec/models/solidus_subscriptions/line_item_spec.rb index ad80384..aaa24a5 100644 --- a/spec/models/solidus_subscriptions/line_item_spec.rb +++ b/spec/models/solidus_subscriptions/line_item_spec.rb @@ -24,90 +24,4 @@ RSpec.describe SolidusSubscriptions::LineItem, type: :model do expect(subject.from_now).to eq Date.parse("2016-10-22") end end - - describe '#as_json' do - subject { line_item.as_json } - - around { |e| Timecop.freeze { e.run } } - - let(:line_item) { create(:subscription_line_item, :with_subscription) } - - let(:expected_hash) do - hash = { - "id" => line_item.id, - "spree_line_item_id" => line_item.spree_line_item.id, - "subscription_id" => line_item.subscription_id, - "quantity" => line_item.quantity, - "end_date" => line_item.end_date, - "subscribable_id" => line_item.subscribable_id, - "created_at" => line_item.created_at, - "updated_at" => line_item.updated_at, - "interval_units" => line_item.interval_units, - "interval_length" => line_item.interval_length - } - Rails.gem_version >= Gem::Version.new('6.0.0') ? hash.as_json : hash - end - - it 'includes the attribute values' do - expect(subject).to match a_hash_including(expected_hash) - end - - it 'includes the dummy lineitem' do - expect(subject).to have_key('dummy_line_item') - end - end - - describe '#dummy_line_item' do - subject { line_item.dummy_line_item } - - let(:line_item) { create(:subscription_line_item, :with_subscription) } - - it { is_expected.to be_a Spree::LineItem } - it { is_expected.to be_frozen } - - it 'has the correct variant' do - expect(subject.variant_id).to eq line_item.subscribable_id - end - - context 'with no spree line item' do - let(:line_item) { create(:subscription_line_item, :with_subscription, spree_line_item: nil) } - - it { is_expected.to be_a Spree::LineItem } - it { is_expected.to be_frozen } - - it 'has the correct variant' do - expect(subject.variant_id).to eq line_item.subscribable_id - end - end - - context 'with an associated subscription' do - context 'the associated subscription has a shipping address' do - let(:line_item) do - create(:subscription_line_item, :with_subscription, subscription_traits: [:with_shipping_address]) - end - - it 'uses the subscription shipping address' do - expect(subject.order.ship_address).to eq line_item.subscription.shipping_address - end - - it 'uses the subscription users billing address' do - expect(subject.order.bill_address).to eq line_item.subscription.user.bill_address - end - end - - context 'the associated subscription has a billing address' do - let(:line_item) do - create(:subscription_line_item, :with_subscription, subscription_traits: [:with_billing_address]) - end - - it 'uses the subscription users shipping address' do - expect(subject.order.ship_address).to eq line_item.subscription.user.ship_address - end - - it 'uses the subscription billing address' do - expect(subject.order.bill_address).to eq line_item.subscription.billing_address - end - end - end - end end diff --git a/spec/models/solidus_subscriptions/subscription_spec.rb b/spec/models/solidus_subscriptions/subscription_spec.rb index 5d855db..f305580 100644 --- a/spec/models/solidus_subscriptions/subscription_spec.rb +++ b/spec/models/solidus_subscriptions/subscription_spec.rb @@ -348,16 +348,6 @@ RSpec.describe SolidusSubscriptions::Subscription, type: :model do end end - describe '#line_item_builder' do - subject { subscription.line_item_builder } - - let(:subscription) { create :subscription, :with_line_item } - let(:line_items) { subscription.line_items } - - it { is_expected.to be_a SolidusSubscriptions::LineItemBuilder } - it { is_expected.to have_attributes(subscription_line_items: line_items) } - end - describe '#processing_state' do subject { subscription.processing_state } |