Module: Torque::PostgreSQL::Relation

Includes:
AuxiliaryStatement, DistinctOn
Defined in:
lib/torque/postgresql/relation.rb,
lib/torque/postgresql/relation/distinct_on.rb,
lib/torque/postgresql/relation/auxiliary_statement.rb

Defined Under Namespace

Modules: AuxiliaryStatement, DistinctOn

Instance Attribute Summary

Attributes included from AuxiliaryStatement

#auxiliary_statements

Attributes included from DistinctOn

#distinct_on_value

Instance Method Summary collapse

Methods included from AuxiliaryStatement

#bound_attributes, #with, #with!

Methods included from DistinctOn

#distinct_on, #distinct_on!

Instance Method Details

#resolve_base_table(relation) ⇒ Object

Get the TableMetadata from a relation



47
48
49
50
51
52
53
54
55
56
# File 'lib/torque/postgresql/relation.rb', line 47

def resolve_base_table(relation)
  return unless relation

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

#resolve_column(list, base = false) ⇒ Object

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"']


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/torque/postgresql/relation.rb', line 23

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

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