-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from gms-electronics/subscription_api_fix_for…
…_subscribable_product Subscription api fix for subscribable product
- Loading branch information
Showing
15 changed files
with
190 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 42 additions & 29 deletions
71
...s_subscriptions/install/templates/app/views/cart_line_items/_subscription_fields.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,52 @@ | ||
<% if @product.subscribable %> | ||
<%= content_tag :h3, t('.subscription_fields') %> | ||
<%= fields_for :'subscription_line_item', SolidusSubscriptions::LineItem.new do |ff| %> | ||
<div> | ||
<%= ff.label :quantity, t('.quantity') %> | ||
<%= ff.number_field :quantity %> | ||
<%= ff.label :quantity, t('.quantity_suffix') %> | ||
</div> | ||
|
||
<div> | ||
<%= ff.label :interval_length, t('.interval_length') %> | ||
<%= ff.number_field :interval_length %> | ||
|
||
<%= ff.collection_radio_buttons :interval_units, SolidusSubscriptions::LineItem.interval_units.to_a, :first, :first %> | ||
</div> | ||
|
||
<%= ff.hidden_field :subscribable_id %> | ||
<div class="subscription-form"> | ||
<% if @product.subscribable %> | ||
<%= content_tag :h3, t('.subscription_fields') %> | ||
<%= fields_for :'subscription_line_item', SolidusSubscriptions::LineItem.new do |ff| %> | ||
<div> | ||
<%= ff.label :quantity, t('.quantity') %> | ||
<%= ff.number_field :quantity %> | ||
<%= ff.label :quantity, t('.quantity_suffix') %> | ||
</div> | ||
|
||
<div> | ||
<%= ff.label :interval_length, t('.interval_length') %> | ||
<%= ff.number_field :interval_length %> | ||
|
||
<%= ff.collection_radio_buttons :interval_units, SolidusSubscriptions::LineItem.interval_units.to_a, :first, :first %> | ||
</div> | ||
|
||
<%= ff.hidden_field :subscribable_id %> <!-- Hidden field for subscribable_id --> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
</div> | ||
|
||
<script> | ||
document.addEventListener("DOMContentLoaded", function(e) { | ||
document.addEventListener("DOMContentLoaded", function() { | ||
var cartForm = document.querySelector('form[action="/cart_line_items"]'); | ||
|
||
cartForm.addEventListener('submit', function(e) { | ||
var variantInput = e.target.querySelector('[name*="variant_id"]:checked'); | ||
var subscribableInput = e.target.querySelector('[name*="subscription_line_item[subscribable_id]"]'); | ||
|
||
if (!variantInput) { | ||
variantInput = cartForm.querySelector('[name="variant_id"]'); | ||
var variantRadioButtons = cartForm.querySelectorAll('[data-js="variant-radio"]'); | ||
var subscribableInput = cartForm.querySelector('[name*="subscription_line_item[subscribable_id]"]'); | ||
var subscriptionFields = document.querySelector('.subscription-form'); // Select the subscription form container | ||
|
||
function handleVariantSelection(e) { | ||
var variantRadioButton = e.target; | ||
var isSubscribable = variantRadioButton.getAttribute('data-subscribable') === 'true'; | ||
|
||
if (isSubscribable) { | ||
subscribableInput.value = variantRadioButton.value; | ||
subscriptionFields.style.display = 'block'; | ||
} else { | ||
subscriptionFields.style.display = 'none'; | ||
} | ||
} | ||
|
||
subscribableInput.value = variantInput.value; | ||
|
||
return true; | ||
variantRadioButtons.forEach(function(variantRadioButton) { | ||
variantRadioButton.addEventListener('change', handleVariantSelection); | ||
}); | ||
|
||
var selectedVariantRadioButton = cartForm.querySelector('[data-js="variant-radio"]:checked'); | ||
if (selectedVariantRadioButton) { | ||
handleVariantSelection({ target: selectedVariantRadioButton }); | ||
} | ||
}); | ||
</script> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
require 'spec_helper' | ||
require_relative '../../../lib/generators/solidus_subscriptions/install/templates/app/controllers/concerns/create_subscription' | ||
|
||
RSpec.describe CreateSubscription, type: :controller do | ||
subject(:controller_instance) do | ||
Class.new(ApplicationController) do | ||
include CreateSubscription | ||
attr_accessor :params, :current_order | ||
|
||
def initialize(params = {}) | ||
@params = params | ||
@current_order = nil | ||
end | ||
end.new | ||
end | ||
|
||
let(:variant) { create(:variant) } | ||
let(:order) { create(:order) } | ||
|
||
before do | ||
controller_instance.current_order = order | ||
end | ||
|
||
describe '#subscription_line_item_params_present?' do | ||
context 'when all required params are present' do | ||
it 'returns true' do | ||
controller_instance.params = { | ||
subscription_line_item: { | ||
subscribable_id: 1, | ||
quantity: 2, | ||
interval_length: 1 | ||
} | ||
} | ||
expect(controller_instance.send(:valid_subscription_line_item_params?)).to be true | ||
end | ||
end | ||
|
||
context 'when required params are missing' do | ||
it 'returns false' do | ||
controller_instance.params = { | ||
subscription_line_item: { | ||
subscribable_id: '', | ||
quantity: '', | ||
interval_length: '' | ||
} | ||
} | ||
expect(controller_instance.send(:valid_subscription_line_item_params?)).to be false | ||
end | ||
end | ||
end | ||
|
||
describe '#handle_subscription_line_items' do | ||
context 'when subscription params are missing' do | ||
it 'does not invoke handle_subscription_line_items and does not create a subscription line item' do | ||
order.line_items.count | ||
|
||
controller_instance.params = { | ||
variant_id: variant.id, | ||
subscription_line_item: {} | ||
} | ||
|
||
expect(controller_instance.send(:valid_subscription_line_item_params?)).to be false | ||
|
||
expect(controller_instance).not_to receive(:handle_subscription_line_items) | ||
|
||
expect(controller_instance).not_to receive(:create_subscription_line_item) | ||
end | ||
end | ||
|
||
context 'when subscription params are present' do | ||
it 'calls create_subscription_line_item with the correct line item' do | ||
line_item = create(:line_item, order: order, variant: variant) | ||
|
||
controller_instance.params = { | ||
variant_id: variant.id, | ||
subscription_line_item: { | ||
subscribable_id: 1, | ||
quantity: 2, | ||
interval_length: 1 | ||
} | ||
} | ||
|
||
allow(order.line_items).to receive(:find_by).with(variant_id: variant.id).and_return(line_item) | ||
|
||
expect(controller_instance).to receive(:create_subscription_line_item).with(line_item) | ||
|
||
controller_instance.send(:handle_subscription_line_items) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters