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

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

Instance Method Summary collapse

Instance Method Details

#all_file_idsObject



46
47
48
49
# File 'tmpsets/set/mod003-core/all/trash.rb', line 46

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

#all_trashed_card_idsObject



51
52
53
54
55
# File 'tmpsets/set/mod003-core/all/trash.rb', line 51

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.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'tmpsets/set/mod003-core/all/trash.rb', line 32

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)
    raise Card::Error, tr(:exception_almost_deleted) if Card.exists?(file_id)
    ::FileUtils.rm_rf "#{dir}/#{file_id}", secure: true
  end
end

#empty_trashObject



22
23
24
25
26
27
28
29
# File 'tmpsets/set/mod003-core/all/trash.rb', line 22

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