Skip to content

Commit

Permalink
Fix view for orders api.
Browse files Browse the repository at this point in the history
Refactor _big.json.jbuilder so that it dynamically loads the payment
source view based on that source's payment method.

Add payment views for each payment method which will be used to render a
payment source as json.
  • Loading branch information
Taylor Scott committed Apr 2, 2018
1 parent 0666737 commit 6bbe885
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
9 changes: 4 additions & 5 deletions api/app/views/spree/api/orders/_big.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ json.payments(order.payments) do |payment|
json.payment_method { json.(payment.payment_method, :id, :name) }
json.source do
if payment.source
json.(payment.source, *payment_source_attributes)

if @current_user_roles.include?("admin")
json.(payment.source, :gateway_customer_profile_id, :gateway_payment_profile_id)
end
json.partial!(
"spree/api/payments/source_views/#{payment.payment_method.partial_name}",
payment_source: payment.source
)
else
json.nil!
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.nil!
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
attrs = [:id, :month, :year, :cc_type, :last_digits, :name]
if @current_user_roles.include?("admin")
attrs += [:gateway_customer_profile_id, :gateway_payment_profile_id]
end

json.(payment_source, *attrs)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.(payment_source, :id, :memo, :created_at)
json.created_by payment_source.created_by.email
json.category payment_source.category, :id, :name
15 changes: 15 additions & 0 deletions api/spec/requests/spree/api/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,21 @@ module Spree
expect(credit_cards[0]['id']).to eq payment.source.id
expect(credit_cards[0]['address']['id']).to eq credit_card.address_id
end

it 'renders the payment source view for gateway' do
subject
expect(response).to render_template partial: 'spree/api/payments/source_views/_gateway'
end
end

context 'when store credit is present' do
let!(:payment) { create(:store_credit_payment, order: order, source: store_credit) }
let(:store_credit) { create(:store_credit) }

it 'renders the payment source view for store credit' do
subject
expect(response).to render_template partial: 'spree/api/payments/source_views/_storecredit'
end
end
end

Expand Down

0 comments on commit 6bbe885

Please sign in to comment.