Sha256: ebb096754ccbfec8a930459ea30e9de9f2fc7051d7fc96124823f5f518b6b13f

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

module Spree
  module Api
    module V2
      module Renderable
        extend ActiveSupport::Concern

        protected

        def render_collection(collection = [])
          collection = collection.where(filter_params).paginate(page_params)
          render json: collection, include: params[:include],
                 meta: { page: page_details(collection) }
        end

        def render_instance(object = {})
          render json: object, include: params[:include]
        end

        private

        def error_response(resource)
          Spree::ErrorSerializer.new(resource).as_json
        end

        def filter_params
          params.fetch(:filter, {}).permit(filter_attributes << :id).transform_values do |value|
            value.split(',')
          end
        end

        def filter_attributes
          serializer = "Spree::#{controller_name.camelize.singularize}Serializer"
          serializer.constantize._attributes
        end

        def page_details(collection)
          {
            total_items: collection.total_count,
            total_pages: collection.total_pages,
            number: (page_params[:number] || 1).to_i,
            size: (page_params[:size] || Kaminari.config.default_per_page).to_i
          }
        end

        def page_params
          params.fetch(:page, {}).permit(:number, :size)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
spree_api_v2-0.2.2 app/controllers/concerns/spree/api/v2/renderable.rb
solidus_api_v2-0.2.2 app/controllers/concerns/spree/api/v2/renderable.rb
solidus_api_v2-0.2.1 app/controllers/concerns/spree/api/v2/renderable.rb
spree_api_v2-0.2.1 app/controllers/concerns/spree/api/v2/renderable.rb
spree_api_v2-0.2.0 app/controllers/concerns/spree/api/v2/renderable.rb
solidus_api_v2-0.2.0 app/controllers/concerns/spree/api/v2/renderable.rb