Module: Card::Set::All::Trash::ClassMethods

Defined in:
tmpsets/set/mod002-core/all/trash.rb

Instance Method Summary collapse

Instance Method Details

#all_file_idsObject



42
43
44
45
# File 'tmpsets/set/mod002-core/all/trash.rb', line 42

def all_file_ids
  dir = Card.paths["files"].existent.first
  Dir.entries(dir)[2..-1].map(&:to_i)
end

#all_trashed_card_idsObject



47
48
49
50
51
# File 'tmpsets/set/mod002-core/all/trash.rb', line 47

def all_trashed_card_ids
  trashed_card_sql = %( select id from cards where trash is true )
  sql_results = Card.connection.select_all(trashed_card_sql)
  sql_results.map(&:values).flatten.map(&:to_i)
end

#delete_trashed_filesObject

deletes any file not associated with a real card.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'tmpsets/set/mod002-core/all/trash.rb', line 26

def delete_trashed_files
  dir = Cardio.paths["files"].existent.first
  # TODO: handle cloud files
  return unless dir

  trashed_card_ids = all_trashed_card_ids
  file_ids = all_file_ids
  file_ids.each do |file_id|
    next unless trashed_card_ids.member?(file_id)
    if Card.exists?(file_id) # double check!
      raise Card::Error, "Narrowly averted deleting current file"
    end
    FileUtils.rm_rf "#{dir}/#{file_id}", secure: true
  end
end

#empty_trashObject



17
18
19
20
21
22
23
# File 'tmpsets/set/mod002-core/all/trash.rb', line 17

def empty_trash
  Card.delete_trashed_files
  Card.where(trash: true).delete_all
  Card::Action.delete_cardless
  Card::Reference.unmap_if_referee_missing
  Card::Reference.delete_if_referer_missing
end