PageRenderTime 1446ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/api/app/controllers/spree/api/v1/stock_movements_controller.rb

https://gitlab.com/adamlwalker/spree
Ruby | 45 lines | 38 code | 7 blank | 0 comment | 1 complexity | 7c71e792c1038bf5370aa5ac7c825a46 MD5 | raw file
  1. module Spree
  2. module Api
  3. module V1
  4. class StockMovementsController < Spree::Api::BaseController
  5. before_action :stock_location, except: [:update, :destroy]
  6. def index
  7. authorize! :read, StockMovement
  8. @stock_movements = scope.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  9. respond_with(@stock_movements)
  10. end
  11. def show
  12. @stock_movement = scope.find(params[:id])
  13. respond_with(@stock_movement)
  14. end
  15. def create
  16. authorize! :create, StockMovement
  17. @stock_movement = scope.new(stock_movement_params)
  18. if @stock_movement.save
  19. respond_with(@stock_movement, status: 201, default_template: :show)
  20. else
  21. invalid_resource!(@stock_movement)
  22. end
  23. end
  24. private
  25. def stock_location
  26. render 'spree/api/v1/shared/stock_location_required', status: 422 and return unless params[:stock_location_id]
  27. @stock_location ||= StockLocation.accessible_by(current_ability, :read).find(params[:stock_location_id])
  28. end
  29. def scope
  30. @stock_location.stock_movements.accessible_by(current_ability, :read)
  31. end
  32. def stock_movement_params
  33. params.require(:stock_movement).permit(permitted_stock_movement_attributes)
  34. end
  35. end
  36. end
  37. end
  38. end