Class: ActiverecordCursorPagination::OrderBase
- Inherits:
-
Object
- Object
- ActiverecordCursorPagination::OrderBase
- Defined in:
- lib/activerecord_cursor_pagination/order_base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base_id ⇒ Object
Returns the value of attribute base_id.
-
#direction ⇒ Symbol
readonly
abstract
Get the direction of the order.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Class Method Summary collapse
-
.order_factory(direction) ⇒ Object
Order class factory.
-
.parse(string_or_sql_order_node, index) ⇒ OrderBase
Parse the order string or node.
-
.parse_order_node(node, index) ⇒ OrderBase
Parse the order Arel node.
-
.parse_string(string_or_sql_literal, index) ⇒ OrderBase
Parse the order string.
Instance Method Summary collapse
-
#base_id? ⇒ Boolean
Get if the order column is the base id of the table.
-
#equals_sql ⇒ String
Get the SQL for the equals comparison.
-
#full_name ⇒ String
Get the full SQL name.
-
#initialize(table, name, index) ⇒ OrderBase
constructor
Initialize the OrderBase.
-
#order_sql ⇒ Arel::Nodes::SqlLiteral
Get the SQL literal of the column order.
-
#quote_full_name ⇒ String
Get the full quoted name.
-
#quote_name ⇒ String
Get the quoted column name.
-
#quote_table ⇒ String?
Get the quoted table name.
-
#reverse ⇒ OrderBase
abstract
Get the reverse column order.
-
#statement_key ⇒ Symbol
Get the statement key for the named SQL query.
-
#table? ⇒ Boolean
Get if the table name is defined.
-
#table_exists? ⇒ Boolean
Get if the table exists.
-
#than_op ⇒ String
abstract
Get the SQL operation for the greater/less than comparison.
-
#than_or_equal_op ⇒ String
abstract
Get the SQL operation for the greater/less than or equal comparison.
-
#than_or_equal_sql ⇒ String
Get the SQL for the greater/less than or equal to comparison depending on direction.
-
#than_sql ⇒ String
Get the SQL for the greater/less than comparison depending on direction.
-
#valid_name? ⇒ Boolean
Get if the column name is a valid SQL column name.
-
#valid_table_name? ⇒ Boolean
Get if the table name is a valid SQL database name.
Constructor Details
#initialize(table, name, index) ⇒ OrderBase
Initialize the OrderBase.
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_id ⇒ Object
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 |
#direction ⇒ Symbol (readonly)
Get the direction of the order.
26 27 28 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 26 def direction @direction end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
3 4 5 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 3 def index @index end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 3 def name @name end |
#table ⇒ Object (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
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
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
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
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.
34 35 36 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 34 def base_id? !!@base_id end |
#equals_sql ⇒ String
Get the SQL for the equals comparison
124 125 126 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 124 def equals_sql "#{quote_full_name} = :#{statement_key}" end |
#full_name ⇒ String
Get the full SQL name.
82 83 84 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 82 def full_name table? ? "#{table}.#{name}" : name end |
#order_sql ⇒ Arel::Nodes::SqlLiteral
Get the SQL literal of the column order
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_name ⇒ String
Get the full quoted name.
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_name ⇒ String
Get the quoted column name
106 107 108 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 106 def quote_name ActiverecordCursorPagination.quote_column name end |
#quote_table ⇒ String?
Get the quoted table name.
98 99 100 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 98 def quote_table ActiverecordCursorPagination.quote_table table end |
#reverse ⇒ OrderBase
Get the reverse column order
116 117 118 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 116 def reverse raise NotImplementedError end |
#statement_key ⇒ Symbol
Get the statement key for the named SQL query.
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.
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.
50 51 52 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 50 def table_exists? ActiverecordCursorPagination.table_exists? table end |
#than_op ⇒ String
Get the SQL operation for the greater/less than comparison
142 143 144 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 142 def than_op raise NotImplementedError end |
#than_or_equal_op ⇒ String
Get the SQL operation for the greater/less than or equal comparison
160 161 162 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 160 def than_or_equal_op raise NotImplementedError end |
#than_or_equal_sql ⇒ String
Get the SQL for the greater/less than or equal to comparison depending on direction
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_sql ⇒ String
Get the SQL for the greater/less than comparison depending on direction
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.
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.
58 59 60 |
# File 'lib/activerecord_cursor_pagination/order_base.rb', line 58 def valid_table_name? ActiverecordCursorPagination.valid_name? table end |