Sha256: b48574deeab809da334b5715dcd7a2273a6f437954ea59b348571123bcfcdcb7

Contents?: true

Size: 1.39 KB

Versions: 19

Compression:

Stored size: 1.39 KB

Contents

module Spree
  module Api
    module V1
      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 ||= 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 :text => { :exception => "cannot transition to #{@event}" }.to_json,
                   :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
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
spree_api-3.1.14 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.13 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.12 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.11 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.10 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.9 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.8 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.7 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.6 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.5 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.4 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.3 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.2 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.1 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.0 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.0.rc4 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.0.rc3 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.0.rc2 app/controllers/spree/api/v1/inventory_units_controller.rb
spree_api-3.1.0.rc1 app/controllers/spree/api/v1/inventory_units_controller.rb