Module: ChangeDetection

Extended by:
ActiveSupport::Concern
Included in:
Library
Defined in:
app/models/concerns/change_detection.rb

Instance Method Summary collapse

Instance Method Details

#filter_out_common_subfolders(folders) ⇒ Object



34
35
36
37
# File 'app/models/concerns/change_detection.rb', line 34

def filter_out_common_subfolders(folders)
  matcher = /\/(#{FileMatcher.common_subfolders.keys.join("|")})$/i
  folders.map { |f| f.gsub(matcher, "") }.uniq
end

#folders_with_changes ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/concerns/change_detection.rb', line 13

def folders_with_changes
  # Make a list of changed filenames using set XOR
  changes = (indexed_files.to_set ^ indexable_files).to_a
  # Double-check that we only consider indexable files in the changelist
  # This is because some non-indexable files might have been added by uploading
  # (that might be a bug - or a feature)
  changes.select! { |f| MediaType.indexable_extensions.include? File.extname(f).tr(".", "") }
  # Discard thingiverse false-positives - sometimes they add images that have model extensions.
  patterns = MediaType.model_extensions.map { %r{images/[^/]*\.#{it}} }
  changes = changes.reject { |f| patterns.any? { f =~ it } }
  # Make a list of library-relative folders with changed files
  folders_with_changes = changes.map { |f| File.dirname(f) }.uniq
  folders_with_changes = filter_out_common_subfolders(folders_with_changes)
  # Ignore root folder, however specified
  folders_with_changes.delete("/")
  folders_with_changes.delete(".")
  folders_with_changes.delete("./")
  # Trim out anything left blank and we're done
  folders_with_changes.compact_blank
end

#indexable_files ⇒ Object

Get a list of all the files in the library storage that could be indexed



5
6
7
# File 'app/models/concerns/change_detection.rb', line 5

def indexable_files
  list_files(File.join("**", FileMatcher.file_pattern))
end

#indexed_files ⇒ Object



9
10
11
# File 'app/models/concerns/change_detection.rb', line 9

def indexed_files
  model_files.includes(:model).without_special.pluck("models.path", :filename).map { File.join(it) }
end

#sample(num = 3) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/concerns/change_detection.rb', line 39

def sample(num = 3)
  remaining_attempts = num * 5 # Got to have some kind of cutoff
  rng = Random.new(path.unpack1("S")) # See a random number generator with the path so that the random selections are stable for each library
  results = []
  while results.length < num && remaining_attempts > 0
    leaf = dive_for_leaf_folder(path, nil, rng)
    results << leaf if leaf
    remaining_attempts -= 1
    results = results.compact.sort.uniq
  end
  results
end