/api/app/controllers/spree/api/return_authorizations_controller.rb

https://github.com/solidusio/solidus · Ruby · 81 lines · 65 code · 15 blank · 1 comment · 3 complexity · c84c92e2374cb9e1ce3a3572f144eb52 MD5 · raw file

  1. # frozen_string_literal: true
  2. module Spree
  3. module Api
  4. class ReturnAuthorizationsController < Spree::Api::BaseController
  5. before_action :load_order
  6. around_action :lock_order, only: [:create, :update, :destroy, :cancel]
  7. rescue_from Spree::Order::InsufficientStock, with: :insufficient_stock_error
  8. def create
  9. authorize! :create, ReturnAuthorization
  10. @return_authorization = @order.return_authorizations.build(return_authorization_params)
  11. if @return_authorization.save
  12. respond_with(@return_authorization, status: 201, default_template: :show)
  13. else
  14. invalid_resource!(@return_authorization)
  15. end
  16. end
  17. def destroy
  18. @return_authorization = @order.return_authorizations.accessible_by(current_ability, :destroy).find(params[:id])
  19. @return_authorization.destroy
  20. respond_with(@return_authorization, status: 204)
  21. end
  22. def index
  23. authorize! :admin, ReturnAuthorization
  24. @return_authorizations = @order.
  25. return_authorizations.
  26. accessible_by(current_ability).
  27. ransack(params[:q]).
  28. result
  29. @return_authorizations = paginate(@return_authorizations)
  30. respond_with(@return_authorizations)
  31. end
  32. def new
  33. authorize! :admin, ReturnAuthorization
  34. end
  35. def show
  36. authorize! :admin, ReturnAuthorization
  37. @return_authorization = @order.return_authorizations.accessible_by(current_ability, :show).find(params[:id])
  38. respond_with(@return_authorization)
  39. end
  40. def update
  41. @return_authorization = @order.return_authorizations.accessible_by(current_ability, :update).find(params[:id])
  42. if @return_authorization.update(return_authorization_params)
  43. respond_with(@return_authorization, default_template: :show)
  44. else
  45. invalid_resource!(@return_authorization)
  46. end
  47. end
  48. def cancel
  49. @return_authorization = @order.return_authorizations.accessible_by(current_ability, :update).find(params[:id])
  50. if @return_authorization.cancel
  51. respond_with @return_authorization, default_template: :show
  52. else
  53. invalid_resource!(@return_authorization)
  54. end
  55. end
  56. private
  57. def load_order
  58. @order ||= Spree::Order.find_by!(number: order_id)
  59. authorize! :show, @order
  60. end
  61. def return_authorization_params
  62. params.require(:return_authorization).permit(permitted_return_authorization_attributes)
  63. end
  64. end
  65. end
  66. end