Sha256: 570a2220a9083e61c19739b196065922056c275bd00bad0537b2635cc7230087

Contents?: true

Size: 785 Bytes

Versions: 31

Compression:

Stored size: 785 Bytes

Contents

# frozen_string_literal: true

require "active_support/concern"

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

    OPTIONS = [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]
        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

31 entries across 31 versions & 1 rubygems

Version Path
decidim-core-0.26.10 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.9 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.27.5 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.8 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.27.4 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.27.3 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.7 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.5 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.27.2 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.27.1 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.4 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.27.0 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.3 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.27.0.rc2 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.27.0.rc1 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.2 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.1 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.0 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.0.rc2 app/controllers/concerns/decidim/paginable.rb
decidim-core-0.26.0.rc1 app/controllers/concerns/decidim/paginable.rb