Sha256: d1b94d75e20147c8aa42fd1d96eed71afc838e6a57ebcbc9fc502c08b8bd3f13

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module Esse
  module Kaminari
    module Pagination
      module SearchQuery
        def self.included(base)
          base.__send__ :include, ::Kaminari::PageScopeMethods
          base.__send__ :extend, Forwardable
          base.__send__ :def_delegators, :_kaminari_config, :default_per_page, :max_per_page, :max_pages

          base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
            # Define the `page` Kaminari method
            #
            # @param [Integer] page
            # @return [self]
            def #{::Kaminari.config.page_method_name}(num=nil)
              reset!

              @page  = [num.to_i, 1].max
              @per_page ||= limit_value

              definition.update(
                size: @per_page,
                from: @per_page * (@page - 1),
              )

              self
            end
          RUBY
        end

        def limit_value
          (raw_limit_value || default_per_page).to_i
        end

        def offset_value
          raw_offset_value.to_i
        end

        def total_count
          response.total
        end

        def paginated_results
          ::Kaminari.paginate_array(response.hits, limit: limit_value, offset: offset_value, total_count: total_count)
        end

        def limit(value)
          return self if value.to_i <= 0
          reset!
          @per_page = value.to_i

          definition.update(size: @per_page)
          definition.update(from: @per_page * (@page - 1)) if @page
          self
        end

        def offset(value)
          return self if value.to_i < 0
          reset!
          @page = nil
          definition.update(from: value.to_i)
          self
        end

        private

        def _kaminari_config
          ::Kaminari.config
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
esse-kaminari-0.1.1 lib/esse/kaminari/pagination.rb