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



123
124
125
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 123

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 74

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



116
117
118
119
120
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 116

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