class GraphQL::FancyLoader::RankQueryGenerator

Public Class Methods

new(column:, partition_by:, table:, name_suffix: '_rank') click to toggle source

@param column [Symbol] The table column to rank by @param partition_by [Symbol] The find_by key for the table @param table [Arel::Table] @param name_suffix [String] The suffix the be used for the column name

# File lib/graphql/fancy_loader/rank_query_generator.rb, line 9
def initialize(column:, partition_by:, table:, name_suffix: '_rank')
  @column = column
  @partition_by = partition_by
  @table = table
  @name_suffix = name_suffix
end

Public Instance Methods

arel() click to toggle source

Our actual window function.

ROW_NUMBER() OVER (#{partition})
# File lib/graphql/fancy_loader/rank_query_generator.rb, line 19
def arel
  Arel::Nodes::NamedFunction.new('ROW_NUMBER', []).over(partition).as(name)
end

Private Instance Methods

name() click to toggle source
# File lib/graphql/fancy_loader/rank_query_generator.rb, line 25
def name
  return @column if @name_suffix.blank?

  "#{@column}#{@name_suffix}"
end
order() click to toggle source
# File lib/graphql/fancy_loader/rank_query_generator.rb, line 35
def order
  @table[@column].asc
end
partition() click to toggle source
# File lib/graphql/fancy_loader/rank_query_generator.rb, line 31
def partition
  @partition ||= Arel::Nodes::Window.new.partition(@table[@partition_by]).order(order)
end