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



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

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

.insert_in_slices(array) ⇒ Object

bulk insert improves performance considerably



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

def insert_in_slices array
  array.each_slice(5000) do |slice|
    insert_all slice
  end
end

.map_referees(referee_key, referee_id) ⇒ Object

map existing reference to name to card via id



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

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

.recreate_allObject

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



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

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



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

def repair_all
  clean
  each_card(&:update_references_out)
end

.unmap_referees(referee_id) ⇒ Object

references no longer refer to card, so remove id



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

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