Skip to content

Commit

Permalink
don't duplicate previously-merged models during rescan
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Jun 27, 2022
1 parent 1b0b789 commit b991ba7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
27 changes: 19 additions & 8 deletions app/jobs/model_scan_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,40 @@ def self.file_pattern
"*.{#{lower.zip(upper).flatten.join(",")}}"
end

def model_file_paths(library)
library.model_files.reload.map do |x|
File.join(library.path, x.model.path, x.filename)
end
end

def perform(model)
# For each file in the model, create a file object
model_path = File.join(model.library.path, model.path)
all_file_paths = model_file_paths(model.library)
Dir.open(model_path) do |dir|
Dir.glob([
File.join(dir.path, ModelScanJob.file_pattern),
File.join(dir.path, "files", ModelScanJob.file_pattern)
]).each do |filename|
# Create the file
file = model.model_files.find_or_create_by(filename: filename.gsub(model_path + "/", ""))
# Try to guess if the file is presupported
if !(
File.join(model_path, filename).split(/[[:punct:]]|[[:space:]]/).map(&:downcase) &
["presupported", "supported", "sup", "wsupports", "withsupports"]
).empty?
file.update!(presupported: true)
unless all_file_paths.include?(filename)
# Create the file
file = model.model_files.find_or_create_by(filename: filename.gsub(model_path + "/", ""))
# Try to guess if the file is presupported
if !(
File.join(model_path, filename).split(/[[:punct:]]|[[:space:]]/).map(&:downcase) &
["presupported", "supported", "sup", "wsupports", "withsupports"]
).empty?
file.update!(presupported: true)
end
end
end
end
# Clean out missing files
model.model_files.select { |f|
!File.exist?(File.join(model_path, f.filename))
}.each(&:destroy)
# If this model has no files, self destruct
model.destroy if model.model_files.reload.count == 0
# Set tags and default files
model.model_files.reload
model.preview_file = model.model_files.first unless model.preview_file
Expand Down
1 change: 1 addition & 0 deletions app/models/library.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Library < ApplicationRecord
has_many :models, dependent: :destroy
has_many :model_files, through: :models
validates :path, presence: true, uniqueness: true, existing_path: true

default_scope { order(:path) }
Expand Down
27 changes: 27 additions & 0 deletions spec/jobs/model_scan_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,32 @@
expect { ModelScanJob.perform_now(thing) }.to change { thing.model_files.count }.from(2).to(1)
expect(thing.model_files.first.filename).to eq "files/part_one.stl"
end

it "does not recreate parts that have been merged into other models" do
# Set up one model nested inside another
model_one = create(:model, path: "model_one", library: library)
ModelScanJob.perform_now(model_one)
nested_model = create(:model, path: "model_one/nested_model", library: library)
ModelScanJob.perform_now(nested_model)
# Check initial conditions
expect(Model.count).to eq 2
expect(ModelFile.count).to eq 3
expect(model_one.model_files.count).to eq 2
expect(nested_model.model_files.count).to eq 1
# Merge the models
nested_model.merge_into_parent!
# Check that worked
expect(Model.count).to eq 1
expect(ModelFile.count).to eq 3
expect(model_one.model_files.count).to eq 3
# Now recreate the nested model and rescan
nested_model = create(:model, path: "model_one/nested_model", library: library)
ModelScanJob.perform_now(nested_model)
# That should not have changed anything apart from creating an empty model which will be cleaned up later
expect(Model.count).to eq 1
expect(ModelFile.count).to eq 3
expect(model_one.model_files.count).to eq 3
expect(nested_model.model_files.count).to eq 0
end
end
end

0 comments on commit b991ba7

Please sign in to comment.