Sha256: 00f6422bde3deeb57b83e2b2fa4f17dbc4a86d3bf251046204ef12cb13101a81

Contents?: true

Size: 1.27 KB

Versions: 22

Compression:

Stored size: 1.27 KB

Contents

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_attributes(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, :read).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

22 entries across 22 versions & 1 rubygems

Version Path
solidus_api-2.5.2 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.5.1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.5.0 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.5.0.rc1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.5.0.beta2 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.5.0.beta1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.4.2 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.4.1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.3.1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.2.2 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.4.0 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.4.0.rc1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.4.0.beta1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.3.0 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.3.0.rc3 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.3.0.rc2 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.3.0.rc1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.3.0.beta1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.2.1 app/controllers/spree/api/inventory_units_controller.rb
solidus_api-2.2.0 app/controllers/spree/api/inventory_units_controller.rb