Module: Torque::PostgreSQL::Relation::Inheritance

Included in:
Torque::PostgreSQL::Relation
Defined in:
lib/torque/postgresql/relation/inheritance.rb

Defined Under Namespace

Modules: Expansion

Constant Summary collapse

RECORD_CLASS_TOKEN =
:_regclass

Instance Method Summary collapse

Instance Method Details

#expand_records(*types, **options) ⇒ Object

Load the columns that only exist on the inherited tables, so that records come out complete and writable. Defaults to every dependent that adds columns

Activity.expand_records
# Runs one additional query per inherited table

Activity.expand_records(ActivityBook, eager_load: true)
# Runs a single query using outer joins


78
79
80
# File 'lib/torque/postgresql/relation/inheritance.rb', line 78

def expand_records(*types, **options)
  spawn.expand_records!(*types, **options)
end

#expand_records!(*types, eager_load: false, filter: false) ⇒ Object

Like #expand_records, but modifies relation in place



83
84
85
86
87
88
89
90
91
92
# File 'lib/torque/postgresql/relation/inheritance.rb', line 83

def expand_records!(*types, eager_load: false, filter: false)
  raise_itself_only_conflict! if itself_only_value === true

  types = types.presence || model.inheritance_expandable_dependents.values

  where!(regclass.pg_cast(:varchar).in(types.map(&:table_name))) if filter
  self.expand_records_values = types
  self.expand_records_eager_load_value = eager_load
  self
end

#expand_records_eager_load_valueObject

:nodoc:



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

def expand_records_eager_load_value
  @values.fetch(:expand_records_eager_load, nil)
end

#expand_records_eager_load_value=(value) ⇒ Object

:nodoc:



23
24
25
26
# File 'lib/torque/postgresql/relation/inheritance.rb', line 23

def expand_records_eager_load_value=(value)
  assert_modifiable!
  @values[:expand_records_eager_load] = value
end

#expand_records_scoped_valueObject

:nodoc:



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

def expand_records_scoped_value
  @values.fetch(:expand_records_scoped, nil) || {}
end

#expand_records_scoped_value=(value) ⇒ Object

:nodoc:



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

def expand_records_scoped_value=(value)
  assert_modifiable!
  @values[:expand_records_scoped] = value
end

#expand_records_valuesObject

:nodoc:



9
10
11
# File 'lib/torque/postgresql/relation/inheritance.rb', line 9

def expand_records_values
  @values.fetch(:expand_records, FROZEN_EMPTY_ARRAY)
end

#expand_records_values=(value) ⇒ Object

:nodoc:



13
14
15
16
# File 'lib/torque/postgresql/relation/inheritance.rb', line 13

def expand_records_values=(value)
  assert_modifiable!
  @values[:expand_records] = value
end

#itself_onlyObject

Specify that the results should come only from the table that the entries were created on. For example:

Activity.itself_only
# Does not return entries for inherited tables


57
58
59
# File 'lib/torque/postgresql/relation/inheritance.rb', line 57

def itself_only
  spawn.itself_only!
end

#itself_only!Object

Like #itself_only, but modifies relation in place.



62
63
64
65
66
67
# File 'lib/torque/postgresql/relation/inheritance.rb', line 62

def itself_only!(*)
  raise_itself_only_conflict! if expand_records_values.present?

  self.itself_only_value = true
  self
end

#itself_only_valueObject

:nodoc:



39
40
41
# File 'lib/torque/postgresql/relation/inheritance.rb', line 39

def itself_only_value
  @values.fetch(:itself_only, nil)
end

#itself_only_value=(value) ⇒ Object

:nodoc:



43
44
45
46
# File 'lib/torque/postgresql/relation/inheritance.rb', line 43

def itself_only_value=(value)
  assert_modifiable!
  @values[:itself_only] = value
end

#select(*fields) ⇒ Object

Accept the record class marker as a column, which is the only way an explicit selection can still produce casted records

Activity.select(:_regclass, :id, :title)


98
99
100
101
102
103
# File 'lib/torque/postgresql/relation/inheritance.rb', line 98

def select(*fields)
  return super if fields.empty?
  return super(*fields, build_record_class_marker) if fields.delete(RECORD_CLASS_TOKEN)

  super
end