Class: Torque::PostgreSQL::Associations::BelongsToManyAssociation

Inherits:
ActiveRecord::Associations::CollectionAssociation
  • Object
show all
Includes:
ActiveRecord::Associations::ForeignAssociation
Defined in:
lib/torque/postgresql/associations/belongs_to_many_association.rb

Instance Method Summary collapse

Instance Method Details

#build_changes(from_target = false) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 64

def build_changes(from_target = false)
  return yield if defined?(@_building_changes) && @_building_changes

  @_building_changes = true
  yield.tap { ids_writer(from_target ? ids_reader : stale_state) }
ensure
  @_building_changes = nil
end

#default(&block) ⇒ Object

BELONGS TO



144
145
146
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 144

def default(&block)
  writer(owner.instance_exec(&block)) if reader.nil?
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 42

def empty?
  size.zero?
end

#handle_dependencyObject

HAS MANY



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 95

def handle_dependency
  case options[:dependent]
  when :restrict_with_exception
    raise ActiveRecord::DeleteRestrictionError.new(reflection.name) unless empty?

  when :restrict_with_error
    unless empty?
      record = owner.class.human_attribute_name(reflection.name).downcase
      owner.errors.add(:base, :'restrict_dependent_destroy.has_many', record: record)
      throw(:abort)
    end

  when :destroy
    load_target.each { |t| t.destroyed_by_association = reflection }
    destroy_all
  when :destroy_async
    load_target.each do |t|
      t.destroyed_by_association = reflection
    end

    unless target.empty?
      association_class = target.first.class
      primary_key_column = association_class.primary_key.to_sym

      ids = target.collect do |assoc|
        assoc.public_send(primary_key_column)
      end

      enqueue_destroy_association(
        owner_model_name: owner.class.to_s,
        owner_id: owner.id,
        association_class: association_class.to_s,
        association_ids: ids,
        association_primary_key_column: primary_key_column,
        ensuring_owner_was_method: options.fetch(:ensuring_owner_was, nil)
      )
    end
  else
    delete_all
  end
end

#ids_readerObject

CUSTOM



13
14
15
16
17
18
19
20
21
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 13

def ids_reader
  if loaded?
    target.pluck(reflection.active_record_primary_key)
  elsif !target.empty?
    load_target.pluck(reflection.active_record_primary_key)
  else
    stale_state || column_default_value
  end
end

#ids_writer(ids) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 23

def ids_writer(ids)
  ids = ids.presence || column_default_value
  owner.write_attribute(source_attr, ids)
  return unless owner.persisted? && owner.attribute_changed?(source_attr)

  owner.update_attribute(source_attr, ids)
end

#include?(record) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 46

def include?(record)
  return false unless record.is_a?(reflection.klass)
  return include_in_memory?(record) if record.new_record?

  (!target.empty? && target.include?(record)) ||
    stale_state&.include?(record.read_attribute(klass_attr))
end

#insert_record(record) ⇒ Object



137
138
139
140
141
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 137

def insert_record(record, *)
  (record.persisted? || super).tap do |saved|
    ids_rewriter(record.read_attribute(klass_attr), :<<) if saved
  end
end

#load_targetObject



54
55
56
57
58
59
60
61
62
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 54

def load_target
  if stale_target? || find_target?
    persisted_records = (find_target || []) + target.extract!(&:persisted?)
    @target = merge_target_lists(persisted_records, target)
  end

  loaded!
  target
end

#sizeObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 31

def size
  if loaded?
    target.size
  elsif !target.empty?
    unsaved_records = target.select(&:new_record?)
    unsaved_records.size + stale_state.size
  else
    stale_state&.size || 0
  end
end

#trigger(prefix, before_ids, after_ids) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 73

def trigger(prefix, before_ids, after_ids)
  removed_ids = before_ids - after_ids
  added_ids = after_ids - before_ids

  if removed_ids.any?
    callbacks_for(method = :"#{prefix}_remove").each do |callback|
      target_scope.find(removed_ids).each do |record|
        callback.call(method, owner, record)
      end
    end
  end

  if added_ids.any?
    callbacks_for(method = :"#{prefix}_add").each do |callback|
      target_scope.find(added_ids).each do |record|
        callback.call(method, owner, record)
      end
    end
  end
end