Class: Spree::Api::ShipmentsController
- Inherits:
-
BaseController
- Object
- ActionController::Base
- BaseController
- Spree::Api::ShipmentsController
- Defined in:
- app/controllers/spree/api/shipments_controller.rb
Instance Attribute Summary
Attributes inherited from BaseController
Instance Method Summary collapse
- #add ⇒ Object
- #create ⇒ Object
- #estimated_rates ⇒ Object
- #mine ⇒ Object
- #ready ⇒ Object
- #remove ⇒ Object
- #select_shipping_method ⇒ Object
- #ship ⇒ Object
- #transfer_to_location ⇒ Object
- #transfer_to_shipment ⇒ Object
- #update ⇒ Object
Instance Method Details
#add ⇒ Object
77 78 79 80 81 82 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 77 def add quantity = params[:quantity].to_i @shipment.order.contents.add(variant, quantity, { shipment: @shipment }) respond_with(@shipment, default_template: :show) end |
#create ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 39 def create :create, Shipment quantity = params[:quantity].to_i @shipment = @order.shipments.create(stock_location_id: params.fetch(:stock_location_id)) @order.contents.add(variant, quantity, { shipment: @shipment }) @shipment.save! respond_with(@shipment.reload, default_template: :show) end |
#estimated_rates ⇒ Object
25 26 27 28 29 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 25 def estimated_rates :update, @shipment estimator = Spree::Config.stock.estimator_class.new @shipping_rates = estimator.shipping_rates(@shipment.to_package, false) end |
#mine ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 10 def mine if current_api_user @shipments = Spree::Shipment .reverse_chronological .joins(:order) .where(spree_orders: { user_id: current_api_user.id }) .includes(mine_includes) .ransack(params[:q]).result @shipments = paginate(@shipments) else render "spree/api/errors/unauthorized", status: :unauthorized end end |
#ready ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 56 def ready unless @shipment.ready? if @shipment.can_ready? @shipment.ready! else logger.error("cannot_ready_shipment shipment_state=#{@shipment.state}") render('spree/api/shipments/cannot_ready_shipment', status: 422) && return end end respond_with(@shipment, default_template: :show) end |
#remove ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 84 def remove quantity = params[:quantity].to_i if @shipment.shipped? || @shipment.canceled? @shipment.errors.add(:base, :cannot_remove_items_shipment_state, state: @shipment.state) invalid_resource!(@shipment) else @shipment.order.contents.remove(variant, quantity, { shipment: @shipment }) @shipment.reload if @shipment.persisted? respond_with(@shipment, default_template: :show) end end |
#select_shipping_method ⇒ Object
31 32 33 34 35 36 37 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 31 def select_shipping_method :update, @shipment shipping_method = Spree::ShippingMethod.find(params.require(:shipping_method_id)) @shipment.select_shipping_method(shipping_method) @order.recalculate respond_with(@shipment, default_template: :show) end |
#ship ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 68 def ship :ship, @shipment unless @shipment.shipped? @shipment.suppress_mailer = (params[:send_mailer] == 'false') @shipment.ship! end respond_with(@shipment, default_template: :show) end |
#transfer_to_location ⇒ Object
97 98 99 100 101 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 97 def transfer_to_location @desired_stock_location = Spree::StockLocation.find(params[:stock_location_id]) @desired_shipment = @original_shipment.order.shipments.build(stock_location: @desired_stock_location) transfer_to_shipment end |
#transfer_to_shipment ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 103 def transfer_to_shipment @desired_shipment ||= Spree::Shipment.find_by!(number: params[:target_shipment_number]) fulfilment_changer = Spree::FulfilmentChanger.new( current_shipment: @original_shipment, desired_shipment: @desired_shipment, variant: @variant, quantity: @quantity ) if fulfilment_changer.run! render json: { success: true, message: t('spree.shipment_transfer_success') }, status: :accepted else render json: { success: false, message: fulfilment_changer.errors..to_sentence }, status: :accepted end end |
#update ⇒ Object
50 51 52 53 54 |
# File 'app/controllers/spree/api/shipments_controller.rb', line 50 def update @shipment.update_attributes_and_order(shipment_params) respond_with(@shipment.reload, default_template: :show) end |