Module: SmartPaginate::ActiveRecordExtension::RelationMethods

Defined in:
lib/smart_paginate/active_record_extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



23
24
25
# File 'lib/smart_paginate/active_record_extension.rb', line 23

def current_page
  @current_page
end

#per_pageObject

Returns the value of attribute per_page.



23
24
25
# File 'lib/smart_paginate/active_record_extension.rb', line 23

def per_page
  @per_page
end

Instance Method Details

#count(*args) ⇒ Object

Override #count: make it return the number of entries that we expect



26
27
28
29
30
31
32
33
# File 'lib/smart_paginate/active_record_extension.rb', line 26

def count(*args)
  if limit_value && per_page && limit_value > per_page
    rel = except(:limit).limit(per_page)
    rel.count(*args)
  else
    super(*args)
  end
end

#empty?Boolean

Override #empty: check fetched records instead of counting

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/smart_paginate/active_record_extension.rb', line 36

def empty?
  load # make sure we have determined the number of fetched records

  @number_of_records.zero?
end

#loadObject

Override #load: fetch/slice records and determine number of records



43
44
45
46
47
48
# File 'lib/smart_paginate/active_record_extension.rb', line 43

def load
  super
  slice_records!

  self
end

#next_pageObject



60
61
62
# File 'lib/smart_paginate/active_record_extension.rb', line 60

def next_page
  current_page + 1 if next_page?
end

#next_page?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/smart_paginate/active_record_extension.rb', line 50

def next_page?
  load # make sure we have determined the number of fetched records

  @number_of_records > per_page # there's a next page when we've fetched more records than per_page
end

#previous_pageObject



64
65
66
# File 'lib/smart_paginate/active_record_extension.rb', line 64

def previous_page
  current_page - 1 if previous_page?
end

#previous_page?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/smart_paginate/active_record_extension.rb', line 56

def previous_page?
  current_page > 1
end

#total_entriesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/smart_paginate/active_record_extension.rb', line 72

def total_entries
  @total_entries ||= begin
    load # make sure we have determined the number of fetched records

    # if we know that there are no more records, then we can calculate total_entries
    if @number_of_records <= per_page
      offset_value + @number_of_records
    else
      rel = except(:limit, :offset)
      rel.count(:all)
    end
  end
end

#total_pagesObject



68
69
70
# File 'lib/smart_paginate/active_record_extension.rb', line 68

def total_pages
  (total_entries / per_page.to_f).ceil
end