get will_paginate to define a custom offset on the first page

前提是你 提交于 2019-12-01 17:15:51
MrRuru

will_paginate redefines every offset and limit query conditions, to get the rows of a specific page. I can see two options for you :

The ugly one : take advantage of the fact that will_paginate works on collections, and use this syntax (it will load all your table though)

@news = Post.offset(1).all.paginate(:page => params[:page])

The longer one : fork the will_paginate gem so that it can handle custom offsets. I haven't tried it, but something like this should work (the changes to the gem are highlighted)

# will_paginate / lib / will_paginate / active_record.rb

module Pagination
  def paginate(options)
    options  = options.dup
    pagenum  = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
    per_page = options.delete(:per_page) || self.per_page
    total    = options.delete(:total_entries)

    #######################################
    custom_offset = options.delete(:offset)
    ####################################### 

    count_options = options.delete(:count)
    options.delete(:page)

    #######################################################
    # rel = limit(per_page.to_i).page(pagenum)
    rel = limit(per_page.to_i).page(pagenum, custom_offset)
    #######################################################

    rel = rel.apply_finder_options(options) if options.any?
    rel.wp_count_options = count_options    if count_options
    rel.total_entries = total.to_i          unless total.blank?
    rel
  end

  ################################
  # def page(num)
  def page(num, custom_offset = 0)
  ################################
    rel = scoped.extending(RelationMethods)
    pagenum = ::WillPaginate::PageNumber(num.nil? ? 1 : num)
    per_page = rel.limit_value || self.per_page
    ##################################################################
    # rel = rel.offset(pagenum.to_offset(per_page).to_i)
    rel = rel.offset(pagenum.to_offset(per_page).to_i + custom_offset)
    ##################################################################
    rel = rel.limit(per_page) unless rel.limit_value
    rel.current_page = pagenum
    rel
  end
end

This should allow you to use this syntax:

@news = Post.paginate(:page => params[:page], :offset => 1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!