Class: Rotulus::Order
- Inherits:
-
Object
- Object
- Rotulus::Order
- Defined in:
- lib/rotulus/order.rb
Instance Method Summary collapse
-
#column_names ⇒ Array<String>
Returns an array of the ordered columns’ names.
-
#columns ⇒ Array<Rotulus::Column>
Returns an array of the ordered columns.
-
#initialize(ar_model, raw_hash = {}) ⇒ Order
constructor
Creates an Order object that builds the column objects in the “ORDER BY” expression.
-
#prefixed_column_names ⇒ Array<String>
Returns an array of column names prefixed with table name.
-
#reversed_sql ⇒ String
Returns the reversed ‘ORDER BY` expression(s) for the current page when the page was accessed via a ’previous’ cursor(i.e. navigating back/ page#paged_back?).
-
#select_sql ⇒ String
Returns the SELECT expressions to include the ordered columns in the selected columns of a query.
-
#selected_values(record) ⇒ Hash
Returns a hash containing the ordered column values of a given ActiveRecord::Base record instance.
-
#sql ⇒ String
Returns the ORDER BY sort expression(s) to sort the records.
-
#state ⇒ String
Generate a ‘state’ so we can detect whether the order definition has changed.
-
#to_h ⇒ Hash
Returns a hash containing the hash representation of the ordered columns.
Constructor Details
#initialize(ar_model, raw_hash = {}) ⇒ Order
Creates an Order object that builds the column objects in the “ORDER BY” expression
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rotulus/order.rb', line 8 def initialize(ar_model, raw_hash = {}) @ar_model = ar_model @raw_hash = raw_hash&.with_indifferent_access || {} @definition = {} build_column_definitions! return if has_tiebreaker? raise Rotulus::MissingTiebreaker.new('A non-nullable and distinct column is required.') end |
Instance Method Details
#column_names ⇒ Array<String>
Returns an array of the ordered columns’ names
30 31 32 |
# File 'lib/rotulus/order.rb', line 30 def column_names @column_names ||= columns.map(&:name) end |
#columns ⇒ Array<Rotulus::Column>
Returns an array of the ordered columns
23 24 25 |
# File 'lib/rotulus/order.rb', line 23 def columns @columns ||= definition.values end |
#prefixed_column_names ⇒ Array<String>
Returns an array of column names prefixed with table name
37 38 39 |
# File 'lib/rotulus/order.rb', line 37 def prefixed_column_names @prefixed_column_names ||= definition.keys.map(&:to_s) end |
#reversed_sql ⇒ String
Returns the reversed ‘ORDER BY` expression(s) for the current page when the page was accessed via a ’previous’ cursor(i.e. navigating back/ page#paged_back?)
68 69 70 |
# File 'lib/rotulus/order.rb', line 68 def reversed_sql Arel.sql(columns_for_order.map(&:reversed_order_sql).join(', ')) end |
#select_sql ⇒ String
Returns the SELECT expressions to include the ordered columns in the selected columns of a query.
45 46 47 |
# File 'lib/rotulus/order.rb', line 45 def select_sql columns.map(&:select_sql).join(', ') end |
#selected_values(record) ⇒ Hash
Returns a hash containing the ordered column values of a given ActiveRecord::Base record instance. These values will be used to generate the query to fetch the preceding/succeeding records of a given :record.
55 56 57 58 59 60 61 62 |
# File 'lib/rotulus/order.rb', line 55 def selected_values(record) return {} if record.blank? record.slice(*select_aliases) .transform_keys do |a| Column.select_alias_to_name(a) end end |
#sql ⇒ String
Returns the ORDER BY sort expression(s) to sort the records
75 76 77 |
# File 'lib/rotulus/order.rb', line 75 def sql Arel.sql(columns_for_order.map(&:order_sql).join(', ')) end |
#state ⇒ String
Generate a ‘state’ so we can detect whether the order definition has changed.
82 83 84 85 86 |
# File 'lib/rotulus/order.rb', line 82 def state data = MultiJson.dump(to_h) Digest::MD5.hexdigest("#{data}#{Rotulus.configuration.secret}") end |
#to_h ⇒ Hash
Returns a hash containing the hash representation of the ordered columns.
91 92 93 94 95 |
# File 'lib/rotulus/order.rb', line 91 def to_h definition.each_with_object({}) do |(name, column), h| h.merge!(column.to_h) end end |