Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get will_paginate to define a custom offset on the first page

i'm building a news section on my rails website and it uses will_paginate for pagination. now i was wondering how i can specify a custom offset for the first page with will_paginate. something like this:

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

i need the latest news-entry to be special and not be included in the @news objects.

how i can achieve this?

thanks for your time!

like image 770
Oliver Avatar asked Sep 22 '11 10:09

Oliver


1 Answers

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)
like image 174
MrRuru Avatar answered Oct 05 '22 23:10

MrRuru