Class: ActiverecordCursorPagination::OrderBase

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_cursor_pagination/order_base.rb

Direct Known Subclasses

AscendingOrder, DescendingOrder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, name, index) ⇒ OrderBase

Initialize the OrderBase.

Parameters:

  • table (String)

    The table name.

  • name (String)

    The name of the column.

  • index (Integer)

    The index of the order column.



13
14
15
16
17
18
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 13

def initialize(table, name, index)
  @table = ActiverecordCursorPagination.strip_quotes table
  @name = ActiverecordCursorPagination.strip_quotes name
  @base_id = false
  @index = index
end

Instance Attribute Details

#base_idObject

Returns the value of attribute base_id.



5
6
7
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 5

def base_id
  @base_id
end

#directionSymbol (readonly)

This method is abstract.

Get the direction of the order.

Returns:

  • (Symbol)

    Returns the order symbol.

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 26

def direction
  @direction
end

#indexObject (readonly)

Returns the value of attribute index.



3
4
5
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 3

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 3

def name
  @name
end

#tableObject (readonly)

Returns the value of attribute table.



3
4
5
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 3

def table
  @table
end

Class Method Details

.order_factory(direction) ⇒ Object

Order class factory

Parameters:

  • direction (String, Symbol)

    The direction of the order column.



258
259
260
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 258

def order_factory(direction)
  direction&.to_s&.downcase === 'desc' ? DescendingOrder : AscendingOrder
end

.parse(string_or_sql_order_node, index) ⇒ OrderBase

Parse the order string or node

Parameters:

  • string_or_sql_order_node (String, Arel::Nodes::Node, Arel::Nodes::SqlLiteral)

    The table, column, and/or order representation.

  • index (Integer)

    The index of the node.

Returns:



185
186
187
188
189
190
191
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 185

def parse(string_or_sql_order_node, index)
  if string_or_sql_order_node.is_a?(Arel::Nodes::SqlLiteral) || string_or_sql_order_node.is_a?(String)
    parse_string string_or_sql_order_node, index
  else
    parse_order_node string_or_sql_order_node, index
  end
end

.parse_order_node(node, index) ⇒ OrderBase

Parse the order Arel node

Parameters:

  • node (Arel::Nodes::Node)

    The order node.

  • index (Integer)

    The index of the order node.

Returns:



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 237

def parse_order_node(node, index)
  order_klass = order_factory node.direction

  table, column = if node.expr.is_a? Arel::Nodes::SqlLiteral
                    parse_table_column node.expr.to_s.strip
                  else
                    [node.expr.relation.name, node.expr.name]
                  end

  if column.nil? || column.empty?
    column = table
    table = @table
  end

  order_klass.new table, column, index
end

.parse_string(string_or_sql_literal, index) ⇒ OrderBase

Parse the order string

Limitations:

1. Complex queries must use +'+ quotes for strings

Parameters:

  • string_or_sql_literal (String, Arel::Nodes::SqlLiteral)

    The string representation of the table, column, and/or order direction.

  • index (Integer)

    The index of the node.

Returns:



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 208

def parse_string(string_or_sql_literal, index)
  string_or_sql_literal.strip!

  table_column, dir = if (match = string_or_sql_literal.match(/\A(?<rest>.*)\s+(?<order>ASC|DESC)\z/i))
                        [match[:rest]&.strip, match[:order]&.downcase]
                      else
                        [string_or_sql_literal, nil]
                      end

  order_klass = order_factory dir


  table, column = parse_table_column table_column.to_s.strip

  if column.nil? || column.empty?
    column = table
    table = nil
  end

  order_klass.new table, column, index
end

Instance Method Details

#base_id?Boolean

Get if the order column is the base id of the table.

Returns:

  • (Boolean)

    Is the base id.



34
35
36
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 34

def base_id?
  !!@base_id
end

#equals_sqlString

Get the SQL for the equals comparison

Returns:

  • (String)


124
125
126
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 124

def equals_sql
  "#{quote_full_name} = :#{statement_key}"
end

#full_nameString

Get the full SQL name.

Returns:

  • (String)

    .



82
83
84
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 82

def full_name
  table? ? "#{table}.#{name}" : name
end

#order_sqlArel::Nodes::SqlLiteral

Get the SQL literal of the column order

Returns:

  • (Arel::Nodes::SqlLiteral)

    Sql literal



168
169
170
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 168

def order_sql
  Arel.sql "#{quote_full_name} #{direction.to_s.upcase}"
end

#quote_full_nameString

Get the full quoted name.

Returns:

  • (String)

    .



90
91
92
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 90

def quote_full_name
  ActiverecordCursorPagination.quote_table_column table, name
end

#quote_nameString

Get the quoted column name

Returns:

  • (String)


106
107
108
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 106

def quote_name
  ActiverecordCursorPagination.quote_column name
end

#quote_tableString?

Get the quoted table name.

Returns:

  • (String, nil)

    .



98
99
100
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 98

def quote_table
  ActiverecordCursorPagination.quote_table table
end

#reverseOrderBase

This method is abstract.

Get the reverse column order

Returns:

Raises:

  • (NotImplementedError)


116
117
118
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 116

def reverse
  raise NotImplementedError
end

#statement_keySymbol

Get the statement key for the named SQL query.

Returns:

  • (Symbol)

    The statement key.



74
75
76
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 74

def statement_key
  :"order_field#{index}"
end

#table?Boolean

Get if the table name is defined.

Returns:

  • (Boolean)

    True if the table is defined.



42
43
44
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 42

def table?
  !table.nil? && !table.empty?
end

#table_exists?Boolean

Get if the table exists.

Returns:

  • (Boolean)

    True if the table exists.



50
51
52
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 50

def table_exists?
  ActiverecordCursorPagination.table_exists? table
end

#than_opString

This method is abstract.

Get the SQL operation for the greater/less than comparison

Returns:

  • (String)

Raises:

  • (NotImplementedError)


142
143
144
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 142

def than_op
  raise NotImplementedError
end

#than_or_equal_opString

This method is abstract.

Get the SQL operation for the greater/less than or equal comparison

Returns:

  • (String)

Raises:

  • (NotImplementedError)


160
161
162
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 160

def than_or_equal_op
  raise NotImplementedError
end

#than_or_equal_sqlString

Get the SQL for the greater/less than or equal to comparison depending on direction

Returns:

  • (String)


150
151
152
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 150

def than_or_equal_sql
  "#{quote_full_name} #{than_or_equal_op} :#{statement_key}"
end

#than_sqlString

Get the SQL for the greater/less than comparison depending on direction

Returns:

  • (String)


132
133
134
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 132

def than_sql
  "#{quote_full_name} #{than_op} :#{statement_key}"
end

#valid_name?Boolean

Get if the column name is a valid SQL column name.

Returns:

  • (Boolean)

    True if a valid name.



66
67
68
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 66

def valid_name?
  ActiverecordCursorPagination.valid_name? name
end

#valid_table_name?Boolean

Get if the table name is a valid SQL database name.

Returns:

  • (Boolean)

    True if a valid name.



58
59
60
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 58

def valid_table_name?
  ActiverecordCursorPagination.valid_name? table
end