class Card::Reference

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

Public Class Methods

clean() click to toggle source

remove reference to and from missing cards

# 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
insert_in_slices(array) click to toggle source

bulk insert improves performance considerably

# File lib/card/reference.rb, line 18
def insert_in_slices array
  array.each_slice(5000) do |slice|
    insert_all slice
  end
end
map_referees(referee_key, referee_id) click to toggle source

map existing reference to name to card via id

# 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
recreate_all() click to toggle source

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

# File lib/card/reference.rb, line 53
def recreate_all
  delete_all
  each_card(&:create_references_out)
end
repair_all() click to toggle source

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

# File lib/card/reference.rb, line 46
def repair_all
  clean
  each_card(&:update_references_out)
end
unmap_referees(referee_id) click to toggle source

references no longer refer to card, so remove id

# File lib/card/reference.rb, line 30
def unmap_referees referee_id
  where(referee_id: referee_id).update_all referee_id: nil
end

Private Class Methods

each_card() { |include_set_modules| ... } click to toggle source
# File lib/card/reference.rb, line 66
def each_card
  Card.where(trash: false).find_each do |card|
    Rails.logger.debug "references from #{card.name}"
    yield card.include_set_modules
  end
end
missing(field) click to toggle source

find all references to or from missing (eg deleted) cards

# File lib/card/reference.rb, line 61
def missing field
  joins("LEFT JOIN cards ON card_references.#{field} = cards.id")
    .where("(cards.id IS NULL OR cards.trash IS TRUE)")
end

Public Instance Methods

referee() click to toggle source

card that is referred to

# File lib/card/reference.rb, line 12
def referee
  Card[referee_id]
end
referer() click to toggle source

card that refers

# File lib/card/reference.rb, line 7
def referer
  Card[referer_id]
end