Module: Torque::PostgreSQL::Relation

Extended by:
ActiveSupport::Concern
Includes:
DistinctOn, Inheritance
Included in:
ActiveRecord::Relation
Defined in:
lib/torque/postgresql/relation.rb,
lib/torque/postgresql/relation/merger.rb,
lib/torque/postgresql/relation/buckets.rb,
lib/torque/postgresql/relation/segments.rb,
lib/torque/postgresql/relation/cross_join.rb,
lib/torque/postgresql/relation/distinct_on.rb,
lib/torque/postgresql/relation/inheritance.rb,
lib/torque/postgresql/relation/join_series.rb,
lib/torque/postgresql/relation/auxiliary_statement.rb,
sig/torque/postgresql/relation.rbs,
sig/torque/postgresql/relation/merger.rbs,
sig/torque/postgresql/relation/buckets.rbs,
sig/torque/postgresql/relation/segments.rbs,
sig/torque/postgresql/relation/cross_join.rbs,
sig/torque/postgresql/relation/distinct_on.rbs,
sig/torque/postgresql/relation/inheritance.rbs,
sig/torque/postgresql/relation/join_series.rbs,
sig/torque/postgresql/relation/auxiliary_statement.rbs

Defined Under Namespace

Modules: AuxiliaryStatement, Buckets, ClassMethods, CrossJoin, DistinctOn, Inheritance, Initializer, JoinSeries, Merger, Segments

Constant Summary collapse

SINGLE_VALUE_METHODS =

Returns:

  • (Array[Symbol])
%i[
  itself_only buckets segments expand_records_eager_load expand_records_scoped
]
MULTI_VALUE_METHODS =

Returns:

  • (Array[Symbol])
%i[
  select_extra distinct_on auxiliary_statements expand_records
]
VALUE_METHODS =

Returns:

  • (Array[Symbol])
SINGLE_VALUE_METHODS + MULTI_VALUE_METHODS
FROZEN_EMPTY_ARRAY =

Returns:

  • (Array[untyped])
::ActiveRecord::QueryMethods::FROZEN_EMPTY_ARRAY
ARColumn =

Returns:

  • (singleton(ActiveRecord::ConnectionAdapters::PostgreSQL::Column))
::ActiveRecord::ConnectionAdapters::PostgreSQL::Column

Constants included from Inheritance

Inheritance::RECORD_CLASS_TOKEN

Instance Method Summary collapse

Methods included from Inheritance

#expand_records, #expand_records!, #expand_records_eager_load_value, #expand_records_eager_load_value=, #expand_records_scoped_value, #expand_records_scoped_value=, #expand_records_values, #expand_records_values=, #itself_only, #itself_only!, #itself_only_value, #itself_only_value=, #quote_column_name, #quote_table_name, #select

Methods included from DistinctOn

#distinct_on, #distinct_on!, #distinct_on_values, #distinct_on_values=

Instance Method Details

#calculate(operation, column_name) ⇒ Object

Resolve column name when calculating models, allowing the column name to be more complex while keeping the query selection quality

Parameters:

  • operation (Symbol, String)
  • column_name (column_spec, nil)

Returns:

  • (Object)


40
41
42
43
# File 'lib/torque/postgresql/relation.rb', line 40

def calculate(operation, column_name)
  column_name = resolve_column(column_name).first if column_name.is_a?(Hash)
  super(operation, column_name)
end

#cast_for_condition(column, value) ⇒ Object

Serialize the given value so it can be used in a condition tha involves the given column

Parameters:

  • column (column_ref, column)
  • value (Object)

Returns:

  • (Object)


94
95
96
97
98
99
100
101
# File 'lib/torque/postgresql/relation.rb', line 94

def cast_for_condition(column, value)
  column = columns_hash[column.to_s] unless column.is_a?(ARColumn)

  klass.with_connection do |connection|
    caster = connection.lookup_cast_type_for_column(column)
    connection.type_cast(caster.serialize(value))
  end
end

#resolve_base_table(relation) ⇒ model, ...

Get the TableMetadata from a relation

Parameters:

  • relation (column_ref, false)

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/torque/postgresql/relation.rb', line 77

def resolve_base_table(relation)
  return unless relation

  table = predicate_builder.send(:table)
  part = part_table_for(table, relation.to_s)
  return part if part

  associated = AR810 ? table.associated_with(relation.to_s) : table.associated_with?(relation.to_s)
  if associated
    table.associated_table(relation.to_s).send(:klass)
  else
    raise ArgumentError, "Relation for #{relation} not found on #{klass}"
  end
end

#resolve_column(list, base = false) ⇒ Array[arel_node]

Resolve column definition up to second value. For example, based on Post model:

resolve_column(['name', :title])
# Returns ['name', '"posts"."title"']

resolve_column([:title, {authors: :name}])
# Returns ['"posts"."title"', '"authors"."name"']

resolve_column([{authors: [:name, :age]}])
# Returns ['"authors"."name"', '"authors"."age"']

Parameters:

  • list (column_spec)
  • base (column_ref, false) (defaults to: false)

Returns:

  • (Array[arel_node])


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/torque/postgresql/relation.rb', line 56

def resolve_column(list, base = false)
  base = resolve_base_table(base)

  Array.wrap(list).map do |item|
    case item
    when String
      ::Arel.sql(klass.send(:sanitize_sql, item.to_s))
    when Symbol
      base ? base.arel_table[item] : klass.arel_table[item]
    when Array
      resolve_column(item, base)
    when Hash
      raise ArgumentError, 'Unsupported Hash for attributes on third level' if base
      item.map { |key, other_list| resolve_column(other_list, key) }
    else
      raise ArgumentError, "Unsupported argument type: #{value} (#{value.class})"
    end
  end.flatten
end

#select_extra_values ⇒ Array[arel_node]

:nodoc:

Returns:

  • (Array[arel_node])


29
30
31
# File 'lib/torque/postgresql/relation.rb', line 29

def select_extra_values
  @values.fetch(:select_extra, FROZEN_EMPTY_ARRAY)
end

#select_extra_values=(value) ⇒ Array[arel_node]

:nodoc:

Parameters:

  • value (Array[arel_node])

Returns:

  • (Array[arel_node])


33
34
35
36
# File 'lib/torque/postgresql/relation.rb', line 33

def select_extra_values=(value)
  assert_modifiable!
  @values[:select_extra] = value
end