Module: Card::Reference::All

Included in:
Card
Defined in:
lib/card/reference/all.rb

Overview

Cards can refer to other cards in their content, eg via links and nests. The card that refers is the “referer”, the card that is referred to is the “referee”. The reference itself has its own class (Card::Reference), which handles id-based reference tracking.

Constant Summary collapse

PARTIAL_REF_CODE =
"P".freeze

Instance Method Summary collapse

Instance Method Details

#create_references_outObject

interpret references from this card’s content and insert entries in reference table



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

def create_references_out
  ref_hash = {}
  each_reference_out do |referee_name, ref_type|
    interpret_reference ref_hash, referee_name, ref_type
  end
  return if ref_hash.empty?

  Reference.insert_in_slices reference_values_array(ref_hash)
end

#name_referersObject

cards that refer to self by name (finds cards not yet linked by id)



42
43
44
# File 'lib/card/reference/all.rb', line 42

def name_referers
  Card.joins(:references_out).where card_references: { referee_key: key }
end

#nesteesObject

cards that self includes



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

def nestees
  referees_from_references references_out.where(ref_type: "I")
end

#nestersObject

cards that include self



18
19
20
# File 'lib/card/reference/all.rb', line 18

def nesters
  referer_cards_from_references references_in.where(ref_type: "I")
end

#refereesObject

cards that self refers to



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

def referees
  referees_from_references references_out
end

#referees_from_references(references) ⇒ Object



36
37
38
# File 'lib/card/reference/all.rb', line 36

def referees_from_references references
  references.map(&:referee_key).uniq.map { |key| Card.fetch key, new: {} }
end

#referer_cards_from_references(references) ⇒ Object



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

def referer_cards_from_references references
  references.map(&:referer_id).uniq.map(&:card).compact
end

#referersObject

cards that refer to self



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

def referers
  referer_cards_from_references references_in
end

#swap_names(old_name, new_name) ⇒ Object

replace references in card content



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

def swap_names old_name, new_name
  cont = content_object
  cont.find_chunks(:Reference).each do |chunk|
    chunk.swap_name old_name, new_name
  end
  cont.to_s
end

#update_references_outObject

delete old references from this card’s content, create new ones



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

def update_references_out
  delete_references_out
  create_references_out
end