Module: Traits::Association::Intermediate

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

Instance Method Summary collapse

Instance Method Details

#intermediate?Boolean

class Person

has_and_belongs_to_many :groups

person_traits.associations.intermediate? => true

Returns:

  • (Boolean)


12
13
14
# File 'lib/traits/association/intermediate.rb', line 12

def intermediate?
  has_and_belongs_to_many?
end

#intermediate_from_keyObject



41
42
43
44
45
# File 'lib/traits/association/intermediate.rb', line 41

def intermediate_from_key
  if intermediate?
    intermediate_table[intermediate_from_key_name]
  end
end

#intermediate_from_key_nameObject



47
48
49
50
51
# File 'lib/traits/association/intermediate.rb', line 47

def intermediate_from_key_name
  if intermediate?
    @im_from_key_name ||= reflection.association_foreign_key.to_sym
  end
end

#intermediate_tableObject



16
17
18
# File 'lib/traits/association/intermediate.rb', line 16

def intermediate_table
  Arel::Table.new(reflection.join_table) if intermediate?
end

#intermediate_table_nameObject



20
21
22
23
24
25
26
27
# File 'lib/traits/association/intermediate.rb', line 20

def intermediate_table_name
  return unless intermediate?
  unless @im_table_name
    name           = reflection.join_table
    @im_table_name = name.kind_of?(String) ? name.to_sym : name
  end
  @im_table_name
end

#intermediate_to_keyObject



29
30
31
32
33
# File 'lib/traits/association/intermediate.rb', line 29

def intermediate_to_key
  if intermediate?
    intermediate_table[intermediate_to_key_name]
  end
end

#intermediate_to_key_nameObject



35
36
37
38
39
# File 'lib/traits/association/intermediate.rb', line 35

def intermediate_to_key_name
  if intermediate?
    @im_to_key_name ||= reflection.foreign_key.to_sym
  end
end

#to_hashObject



53
54
55
56
57
58
59
60
# File 'lib/traits/association/intermediate.rb', line 53

def to_hash
  super.merge!(
    intermediate:               intermediate?,
    intermediate_table_name:    intermediate_table_name,
    intermediate_to_key_name:   intermediate_to_key_name,
    intermediate_from_key_name: intermediate_from_key_name
  )
end