Module: Card::Set::All::Utils::ClassMethods

Defined in:
tmpsets/set/mod001-core/all/utils.rb

Overview

~~ above autogenerated; below pulled from /Users/ethan/dev/wagn/gem/card/mod/core/set/all/utils.rb ~~

Instance Method Summary collapse

Instance Method Details

#all_file_idsObject



36
37
38
39
# File 'tmpsets/set/mod001-core/all/utils.rb', line 36

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

#all_trashed_card_idsObject



41
42
43
44
45
# File 'tmpsets/set/mod001-core/all/utils.rb', line 41

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_tmp_files(id = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'tmpsets/set/mod001-core/all/utils.rb', line 28

def delete_tmp_files id=nil
  dir = Cardio.paths["files"].existent.first + "/tmp"
  dir += "/#{id}" if id
  FileUtils.rm_rf dir, secure: true
rescue
  Rails.logger.info "failed to remove tmp files"
end

#delete_tmp_files_of_cached_uploadsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'tmpsets/set/mod001-core/all/utils.rb', line 47

def delete_tmp_files_of_cached_uploads
  actions = Card::Action.find_by_sql "SELECT * FROM card_actions
    INNER JOIN cards ON card_actions.card_id = cards.id
    WHERE cards.type_id IN (#{Card::FileID}, #{Card::ImageID})
    AND card_actions.draft = true"
  actions.each do |action|
    # we don't want to delete uploads in progress
    if older_than_five_days?(action.created_at) && (card = action.card)
      # we don't want to delete uploads in progress
      card.delete_files_for_action action
    end
  end
end

#delete_trashed_filesObject

deletes any file not associated with a real card.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'tmpsets/set/mod001-core/all/utils.rb', line 15

def delete_trashed_files
  trashed_card_ids = all_trashed_card_ids
  file_ids = all_file_ids
  dir = Cardio.paths["files"].existent.first
  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



5
6
7
8
9
10
11
12
# File 'tmpsets/set/mod001-core/all/utils.rb', line 5

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
  Card.delete_tmp_files_of_cached_uploads
end

#merge(name, attribs = {}, opts = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'tmpsets/set/mod001-core/all/utils.rb', line 89

def merge name, attribs={}, opts={}
  # puts "merging #{name}"
  card = fetch name, new: {}
  [:image, :file].each do |attach|
    next unless attribs[attach] && attribs[attach].is_a?(String)
    attribs[attach] = ::File.open(attribs[attach])
  end
  if opts[:pristine] && !card.pristine?
    false
  else
    card.update_attributes! attribs
  end
end

#merge_list(attribs, opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'tmpsets/set/mod001-core/all/utils.rb', line 61

def merge_list attribs, opts={}
  unmerged = []
  attribs.each do |row|
    result = begin
      merge row["name"], row, opts
    end
    unmerged.push row unless result == true
  end

  if unmerged.empty?
    Rails.logger.info "successfully merged all!"
  else
    unmerged_json = JSON.pretty_generate unmerged
    report_unmerged_json unmerged_json, opts[:output_file]
  end
  unmerged
end

#older_than_five_days?(time) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'tmpsets/set/mod001-core/all/utils.rb', line 103

def older_than_five_days? time
  Time.now - time > 432_000
end

#report_unmerged_json(unmerged_json, output_file) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'tmpsets/set/mod001-core/all/utils.rb', line 79

def report_unmerged_json unmerged_json, output_file
  if output_file
    ::File.open output_file, "w" do |f|
      f.write unmerged_json
    end
  else
    Rails.logger.info "failed to merge:\n\n#{unmerged_json}"
  end
end