Class: ActiverecordCursorPagination::CursorScope
- Inherits:
-
Object
- Object
- ActiverecordCursorPagination::CursorScope
- Defined in:
- lib/activerecord_cursor_pagination/cursor_scope.rb
Instance Attribute Summary collapse
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
Instance Method Summary collapse
-
#any? ⇒ Boolean
Get if there are records in the current page.
-
#current_cursor ⇒ String
Get the current cursor.
-
#each {|ActiveRecord::Base| ... } ⇒ Object
Iterate each record in the current page.
-
#each_with_index {|ActiveRecord::Base, Integer| ... } ⇒ Object
Iterate each record in the current page.
-
#empty? ⇒ Boolean
(also: #none?)
Get if there no records in the current page.
-
#first_page? ⇒ Boolean
Get if the cursor is the first page.
-
#initialize(klass, scope, cursor, per: 15) ⇒ CursorScope
constructor
A new instance of CursorScope.
-
#last_page? ⇒ Boolean
Get if the cursor is the last page.
-
#many? ⇒ Boolean
Get if there are many records in the current page.
-
#map {|ActiveRecord::Base| ... } ⇒ Object
Map each record in the current page.
-
#map_with_index {|ActiveRecord::Base, Integer| ... } ⇒ Object
Map each record in the current page.
-
#next_cursor ⇒ String
Get the next page cursor.
-
#next_cursor_record ⇒ ActiveRecord::Base
Get the next record.
-
#next_page? ⇒ Boolean
Get if there is another page.
-
#one? ⇒ Boolean
Get if there is only one in the current page.
-
#previous_cursor ⇒ String
Get the previous page cursor.
-
#previous_cursor_record ⇒ ActiveRecord::Base
Get the previous record.
-
#previous_page? ⇒ Boolean
Get if there is a previous page from the cursor.
-
#scope_any? ⇒ Boolean
Get if there are records from the query scope.
-
#scope_empty? ⇒ Boolean
(also: #scope_none?)
Get if there are not records from the query scope.
-
#scope_many? ⇒ Boolean
Get if there are many records from the query scope.
-
#scope_one? ⇒ Boolean
Get if there is only one record from the query scope.
-
#scope_size ⇒ Integer
(also: #scope_count, #total_count, #total)
Get the total count of records from the query scope.
-
#single_record? ⇒ Boolean
Get if the query is for single records.
-
#size ⇒ Integer
(also: #count, #length)
Get the number of records in the current page.
Constructor Details
#initialize(klass, scope, cursor, per: 15) ⇒ CursorScope
Returns a new instance of CursorScope.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 27 def initialize(klass, scope, cursor, per: 15) @scope = scope.except :offset, :limit @klass = klass @per_page = per @table = @scope.table_name @id_column = "#{@table}.id" initialize_order_columns initialize_cursor cursor initialize_order_column_values end |
Instance Attribute Details
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
3 4 5 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 3 def per_page @per_page end |
Instance Method Details
#any? ⇒ Boolean
Get if there are records in the current page.
118 119 120 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 118 def any? !empty? end |
#current_cursor ⇒ String
Get the current cursor
176 177 178 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 176 def current_cursor @cursor.to_param end |
#each {|ActiveRecord::Base| ... } ⇒ Object
Iterate each record in the current page.
226 227 228 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 226 def each(&block) current_page_scope.each &block end |
#each_with_index {|ActiveRecord::Base, Integer| ... } ⇒ Object
Iterate each record in the current page.
234 235 236 237 238 239 240 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 234 def each_with_index(&block) i = 0 current_page_scope.each do |r| block.call r, i i += 1 end end |
#empty? ⇒ Boolean Also known as: none?
Get if there no records in the current page.
108 109 110 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 108 def empty? size.zero? end |
#first_page? ⇒ Boolean
Get if the cursor is the first page
160 161 162 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 160 def first_page? scope_empty? || !previous_page? end |
#last_page? ⇒ Boolean
Get if the cursor is the last page
168 169 170 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 168 def last_page? scope_empty? || !next_page? end |
#many? ⇒ Boolean
Get if there are many records in the current page.
126 127 128 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 126 def many? size > 1 end |
#map {|ActiveRecord::Base| ... } ⇒ Object
Map each record in the current page.
246 247 248 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 246 def map(&block) current_page_scope.map &block end |
#map_with_index {|ActiveRecord::Base, Integer| ... } ⇒ Object
Map each record in the current page.
254 255 256 257 258 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 254 def map_with_index(&block) current_page_scope.map.with_index do |r, i| block.call r, i end end |
#next_cursor ⇒ String
Get the next page cursor
184 185 186 187 188 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 184 def next_cursor return EmptyCursor.to_param unless next_page? ids = next_page_scope.pluck(:id) Cursor.to_param @klass, @scope, @per_page, ids.first, ids.last end |
#next_cursor_record ⇒ ActiveRecord::Base
Get the next record
196 197 198 199 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 196 def next_cursor_record raise NotSingleRecordError unless single_record? next_page_scope.first if next_page? end |
#next_page? ⇒ Boolean
Get if there is another page
151 152 153 154 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 151 def next_page? return false if scope_empty? next_page_scope.any? end |
#one? ⇒ Boolean
Get if there is only one in the current page.
134 135 136 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 134 def one? size == 1 end |
#previous_cursor ⇒ String
Get the previous page cursor.
205 206 207 208 209 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 205 def previous_cursor return EmptyCursor.to_param unless previous_page? ids = previous_page_scope.pluck(:id) Cursor.to_param @klass, @scope, @per_page, ids.last, ids.first end |
#previous_cursor_record ⇒ ActiveRecord::Base
Get the previous record.
217 218 219 220 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 217 def previous_cursor_record raise NotSingleRecordError unless single_record? previous_page_scope.first if previous_page? end |
#previous_page? ⇒ Boolean
Get if there is a previous page from the cursor
142 143 144 145 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 142 def previous_page? return false if scope_empty? previous_page_scope.any? end |
#scope_any? ⇒ Boolean
Get if there are records from the query scope.
73 74 75 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 73 def scope_any? !scope_empty? end |
#scope_empty? ⇒ Boolean Also known as: scope_none?
Get if there are not records from the query scope.
63 64 65 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 63 def scope_empty? total_count.zero? end |
#scope_many? ⇒ Boolean
Get if there are many records from the query scope.
89 90 91 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 89 def scope_many? total_count > 1 end |
#scope_one? ⇒ Boolean
Get if there is only one record from the query scope.
81 82 83 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 81 def scope_one? total_count == 1 end |
#scope_size ⇒ Integer Also known as: scope_count, total_count, total
Get the total count of records from the query scope.
51 52 53 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 51 def scope_size @scope.except(:select).size end |
#single_record? ⇒ Boolean
Get if the query is for single records
43 44 45 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 43 def single_record? @per_page === 1 end |
#size ⇒ Integer Also known as: count, length
Get the number of records in the current page
97 98 99 |
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 97 def size current_page_scope.except(:select).size end |