Module: Traits::Association::Join

Included in:
Traits::Association
Defined in:
lib/traits/association/join.rb

Instance Method Summary collapse

Instance Method Details

#from_keyObject



19
20
21
# File 'lib/traits/association/join.rb', line 19

def from_key
  from_table[from_key_name]
end

#from_key_nameObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/traits/association/join.rb', line 23

def from_key_name
  if polymorphic?
    reflection.foreign_key.to_sym

  elsif through?
    through_association.from_key_name

  elsif features.try(:translates_with_globalize?)
    nil

  elsif belongs_to?
    reflection.foreign_key.to_sym

  end || reflection.active_record_primary_key.to_sym
end

#from_tableObject



7
8
9
# File 'lib/traits/association/join.rb', line 7

def from_table
  from.arel
end

#from_table_aliasObject



15
16
17
# File 'lib/traits/association/join.rb', line 15

def from_table_alias
  from_table.table_alias.try(:to_sym)
end

#from_table_nameObject



11
12
13
# File 'lib/traits/association/join.rb', line 11

def from_table_name
  from_table.name.to_sym
end

#to_hashObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/traits/association/join.rb', line 79

def to_hash
  super.merge!(
    from_table_name:  from_table_name,
    from_table_alias: from_table_alias,
    from_key_name:    from_key_name,

    to_table_name:    to_table_name,
    to_table_alias:   to_table_alias,
    to_key_name:      to_key_name
  )
end

#to_keyObject



61
62
63
# File 'lib/traits/association/join.rb', line 61

def to_key
  to_table.try(:[], to_key_name)
end

#to_key_nameObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/traits/association/join.rb', line 65

def to_key_name
  unless polymorphic?
    if through?
      source_association.to_key_name

    elsif belongs_to? || has_and_belongs_to_many?
      reflection.association_primary_key.to_sym

    else
      reflection.foreign_key.to_sym
    end
  end
end

#to_tableObject



39
40
41
42
43
44
45
46
47
# File 'lib/traits/association/join.rb', line 39

def to_table
  unless polymorphic?
    table = to.arel.clone
    if self_to_self?
      table.table_alias = "#{plural_name}_#{from.table_name}"
    end
    table
  end
end

#to_table_aliasObject



55
56
57
58
59
# File 'lib/traits/association/join.rb', line 55

def to_table_alias
  unless polymorphic?
    to_table.table_alias.try(:to_sym)
  end
end

#to_table_nameObject



49
50
51
52
53
# File 'lib/traits/association/join.rb', line 49

def to_table_name
  unless polymorphic?
    to_table.name.to_sym
  end
end