Sha256: be2119b9a74cad62d97cdf0ec7763929929fb688d9931c1f12c638524d5ad0b0

Contents?: true

Size: 1.95 KB

Versions: 37

Compression:

Stored size: 1.95 KB

Contents

module Spree
  module Api
    class AddressBooksController < Spree::Api::BaseController
      # Note: the AddressBook is the resource to think about here, not individual addresses
      before_action :load_user_addresses

      def show
        authorize! :show, address_book_user

        render :show, status: :ok
      end

      # Update a user's address book by adding an address to it or by updating
      # the associated UserAddress (e.g. making it the default).
      #
      # @param user_id [String] the user id of the address book we're updating.
      # @param address_book [Hash] any key-values permitted by
      #   permitted_address_book_attributes
      # @return [Array] *All* of the user's addresses, since the resource here
      #   is the address book and since we may have mutated other UserAddresses
      #   (e.g. changed the 'default' flag).  The user's default address will be
      #   flagged with default=true and the target address from this update will
      #   be flagged with update_target=true.
      def update
        authorize! :save_in_address_book, address_book_user

        address_params = address_book_params
        default_flag = address_params.delete(:default)
        @address = address_book_user.save_in_address_book(address_params, default_flag)
        if @address.valid?
          render :show, status: :ok
        else
          invalid_resource!(@address)
        end
      end

      def destroy
        authorize! :remove_from_address_book, address_book_user

        address_book_user.remove_from_address_book(params[:address_id])
        render :show, status: :ok
      end

      private

      def address_book_user
        @address_book_user ||= Spree.user_class.find(params[:user_id])
      end

      def load_user_addresses
        @user_addresses ||= address_book_user.user_addresses
      end

      def address_book_params
        params.require(:address_book).permit(permitted_address_book_attributes)
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
solidus_api-2.5.2 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.5.1 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.5.0 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.5.0.rc1 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.5.0.beta2 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.5.0.beta1 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.4.2 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.4.1 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.3.1 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.2.2 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.1.1 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.0.3 app/controllers/spree/api/address_books_controller.rb
solidus_api-1.4.2 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.4.0 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.4.0.rc1 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.4.0.beta1 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.3.0 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.3.0.rc3 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.3.0.rc2 app/controllers/spree/api/address_books_controller.rb
solidus_api-2.3.0.rc1 app/controllers/spree/api/address_books_controller.rb