Module: Torque::PostgreSQL::Relation

Extended by:
ActiveSupport::Concern
Includes:
AuxiliaryStatement, DistinctOn, Inheritance
Defined in:
lib/torque/postgresql/relation.rb,
lib/torque/postgresql/relation/merger.rb,
lib/torque/postgresql/relation/distinct_on.rb,
lib/torque/postgresql/relation/inheritance.rb,
lib/torque/postgresql/relation/auxiliary_statement.rb

Defined Under Namespace

Modules: AuxiliaryStatement, ClassMethods, DistinctOn, Inheritance, Initializer, Merger

Constant Summary collapse

SINGLE_VALUE_METHODS =
[:itself_only]
MULTI_VALUE_METHODS =
[:distinct_on, :auxiliary_statements, :cast_records, :select_extra]
VALUE_METHODS =
SINGLE_VALUE_METHODS + MULTI_VALUE_METHODS

Instance Method Summary collapse

Methods included from Inheritance

#cast_records, #cast_records!, #cast_records_value, #cast_records_value=, #itself_only, #itself_only!, #itself_only_value, #itself_only_value=

Methods included from AuxiliaryStatement

#auxiliary_statements_values, #auxiliary_statements_values=, #bound_attributes, #with, #with!

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



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

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

#resolve_base_table(relation) ⇒ Object

Get the TableMetadata from a relation



67
68
69
70
71
72
73
74
75
76
# File 'lib/torque/postgresql/relation.rb', line 67

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


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/torque/postgresql/relation.rb', line 43

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_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

#select_extra_valuesObject

:nodoc:



21
# File 'lib/torque/postgresql/relation.rb', line 21

def select_extra_values; get_value(:select_extra); end

#select_extra_values=(value) ⇒ Object

:nodoc:



23
# File 'lib/torque/postgresql/relation.rb', line 23

def select_extra_values=(value); set_value(:select_extra, value); end