Class: Card::Reference

Inherits:
ApplicationRecord show all
Defined in:
lib/card/reference.rb

Overview

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

Direct Known Subclasses

Content::Chunk::Link, Content::Chunk::Nest

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_if_referer_missingObject

remove all references from missing (eg deleted) cards



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/card/reference.rb', line 39

def delete_if_referer_missing
  joins(
    "LEFT JOIN cards ON card_references.referer_id = cards.id"
  ).where(
    "cards.id IS NULL"
  ).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



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

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], ...]



10
11
12
13
14
15
16
17
# File 'lib/card/reference.rb', line 10

def mass_insert array
  return if array.empty?
  value_statements = array.map { |values| "\n(#{values.join ', '})" }
  sql = "INSERT into card_references "\
        "(referer_id, referee_id, referee_key, ref_type) "\
        "VALUES #{value_statements.join ', '}"
  Card.connection.execute sql
end

.recreate_allObject

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



64
65
66
67
68
69
70
71
# File 'lib/card/reference.rb', line 64

def recreate_all
  delete_all
  Card.where(trash: false).find_each do |card|
    Rails.logger.info "updating references from #{card}"
    card.include_set_modules
    card.create_references_out
  end
end

.repair_allObject

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



53
54
55
56
57
58
59
60
# File 'lib/card/reference.rb', line 53

def repair_all
  delete_if_referer_missing
  Card.where(trash: false).find_each do |card|
    Rails.logger.info "updating references from #{card}"
    card.include_set_modules
    card.update_references_out
  end
end

.unmap_if_referee_missingObject

find all references to missing (eg deleted) cards and reset them



30
31
32
33
34
35
36
# File 'lib/card/reference.rb', line 30

def unmap_if_referee_missing
  joins(
    "LEFT JOIN cards ON card_references.referee_id = cards.id"
  ).where(
    "(cards.id IS NULL OR cards.trash IS TRUE) AND referee_id IS NOT NULL"
  ).update_all referee_id: nil
end

.unmap_referees(referee_id) ⇒ Object

references no longer refer to card, so remove id



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

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



80
81
82
# File 'lib/card/reference.rb', line 80

def referee
  Card[referee_id]
end

#refererObject

card that refers



75
76
77
# File 'lib/card/reference.rb', line 75

def referer
  Card[referer_id]
end