Commit 7549d17f authored by Michael Kozono's avatar Michael Kozono

Refactor

parent 67b58ffd
......@@ -51,28 +51,25 @@ module Gitlab
].freeze
def ensure_tracked!
add_to_uploads unless in_uploads?
add_to_uploads_if_needed
delete
end
def in_uploads?
def add_to_uploads_if_needed
# Even though we are checking relative paths, path is enough to
# uniquely identify uploads. There is no ambiguity between
# FileUploader paths and other Uploader paths because we use the /-/
# separator kind of like an escape character. Project full_path will
# never conflict with an upload path starting with "uploads/-/".
Upload.exists?(path: upload_path)
end
def add_to_uploads
Upload.create!(
path: upload_path,
uploader: uploader,
model_type: model_type,
model_id: model_id,
size: file_size
)
Upload.
where(path: upload_path).
first_or_create!(
uploader: uploader,
model_type: model_type,
model_id: model_id,
size: file_size
)
end
def upload_path
......
......@@ -166,7 +166,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UntrackedFile do
end
end
describe '#add_to_uploads' do
describe '#add_to_uploads_if_needed' do
shared_examples_for 'add_to_uploads_non_markdown_files' do
let!(:expected_upload_attrs) { model.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum') }
let!(:untracked_file) { described_class.create!(path: expected_upload_attrs['path']) }
......@@ -177,7 +177,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UntrackedFile do
it 'creates an Upload record' do
expect do
untracked_file.add_to_uploads
untracked_file.add_to_uploads_if_needed
end.to change { model.reload.uploads.count }.from(0).to(1)
expect(model.uploads.first.attributes).to include(expected_upload_attrs)
......@@ -246,7 +246,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads::UntrackedFile do
it 'creates an Upload record' do
expect do
untracked_file.add_to_uploads
untracked_file.add_to_uploads_if_needed
end.to change { model.reload.uploads.count }.from(0).to(1)
expect(model.uploads.first.attributes).to include(@expected_upload_attrs)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment