Class: Card::Reference

Inherits:
Cardio::Record show all
Defined in:
lib/card/reference.rb,
lib/card/reference/all.rb

Overview

a Reference is a directional relationship from one card (the referer) to another (the referee).

Defined Under Namespace

Modules: All

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cleanObject

remove reference to and from missing cards



35
36
37
38
39
40
41
42
# File 'lib/card/reference.rb', line 35

def clean
  missing(:referee_id).where("referee_id IS NOT NULL").update_all referee_id: nil
  missing(:referer_id).pluck_in_batches(:id) do |group_ids|
    # used to be .delete_all here, but that was failing on large dbs
    Rails.logger.info "deleting batch of references"
    where("id in (#{group_ids.join ','})").delete_all
  end
end

.map_referees(referee_key, referee_id) ⇒ Object

map existing reference to name to card via id



25
26
27
# File 'lib/card/reference.rb', line 25

def map_referees referee_key, referee_id
  where(referee_key: referee_key).update_all referee_id: referee_id
end

.mass_insert(array) ⇒ Object

bulk insert improves performance considerably array takes form [ [referer_id, referee_id, referee_key, ref_type], …]



20
21
22
# File 'lib/card/reference.rb', line 20

def mass_insert array
  Card.connection.execute mass_insert_sql(array) if array.present?
end

.recreate_allObject

delete all references, then recreate them one by one faster than #repair_all, but not recommended for use on running sites



53
54
55
56
# File 'lib/card/reference.rb', line 53

def recreate_all
  delete_all
  each_card(&:create_references_out)
end

.repair_allObject

repair references one by one (delete, create, delete, create…) slower, but better than #recreate_all for use on running sites



46
47
48
49
# File 'lib/card/reference.rb', line 46

def repair_all
  clean
  each_card(&:update_references_out)
end

.unmap_referees(referee_id) ⇒ Object

references no longer refer to card, so remove id



30
31
32
# File 'lib/card/reference.rb', line 30

def unmap_referees referee_id
  where(referee_id: referee_id).update_all referee_id: nil
end

Instance Method Details

#refereeObject

card that is referred to



13
14
15
# File 'lib/card/reference.rb', line 13

def referee
  Card[referee_id]
end

#refererObject

card that refers



8
9
10
# File 'lib/card/reference.rb', line 8

def referer
  Card[referer_id]
end