Sha256: 5f6988d2beeb60216fa24f8f6302dae41302a9caf0f6791b91d24093cb8e0f96

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

if defined?(Kaminari)
  module KaminariExtension
    module ParseBaseExt
      extend ActiveSupport::Concern
      include Kaminari::ConfigurationMethods

      module ClassMethods
        def page(num)
          Query.new(self).page(num)
        end
      end
    end

    module QueryExt
      extend ActiveSupport::Concern
      include Kaminari::PageScopeMethods

      included do
        alias :offset :skip
      end

      def limit_value
        criteria[:limit]
      end

      def offset_value
        criteria[:skip]
      end

      def total_count
        count
      end

      def max_per_page
        @klass.max_per_page
      end

      def page(num)
        limit(@klass.default_per_page).skip(@klass.default_per_page * ([num.to_i, 1].max - 1))
        self
      end

      def per(num)
        if (n = num.to_i) <= 0
          self
        elsif max_per_page && max_per_page < n
          new_offset_value = offset_value / limit_value * max_per_page
          limit(max_per_page).offset(new_offset_value)
        else
          new_offset_value = offset_value / limit_value * n
          limit(n).offset(new_offset_value)
        end
        self
      end
    end
  end

  ParseResource::Base.send :include, KaminariExtension::ParseBaseExt
  Query.send               :include, KaminariExtension::QueryExt
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parse_resource-1.8.0 lib/kaminari_extension.rb