Class: IronTrail::Reflection

Inherits:
ActiveRecord::Reflection::AssociationReflection
  • Object
show all
Defined in:
lib/iron_trail/reflection.rb

Instance Method Summary collapse

Instance Method Details

#association_classObject



7
8
9
# File 'lib/iron_trail/reflection.rb', line 7

def association_class
  ::IronTrail::Association
end

#collection?Boolean

Returns:

  • (Boolean)


5
# File 'lib/iron_trail/reflection.rb', line 5

def collection? = true

#join_scope(table, foreign_table, _foreign_klass) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/iron_trail/reflection.rb', line 11

def join_scope(table, foreign_table, _foreign_klass)
  scope = klass_join_scope(table, nil)

  foreign_key_column_names = Array(join_foreign_key)
  if foreign_key_column_names.length > 1
    raise "IronTrail does not support composite foreign keys (got #{foreign_key_column_names})"
  end

  foreign_key_column_name = foreign_key_column_names.first

  # record_id is always of type text, but the foreign table primary key
  # could be anything (int, uuid, ...), so let's cast it to text.
  foreign_value = ::Arel::Nodes::NamedFunction.new(
    'CAST',
    [
      ::Arel::Nodes::As.new(
        foreign_table[foreign_key_column_name],
        ::Arel::Nodes::SqlLiteral.new('text')
      )
    ]
  )

  scope.where!(
    table['rec_id']
      .eq(foreign_value)
      .and(
        table['rec_table']
        .eq(foreign_table.name)
      )
  )

  scope
end