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

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

Instance Method Summary collapse

Instance Method Details

#all_file_idsObject



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

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

#all_trashed_card_idsObject



53
54
55
56
57
# File 'card/tmpsets/set/mod002-core/all/trash.rb', line 53

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_files_with_id(file_id) ⇒ Object

Raises:



44
45
46
47
48
# File 'card/tmpsets/set/mod002-core/all/trash.rb', line 44

def delete_files_with_id file_id
  raise Card::Error, t(:core_exception_almost_deleted) if Card.exists?(file_id)

  ::FileUtils.rm_rf "#{dir}/#{file_id}", secure: true
end

#delete_trashed_filesObject

deletes any file not associated with a real card.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'card/tmpsets/set/mod002-core/all/trash.rb', line 34

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
30
31
# File 'card/tmpsets/set/mod002-core/all/trash.rb', line 22

def empty_trash
  Card.delete_trashed_files
  Card.where(trash: true).in_batches.update_all(left_id: nil, right_id: nil)
  Card.where(trash: true).in_batches.delete_all
  Card::Action.delete_cardless
  Card::Change.delete_actionless
  Card::Act.delete_actionless
  Card::Reference.unmap_if_referee_missing
  Card::Reference.delete_if_referer_missing
end