Sha256: 872483458afc17e1f3de70bceeb1a72fe3c95bf25c2ddf8d536eca4d392ef88f

Contents?: true

Size: 1.29 KB

Versions: 84

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Spree
  module Api
    class InventoryUnitsController < Spree::Api::BaseController
      before_action :prepare_event, only: :update

      def show
        @inventory_unit = inventory_unit
        respond_with(@inventory_unit)
      end

      def update
        authorize! :update, inventory_unit.order

        inventory_unit.transaction do
          if inventory_unit.update(inventory_unit_params)
            fire
            render :show, status: 200
          else
            invalid_resource!(inventory_unit)
          end
        end
      end

      private

      def inventory_unit
        @inventory_unit ||= Spree::InventoryUnit.accessible_by(current_ability, :show).find(params[:id])
      end

      def prepare_event
        return unless @event = params[:fire]

        can_event = "can_#{@event}?"

        unless inventory_unit.respond_to?(can_event) &&
               inventory_unit.send(can_event)
          render json: { exception: "cannot transition to #{@event}" },
                 status: 200
          false
        end
      end

      def fire
        inventory_unit.send("#{@event}!") if @event
      end

      def inventory_unit_params
        params.require(:inventory_unit).permit(permitted_inventory_unit_attributes)
      end
    end
  end
end

Version data entries

84 entries across 84 versions & 1 rubygems

Version Path
solidus_api-3.2.5 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.2.4 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.2.3 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.1.8 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.0.8 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.2.2 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.2.1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.2.0 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.2.0.alpha app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.0.7 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.11.17 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.1.7 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.11.16 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.0.6 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.1.6 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.11.15 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.11.14 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.0.5 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-3.1.5 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.11.13 app/controllers/spree/api/inventory_units_controller.rb