Sha256: 6cc806b7aea32cc6c1097d819472214a183e8e211a45837e29d27c3edb0b1dc8

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'adequate_json/base'
module AdequateJson
  class Collection < AdequateJson::Base
    def initialize(collection, json = nil, variant: nil)
      @first_level = true if json.nil?
      super
      @variant ||= :no_wrapper
    end

    def to_builder
      with_jbuilder do |json|
        json.set!(collection_key) do
          json.array! @model do |item|
            serialize item, variant: @variant
          end
        end
        attach_pagination(json)
      end
    end

    private

    def collection_key
      return @model.model_name.plural if !@first_level || AdequateJson.configuration.use_model_name_for_collection_key

      AdequateJson.configuration.collection_key
    end

    def attach_pagination(json)
      return unless @first_level && @model.respond_to?(:current_page)

      json.pagination do
        json.current_page @model.current_page
        json.total_count @model.total_count
        json.next_page @model.next_page
        json.previous_page @model.prev_page
        json.total_pages @model.total_pages
      end
    end

    def with_jbuilder
      yield @json if @json
      Jbuilder.new do |json|
        @json = json
        yield json
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
adequate_json-0.1.0 lib/adequate_json/collection.rb