Class: TypicalSort::AggregateSorter

Inherits:
Object
  • Object
show all
Defined in:
lib/typical_sort/aggregate_sorter.rb

Constant Summary collapse

VALID_AGGREGATES =
%i[min max sum avg count directional].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records:, relation_name:, reflection:, column_name:, direction:, definition:, configuration:) ⇒ AggregateSorter

Returns a new instance of AggregateSorter.



9
10
11
12
13
14
15
16
17
# File 'lib/typical_sort/aggregate_sorter.rb', line 9

def initialize(records:, relation_name:, reflection:, column_name:, direction:, definition:, configuration:)
  @records = records
  @relation_name = relation_name.to_sym
  @reflection = reflection
  @column_name = column_name.to_s
  @direction = direction.to_sym
  @definition = definition
  @configuration = configuration
end

Instance Attribute Details

#column_nameObject (readonly)

Returns the value of attribute column_name.



7
8
9
# File 'lib/typical_sort/aggregate_sorter.rb', line 7

def column_name
  @column_name
end

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/typical_sort/aggregate_sorter.rb', line 7

def configuration
  @configuration
end

#definitionObject (readonly)

Returns the value of attribute definition.



7
8
9
# File 'lib/typical_sort/aggregate_sorter.rb', line 7

def definition
  @definition
end

#directionObject (readonly)

Returns the value of attribute direction.



7
8
9
# File 'lib/typical_sort/aggregate_sorter.rb', line 7

def direction
  @direction
end

#recordsObject (readonly)

Returns the value of attribute records.



7
8
9
# File 'lib/typical_sort/aggregate_sorter.rb', line 7

def records
  @records
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



7
8
9
# File 'lib/typical_sort/aggregate_sorter.rb', line 7

def reflection
  @reflection
end

#relation_nameObject (readonly)

Returns the value of attribute relation_name.



7
8
9
# File 'lib/typical_sort/aggregate_sorter.rb', line 7

def relation_name
  @relation_name
end

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/typical_sort/aggregate_sorter.rb', line 19

def call
  aggregate = aggregate_sql_function
  connection = records.klass.connection
  base_table_name = records.klass.table_name
  base_pk = records.klass.primary_key
  quoted_base_table = connection.quote_table_name(base_table_name)
  quoted_base_pk = connection.quote_column_name(base_pk)
  sort_value_sql = value_sql(connection)

  base_ids = records
    .except(:select, :order)
    .select("#{quoted_base_table}.#{quoted_base_pk}")

  filtered_ids = records
    .except(:select, :order, :limit, :offset, :group)
    .select("#{quoted_base_table}.#{quoted_base_pk}")

  sort_subquery = records.klass
    .where(base_pk => filtered_ids)
    .left_outer_joins(relation_name)
    .select(
      "#{quoted_base_table}.#{quoted_base_pk} AS typical_sort_record_id",
      "#{aggregate}(#{sort_value_sql}) AS typical_sort_value"
    )
    .group("#{quoted_base_table}.#{quoted_base_pk}")

  records.klass
    .where(base_pk => base_ids)
    .joins(<<~SQL.squish)
      LEFT JOIN (#{sort_subquery.to_sql}) typical_sort_scope
        ON typical_sort_scope.typical_sort_record_id = #{quoted_base_table}.#{quoted_base_pk}
    SQL
    .order(Arel.sql("typical_sort_scope.typical_sort_value #{sql_direction} NULLS #{nulls_position}"))
end