Sha256: 432778101022051c826a5d68a5dbb17f3d7aeaa03acbff4120208a630c681f78

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

# Represents search results for a JSON API
#
# Subclasses should define the representer for the search results:
#   collection :items, inherit: true, decorator: SomeRepresenter
#
# See spec/dummy/app/representers/user_search_representer.rb for an example search representer

module OpenStax
  module Api
    module V1
      class AbstractSearchRepresenter < ::Roar::Decorator

        include ::Roar::JSON

        property :total_count,
                 type: Integer,
                 readable: true,
                 writeable: false,
                 exec_context: :decorator,
                 schema_info: {
                   required: true,
                   description: "The number of items matching the query; can be more than the number returned if paginating"
                 }

        collection :items,
                   readable: true,
                   writeable: false,
                   schema_info: {
                     required: true,
                     description: "The items matching the query or a subset thereof when paginating"
                   }

        def total_count
          return represented[:total_count] if represented[:total_count]
          case represented[:items]
          when ActiveRecord::Relation
            represented[:items].limit(nil).offset(nil).count
          when Array
            represented[:items].count
          else
            1
          end
        end

      end
    end 
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
openstax_api-5.3.0 app/representers/openstax/api/v1/abstract_search_representer.rb
openstax_api-5.2.2 app/representers/openstax/api/v1/abstract_search_representer.rb
openstax_api-5.2.1 app/representers/openstax/api/v1/abstract_search_representer.rb
openstax_api-5.2.0 app/representers/openstax/api/v1/abstract_search_representer.rb
openstax_api-5.1.1 app/representers/openstax/api/v1/abstract_search_representer.rb
openstax_api-5.1.0 app/representers/openstax/api/v1/abstract_search_representer.rb