Module: SmartPaginate::ActiveRecordExtension::RelationMethods
- Defined in:
- lib/smart_paginate/active_record_extension.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
Returns the value of attribute current_page.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
Instance Method Summary collapse
-
#count(*args) ⇒ Object
Override #count: make it return the number of entries that we expect.
-
#empty? ⇒ Boolean
Override #empty: check fetched records instead of counting.
-
#load ⇒ Object
Override #load: fetch/slice records and determine number of records.
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #previous_page ⇒ Object
- #previous_page? ⇒ Boolean
- #total_entries ⇒ Object
- #total_pages ⇒ Object
Instance Attribute Details
#current_page ⇒ Object
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_page ⇒ Object
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
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 |
#load ⇒ Object
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_page ⇒ Object
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
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_page ⇒ Object
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
56 57 58 |
# File 'lib/smart_paginate/active_record_extension.rb', line 56 def previous_page? current_page > 1 end |
#total_entries ⇒ Object
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_pages ⇒ Object
68 69 70 |
# File 'lib/smart_paginate/active_record_extension.rb', line 68 def total_pages (total_entries / per_page.to_f).ceil end |