ruby ruhoh的Paginator助手,当有很多帖子时折叠页面链接。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby ruhoh的Paginator助手,当有很多帖子时折叠页面链接。相关的知识,希望对你有一定的参考价值。

module Ruhoh::Views::Helpers
  module Paginator
    # current_page is set via a compiler or previewer
    # in which it can discern what current_page to serve
    def paginator
      per_page = config["paginator"]["per_page"] rescue 5
      current_page = master.page_data['current_page'].to_i
      current_page = current_page.zero? ? 1 : current_page
      offset = (current_page-1)*per_page

      page_batch = all[offset, per_page]
      raise "Page does not exist" unless page_batch
      page_batch
    end

    # [1][2] ... [n-1][n][n+1] ... [last-1][last]
    def paginator_navigation
      paginator_config = {"borders"=>5, "per_page"=>10}.merge(config["paginator"] || {})
      page_count = all.length
      total_pages = (page_count.to_f/paginator_config["per_page"]).ceil
      current_page = master.page_data['current_page'].to_i
      current_page = current_page.zero? ? 1 : current_page

      left_dots = ((current_page+1)/2).ceil
      right_dots = ((total_pages+current_page)/2).ceil
      borders = paginator_config["borders"]

      pages = total_pages.times.select { |i|
          i+1 <= borders || i+1 > total_pages-borders ||
         (i+1 >= current_page-(borders/2).ceil && i+1 <= current_page+(borders/2).ceil) ||
          i+1 == left_dots || i+1 == right_dots
      }.map { |i|
        url = i.zero? && paginator_config["root_page"] ?
                paginator_config["root_page"] : "#{paginator_config["url"]}/#{i+1}"
        name = (i+1 > borders) && (i+1 < total_pages-borders) &&
              ((i+1 < current_page-(borders/2).ceil) || (i+1 > current_page+(borders/2).ceil)) &&
               (i+1 == left_dots || i+1 == right_dots) ? '…' : "#{i+1}"

        {
          "url" => ruhoh.to_url(url),
          "name" => name,
          "is_active_page" => (i+1 == current_page),
          "title" => "#{i+1}"
        }
      }
      pages
    end
  end
end

以上是关于ruby ruhoh的Paginator助手,当有很多帖子时折叠页面链接。的主要内容,如果未能解决你的问题,请参考以下文章

ruby 助手围绕关联创建视图文本

ruby 助手

ruby 样本规范助手

ruby 适用于Comfy的CSS和Javascript助手

ruby kaminari + JSON API分页助手

如何在 ruby​​ on rails 中访问 rails 助手和嵌入资产 javascript 文件中的 ruby​​?