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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  size.zero?
end

#handle_dependencyObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 9

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
    # No point in executing the counter update since we're going to destroy the parent anyway
    load_target.each { |t| t.destroyed_by_association = reflection }
    destroy_all
  else
    delete_all
  end
end

#ids_readerObject



30
31
32
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 30

def ids_reader
  owner[reflection.active_record_primary_key]
end

#ids_writer(new_ids) ⇒ Object



34
35
36
37
38
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 34

def ids_writer(new_ids)
  column = reflection.active_record_primary_key
  owner.update_column(column, owner[column] = new_ids.presence)
  @association_scope = nil
end

#include?(record) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 52

def include?(record)
  list = owner[reflection.active_record_primary_key]
  ids_reader && ids_reader.include?(record[klass_fk])
end

#insert_record(record) ⇒ Object



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

def insert_record(record, *)
  super

  attribute = (ids_reader || owner[reflection.active_record_primary_key] = [])
  attribute.push(record[klass_fk])
  record
end