Class: ActiveRecord::Associations::Builder::HasAndBelongsToMany

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association_name, lhs_model, options) ⇒ HasAndBelongsToMany

Returns a new instance of HasAndBelongsToMany.


7
8
9
10
11
# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 7

def initialize(association_name, lhs_model, options)
  @association_name = association_name
  @lhs_model = lhs_model
  @options = options
end

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name


5
6
7
# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 5

def association_name
  @association_name
end

#lhs_modelObject (readonly)

Returns the value of attribute lhs_model


5
6
7
# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 5

def lhs_model
  @lhs_model
end

#optionsObject (readonly)

Returns the value of attribute options


5
6
7
# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 5

def options
  @options
end

Instance Method Details

#middle_reflection(join_model) ⇒ Object


63
64
65
66
67
68
69
70
71
72
# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 63

def middle_reflection(join_model)
  middle_name = [lhs_model.name.downcase.pluralize,
                 association_name.to_s].sort.join("_").gsub("::", "_").to_sym
  middle_options = middle_options join_model

  HasMany.create_reflection(lhs_model,
                            middle_name,
                            nil,
                            middle_options)
end

#through_modelObject


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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 13

def through_model
  join_model = Class.new(ActiveRecord::Base) {
    class << self
      attr_accessor :left_model
      attr_accessor :name
      attr_accessor :table_name_resolver
      attr_accessor :left_reflection
      attr_accessor :right_reflection
    end

    def self.table_name
      # Table name needs to be resolved lazily
      # because RHS class might not have been loaded
      @table_name ||= table_name_resolver.call
    end

    def self.compute_type(class_name)
      left_model.compute_type class_name
    end

    def self.add_left_association(name, options)
      belongs_to name, required: false, **options
      self.left_reflection = _reflect_on_association(name)
    end

    def self.add_right_association(name, options)
      rhs_name = name.to_s.singularize.to_sym
      belongs_to rhs_name, required: false, **options
      self.right_reflection = _reflect_on_association(rhs_name)
    end

    def self.retrieve_connection
      left_model.retrieve_connection
    end

    private
      def self.suppress_composite_primary_key(pk)
        pk unless pk.is_a?(Array)
      end
  }

  join_model.name                = "HABTM_#{association_name.to_s.camelize}"
  join_model.table_name_resolver = -> { table_name }
  join_model.left_model          = lhs_model

  join_model.add_left_association :left_side, anonymous_class: lhs_model
  join_model.add_right_association association_name, belongs_to_options(options)
  join_model
end