Skip to content

Commit

Permalink
Merge pull request #1335 from alphagov/remove-saved-guard
Browse files Browse the repository at this point in the history
Ensure asset metadata is updated when file name isn't changed
  • Loading branch information
jkempster34 authored Mar 18, 2024
2 parents 6c583bc + b4edc19 commit 79c5005
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Asset

mount_uploader :file, AssetUploader

before_save :store_metadata, unless: :uploaded?, if: -> { changes.include?(:file) }
before_save :store_metadata, unless: :uploaded?
after_save :schedule_virus_scan
after_save :update_indirect_replacements_on_publish
after_save :backpropagate_replacement
Expand Down
13 changes: 13 additions & 0 deletions spec/models/asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,19 @@
expect(asset.save).to be_truthy
end
end

context "when the file name is unchanged" do
let(:saved_asset) { FactoryBot.create(:asset) }
let(:md5_hexdigest) { saved_asset.md5_hexdigest }
let(:original_filename) { saved_asset.file.send(:original_filename) }

it "updates the md5 hexdigest" do
asset = described_class.find(saved_asset.id) # find to clear memoization
asset.update!(file: load_fixture_file("lorem.txt", named: original_filename))

expect(asset.md5_hexdigest).not_to eq md5_hexdigest
end
end
end

describe "#replacement" do
Expand Down

0 comments on commit 79c5005

Please sign in to comment.