Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve admin return authorization controller #2420

Merged
merged 3 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ class ReturnAuthorizationsController < ResourceController
update.fails :load_form_data

def fire
@return_authorization.send("#{params[:e]}!")
action_from_params = "#{params[:e]}!"

if @return_authorization.state_events.include?(params[:e].to_sym) &&
@return_authorization.send(action_from_params)

flash_message = { success: t('spree.return_authorization_updated') }
else
flash_message = { error: t('spree.return_authorization_fire_error') }
end

redirect_back(fallback_location: admin_order_return_authorizations_path(@order),
flash: { success: t('spree.return_authorization_updated') })
flash: flash_message)
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,55 @@
let(:inventory_unit_2) { order.inventory_units.order('id asc')[1] }
let(:inventory_unit_3) { order.inventory_units.order('id asc')[2] }

describe '#fire' do
let(:return_authorization) { create(:return_authorization, order: order) }

context 'with the event parameter set' do
let(:params) do
{
id: return_authorization.to_param,
order_id: return_authorization.order.to_param,
e: event,
}
end

context 'when event method exists on return authorization' do
let(:event) { 'cancel' }

it 'sends method with ! to return authorization and redirect back' do
get :fire, params: params

expect(response).to redirect_to(admin_order_return_authorizations_path(order))
expect(flash[:success]).to eq 'Return merchandise authorization updated'
end
end

context 'when event method does not exist on return authorization' do
let(:event) { 'do_something_crazy' }

it 'redirects back with an error message' do
get :fire, params: params

expect(response).to redirect_to(admin_order_return_authorizations_path(order))
expect(flash[:error]).to eq 'Cannot perform this action on return merchandise authorization'
end
end

context 'when event method exists but it is not a state machine event' do
let(:event) { 'destroy' }

it 'redirects back with an error message' do
expect(return_authorization).not_to receive :destroy!

get :fire, params: params

expect(response).to redirect_to(admin_order_return_authorizations_path(order))
expect(flash[:error]).to eq 'Cannot perform this action on return merchandise authorization'
end
end
end
end

describe "#load_return_reasons" do
let!(:inactive_rma_reason) { create(:return_reason, active: false) }

Expand Down
1 change: 1 addition & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,7 @@ en:
authorized: Authorized
canceled: Canceled
return_authorizations: Return Merchandise Authorizations
return_authorization_fire_error: Cannot perform this action on return merchandise authorization
return_authorization_updated: Return merchandise authorization updated
return_item_inventory_unit_ineligible: Return item's inventory unit must be shipped
return_item_inventory_unit_reimbursed: Return item's inventory unit is already reimbursed
Expand Down