Class: Rotulus::Page
- Inherits:
-
Object
- Object
- Rotulus::Page
- Defined in:
- lib/rotulus/page.rb
Instance Attribute Summary collapse
-
#ar_relation ⇒ Object
readonly
Returns the value of attribute ar_relation.
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Instance Method Summary collapse
-
#as_table ⇒ String
Returns a string showing the page’s records in table form with the ordered columns as the columns.
-
#at(token) ⇒ Page
Return a new page pointed to the given cursor(in encoded token format).
-
#at!(token) ⇒ self
Point the same page instance to the given cursor(in encoded token format).
-
#initialize(ar_relation, order: { id: :asc }, limit: nil) ⇒ Page
constructor
Creates a new Page instance representing a subset of the given ActiveRecord::Relation records sorted using the given ‘order’ definition param.
- #inspect ⇒ Object
-
#links ⇒ Hash
Generate a hash containing the previous and next page cursor tokens.
-
#next ⇒ Page
Next page instance.
-
#next? ⇒ Boolean
Check if a next page exists.
-
#next_token ⇒ String
Generate the cursor token to access the next page if one exists.
-
#prev ⇒ Page
Previous page instance.
-
#prev? ⇒ Boolean
Check if a preceding page exists.
-
#prev_token ⇒ Cursor
Generate the cursor token to access the previous page if one exists.
-
#query_state ⇒ String
Return Hashed value of this page’s state so we can check whether the base ar_relation has changed(e.g. SQL/filters from API params).
-
#records ⇒ Array<ActiveRecord::Base>
Get the records for this page.
-
#reload ⇒ self
Clear memoized records to lazily force to initiate the query again.
-
#root? ⇒ Boolean
Check if the page is the ‘root’ page; meaning, there are no preceding pages.
Constructor Details
#initialize(ar_relation, order: { id: :asc }, limit: nil) ⇒ Page
Creates a new Page instance representing a subset of the given ActiveRecord::Relation records sorted using the given ‘order’ definition param.
46 47 48 49 50 51 52 53 54 |
# File 'lib/rotulus/page.rb', line 46 def initialize(ar_relation, order: { id: :asc }, limit: nil) unless limit_valid?(limit) raise InvalidLimit.new("Allowed page limit is 1 up to #{config.page_max_limit}") end @ar_relation = ar_relation || model.none @order = Order.new(model, order) @limit = (limit.presence || config.page_default_limit).to_i end |
Instance Attribute Details
#ar_relation ⇒ Object (readonly)
Returns the value of attribute ar_relation.
3 4 5 |
# File 'lib/rotulus/page.rb', line 3 def ar_relation @ar_relation end |
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
3 4 5 |
# File 'lib/rotulus/page.rb', line 3 def cursor @cursor end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
3 4 5 |
# File 'lib/rotulus/page.rb', line 3 def limit @limit end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
3 4 5 |
# File 'lib/rotulus/page.rb', line 3 def order @order end |
Instance Method Details
#as_table ⇒ String
Returns a string showing the page’s records in table form with the ordered columns as the columns. This method is primarily used to test/debug the pagination behavior.
193 194 195 |
# File 'lib/rotulus/page.rb', line 193 def as_table Rotulus::PageTableizer.new(self).tableize end |
#at(token) ⇒ Page
Return a new page pointed to the given cursor(in encoded token format)
65 66 67 68 69 |
# File 'lib/rotulus/page.rb', line 65 def at(token) page_copy = dup page_copy.at!(token) page_copy end |
#at!(token) ⇒ self
Point the same page instance to the given cursor(in encoded token format)
79 80 81 82 83 |
# File 'lib/rotulus/page.rb', line 79 def at!(token) @cursor = token.present? ? cursor_clazz.for_page_and_token!(self, token) : nil reload end |
#inspect ⇒ Object
197 198 199 200 201 |
# File 'lib/rotulus/page.rb', line 197 def inspect cursor_info = cursor.nil? ? '' : " cursor='#{cursor}'" "#<#{self.class.name} ar_relation=#{ar_relation} order=#{order} limit=#{limit}#{cursor_info}>" end |
#links ⇒ Hash
Generate a hash containing the previous and next page cursor tokens
170 171 172 173 174 175 176 177 |
# File 'lib/rotulus/page.rb', line 170 def links return {} if records.empty? { previous: prev_token, next: next_token }.delete_if { |_, token| token.nil? } end |
#next ⇒ Page
Next page instance
152 153 154 155 156 |
# File 'lib/rotulus/page.rb', line 152 def next return unless next? at next_token end |
#next? ⇒ Boolean
Check if a next page exists
107 108 109 |
# File 'lib/rotulus/page.rb', line 107 def next? ((cursor.nil? || paged_forward?) && extra_row_returned?) || paged_back? end |
#next_token ⇒ String
Generate the cursor token to access the next page if one exists
128 129 130 131 132 133 134 135 |
# File 'lib/rotulus/page.rb', line 128 def next_token return unless next? record = cursor_reference_record(:next) return if record.nil? cursor_clazz.new(record, :next).to_token end |
#prev ⇒ Page
Previous page instance
161 162 163 164 165 |
# File 'lib/rotulus/page.rb', line 161 def prev return unless prev? at prev_token end |
#prev? ⇒ Boolean
Check if a preceding page exists
114 115 116 |
# File 'lib/rotulus/page.rb', line 114 def prev? (paged_back? && extra_row_returned?) || !cursor.nil? && paged_forward? end |
#prev_token ⇒ Cursor
Generate the cursor token to access the previous page if one exists
140 141 142 143 144 145 146 147 |
# File 'lib/rotulus/page.rb', line 140 def prev_token return unless prev? record = cursor_reference_record(:prev) return if record.nil? cursor_clazz.new(record, :prev).to_token end |
#query_state ⇒ String
Return Hashed value of this page’s state so we can check whether the base ar_relation has changed(e.g. SQL/filters from API params). see Cursor.for_page_and_token!
183 184 185 186 187 |
# File 'lib/rotulus/page.rb', line 183 def query_state data = ar_relation.to_sql Digest::MD5.hexdigest("#{data}#{Rotulus.configuration.secret}") end |
#records ⇒ Array<ActiveRecord::Base>
Get the records for this page. Note an extra record is fetched(limit + 1) to make it easier to check whether a next or previous page exists.
89 90 91 92 93 |
# File 'lib/rotulus/page.rb', line 89 def records return loaded_records[1..limit] if paged_back? && extra_row_returned? loaded_records[0...limit] end |
#reload ⇒ self
Clear memoized records to lazily force to initiate the query again.
98 99 100 101 102 |
# File 'lib/rotulus/page.rb', line 98 def reload @loaded_records = nil self end |
#root? ⇒ Boolean
Check if the page is the ‘root’ page; meaning, there are no preceding pages.
121 122 123 |
# File 'lib/rotulus/page.rb', line 121 def root? cursor.nil? || !prev? end |