Sha256: 4fbca8a8c40becdfe107112f1fb0f8305428c98ff98567a4df94d8662c4847c6

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

module Spree
  module Api
    module V2
      module Storefront
        class CountriesController < ::Spree::Api::V2::BaseController
          include Spree::Api::V2::CollectionOptionsHelpers

          def index
            render_serialized_payload { serialize_collection(collection) }
          end

          def show
            render_serialized_payload { serialize_resource(resource) }
          end

          private

          def serialize_collection(collection)
            dependencies[:collection_serializer].new(collection).serializable_hash
          end

          def serialize_resource(resource)
            dependencies[:resource_serializer].new(
              resource,
              include: resource_includes,
              fields: sparse_fields,
              params: { include_states: true }
            ).serializable_hash
          end

          def collection
            dependencies[:collection_finder].new(scope, params).call
          end

          def resource
            return scope.default if params[:iso] == 'default'

            scope.find_by(iso: params[:iso]&.upcase) ||
              scope.find_by(iso3: params[:iso]&.upcase)
          end

          def dependencies
            {
              collection_serializer: Spree::V2::Storefront::CountrySerializer,
              resource_serializer: Spree::V2::Storefront::CountrySerializer,
              collection_finder: Spree::Countries::Find
            }
          end

          def scope
            Spree::Country.accessible_by(current_ability, :read)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_api-3.7.0.rc2 app/controllers/spree/api/v2/storefront/countries_controller.rb
spree_api-3.7.0.rc1 app/controllers/spree/api/v2/storefront/countries_controller.rb