Class: Card::Reference

Inherits:
ApplicationRecord show all
Defined in:
card/lib/card/reference.rb,
card/lib/card/reference/all.rb,
card-mod-tinymce_editor/lib/card/reference/nest_parser.rb

Overview

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

Direct Known Subclasses

Content::Chunk::Nest

Defined Under Namespace

Modules: All Classes: NestParser

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_if_referer_missingObject

remove all references from missing (eg deleted) cards



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

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



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

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
18
# File 'card/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



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

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



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

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



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

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



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

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



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

def referee
  Card[referee_id]
end

#refererObject

card that refers



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

def referer
  Card[referer_id]
end