Class: Card::Reference
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Card::Reference
- 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
Class Method Summary collapse
-
.delete_if_referer_missing ⇒ Object
remove all references from missing (eg deleted) cards.
-
.map_referees(referee_key, referee_id) ⇒ Object
map existing reference to name to card via id.
-
.mass_insert(array) ⇒ Object
bulk insert improves performance considerably array takes form [ [referer_id, referee_id, referee_key, ref_type], ...].
-
.recreate_all ⇒ Object
delete all references, then recreate them one by one faster than #repair_all, but not recommended for use on running sites.
-
.repair_all ⇒ Object
repair references one by one (delete, create, delete, create...) slower, but better than #repair_all for use on running sites.
-
.unmap_if_referee_missing ⇒ Object
find all references to missing (eg deleted) cards and reset them.
-
.unmap_referees(referee_id) ⇒ Object
references no longer refer to card, so remove id.
Instance Method Summary collapse
-
#referee ⇒ Object
card that is referred to.
-
#referer ⇒ Object
card that refers.
Class Method Details
.delete_if_referer_missing ⇒ Object
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" ).find_in_batches do |group| # used to be .delete_all here, but that was failing on large dbs puts "deleting batch of references" where("id in (#{group.map(&:id).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_all ⇒ Object
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_all ⇒ Object
repair references one by one (delete, create, delete, create...) slower, but better than #repair_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_missing ⇒ Object
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 |