Class: Rotulus::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/rotulus/order.rb

Instance Method Summary collapse

Constructor Details

#initialize(ar_model, raw_hash = {}) ⇒ Order

Creates an Order object that builds the column objects in the “ORDER BY” expression

Parameters:

  • ar_model (Class)

    the ActiveRecord model class name of Page#ar_relation

  • raw_hash (Hash<Symbol, Hash>, Hash<Symbol, Symbol>, nil) (defaults to: {})

    the order definition of columns

Raises:



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_namesArray<String>

Returns an array of the ordered columns’ names

Returns:

  • (Array<String>)

    ordered column names



30
31
32
# File 'lib/rotulus/order.rb', line 30

def column_names
  @column_names ||= columns.map(&:name)
end

#columnsArray<Rotulus::Column>

Returns an array of the ordered columns

Returns:



23
24
25
# File 'lib/rotulus/order.rb', line 23

def columns
  @columns ||= definition.values
end

#prefixed_column_namesArray<String>

Returns an array of column names prefixed with table name

Returns:

  • (Array<String>)

    column names prefixed with the 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_sqlString

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?)

Returns:

  • (String)

    the ORDER BY clause



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_sqlString

Returns the SELECT expressions to include the ordered columns in the selected columns of a query.

Returns:

  • (String)

    the SELECT expressions



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.

Parameters:

  • record (ActiveRecord::Base)

    a record/row returned from Page#records

Returns:

  • (Hash)

    the hash containing the column values with the column name as key



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

#sqlString

Returns the ORDER BY sort expression(s) to sort the records

Returns:

  • (String)

    the ORDER BY clause



75
76
77
# File 'lib/rotulus/order.rb', line 75

def sql
  Arel.sql(columns_for_order.map(&:order_sql).join(', '))
end

#stateString

Generate a ‘state’ so we can detect whether the order definition has changed.

Returns:

  • (String)

    the hashed state



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_hHash

Returns a hash containing the hash representation of the ordered columns.

Returns:

  • (Hash)

    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