Sha256: 20a4e2110525eeaef7172afa5b50006d0b0171df361f57480c40ca3bc5f4bf81

Contents?: true

Size: 794 Bytes

Versions: 6

Compression:

Stored size: 794 Bytes

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # Common logic to paginate resources
  module Paginable
    extend ActiveSupport::Concern

    OPTIONS = [10, 20, 50, 100].freeze

    included do
      helper_method :per_page, :page_offset
      helper Decidim::PaginateHelper

      def paginate(resources)
        resources.page(params[:page]).per(per_page)
      end

      def per_page
        if OPTIONS.include?(params[:per_page])
          params[:per_page].to_i
        elsif params[:per_page]
          sorted = OPTIONS.sort
          params[:per_page].to_i.clamp(sorted.first, sorted.last)
        else
          OPTIONS.first
        end
      end

      def page_offset
        [params[:page].to_i - 1, 0].max * per_page
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.29.0.rc4 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.29.0.rc3 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.29.0.rc2 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.29.0.rc1 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.28.2 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.28.1 app/controllers/concerns/decidim/paginable.rb