Class: GuessPaging::Paginate

Inherits:
Object
  • Object
show all
Defined in:
lib/guess_paging/paginate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query: nil, per_page: 10, essential: 3) ⇒ Paginate

Returns a new instance of Paginate.



5
6
7
8
9
10
# File 'lib/guess_paging/paginate.rb', line 5

def initialize(query: nil, per_page: 10, essential: 3)
  @query = query
  @key = Digest::MD5.hexdigest(query.all.to_sql.to_s)
  @per_page = per_page
  @essential = essential
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



3
4
5
# File 'lib/guess_paging/paginate.rb', line 3

def count
  @count
end

#current_pageObject (readonly)

Returns the value of attribute current_page.



3
4
5
# File 'lib/guess_paging/paginate.rb', line 3

def current_page
  @current_page
end

#max_pageObject (readonly)

Returns the value of attribute max_page.



3
4
5
# File 'lib/guess_paging/paginate.rb', line 3

def max_page
  @max_page
end

#queryObject (readonly)

Returns the value of attribute query.



3
4
5
# File 'lib/guess_paging/paginate.rb', line 3

def query
  @query
end

#recordsObject (readonly)

Returns the value of attribute records.



3
4
5
# File 'lib/guess_paging/paginate.rb', line 3

def records
  @records
end

Instance Method Details

#guess(page_params: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/guess_paging/paginate.rb', line 12

def guess(page_params: nil)
  get_max_page
  if page(page_params).length < @per_page
    last_page = @query.count / @per_page + 1
    GuessPaging::RedisClient.set(@key, last_page) if last_page != get_max_page
    @records = @query.limit(@per_page).offset(@per_page * (last_page - 1))
    @current_page = last_page
    @max_page = last_page
    @count = @query.count
  else
    @current_page = get_page(page_params)
    @max_page = get_max_page
    @count = @max_page * @per_page
  end
end