Removed screen and firmware struct attachment replace behavior

We can safely remove the `#remove` method in favor of using `#upload` for everything now that we conditionally destroy the record only if found and the `*_data` key exists. This also updates firmware attachment behavior to mimic screen attachment behavior in order to improve consistency.

Milestone: minor
This commit is contained in:
Brooke Kuhlmann
2026-08-03 16:47:46 -06:00
parent 3c7238325f
commit 5e9ab0cf02
9 changed files with 40 additions and 123 deletions
+1 -1
View File
@@ -54,7 +54,7 @@ module Terminus
# :reek:FeatureEnvy
def replace record, content, response
Pathname.mktmpdir do |root|
root.join("#{record.version}.bin").write(content).open { record.replace it }
root.join("#{record.version}.bin").write(content).open { record.upload it }
end
update = repository.update record.id, attachment_data: record.attachment_attributes
+1 -1
View File
@@ -47,7 +47,7 @@ module Terminus
def attach record, attachment
return unless attachment
record.replace attachment[:tempfile], metadata: {"filename" => "#{record.version}.bin"}
record.upload attachment[:tempfile], metadata: {"filename" => "#{record.version}.bin"}
repository.update record.id, attachment_data: record.attachment_attributes
end
+1 -1
View File
@@ -42,7 +42,7 @@ module Terminus
tempfile = image[:tempfile]
extension = File.extname tempfile
record.replace tempfile, metadata: {"filename" => "#{record.name}#{extension}"}
record.upload tempfile, metadata: {"filename" => "#{record.name}#{extension}"}
repository.update record.id, image_data: record.image_attributes
end
-1
View File
@@ -16,7 +16,6 @@ module Terminus
def delete id
find(id).then { it.attachment_destroy if it }
firmware.by_pk(id).delete
end
+3 -7
View File
@@ -25,7 +25,7 @@ module Terminus
def attachment_destroy
store.delete attachment_id if attachment_id
attributes[:attachment_data].clear
attributes[:attachment_data].clear if attributes.key? :attachment_data
end
def attachment_id = attachment_attributes[:id]
@@ -49,14 +49,10 @@ module Terminus
attacher.assign(io, **).tap { |file| attributes[:attachment_data] = file.data }
end
def replace(io, **)
attachment_destroy
upload(io, **)
self
end
def upload(io, **)
attachment_destroy
attacher.upload(io, **).tap { |file| attributes[:attachment_data] = file.data }
self
end
def errors = attacher.errors
+2 -7
View File
@@ -30,7 +30,7 @@ module Terminus
def image_destroy
store.delete image_id if image_id
attributes[:image_data].clear
attributes[:image_data].clear if attributes.key? :image_data
end
def image_id = image_attributes[:id]
@@ -61,13 +61,8 @@ module Terminus
def mime_type = image_attributes.dig :metadata, :mime_type
def replace(io, **)
image_destroy
upload(io, **)
self
end
def upload(io, **)
image_destroy
attacher.upload(io, **).tap { |file| attributes[:image_data] = file.data }
self
end
+2 -2
View File
@@ -28,7 +28,7 @@ RSpec.describe Terminus::Repositories::Firmware, :db do
it "deletes associated attachment" do
upload = firmware.upload StringIO.new([123].pack("N"))
repository.update firmware.id, attachment_data: upload.data
repository.update firmware.id, attachment_data: upload.attachment_attributes
repository.delete firmware.id
expect(Hanami.app[:shrine].storages[:store].store).to eq({})
@@ -50,7 +50,7 @@ RSpec.describe Terminus::Repositories::Firmware, :db do
it "deletes all attachments" do
upload = firmware.upload StringIO.new([123].pack("N"))
repository.update firmware.id, attachment_data: upload.data
repository.update firmware.id, attachment_data: upload.attachment_attributes
repository.delete_all
expect(Hanami.app[:shrine].storages[:store].store).to eq({})
+23 -41
View File
@@ -123,50 +123,25 @@ RSpec.describe Terminus::Structs::Firmware, :db do
end
end
describe "#replace" do
it "replaces file" do
instance = path.open { |io| firmware.replace io }
describe "#upload" do
it "uploads file when valid" do
path = temp_dir.join "test.bin"
path.binwrite [123].pack("N")
instance = path.open { |io| firmware.upload io }
expect(instance.attachment_attributes).to match(
id: /\h{32}\.bin/,
metadata: {
filename: "test.bin",
height: nil,
size: 4,
mime_type: "application/octet-stream",
width: nil,
height: nil
width: nil
},
storage: "store"
)
end
it "updates storage ID" do
id = firmware.attachment_id
instance = path.open { |io| firmware.replace io }
expect(id).not_to eq(instance.attachment_id)
end
end
describe "#upload" do
it "uploads file when valid" do
path = temp_dir.join "test.bin"
path.binwrite [123].pack("N")
upload = firmware.upload path.open
expect(upload.data).to match(
"id" => /\h{32}\.bin/,
"metadata" => {
"filename" => "test.bin",
"height" => nil,
"size" => 4,
"mime_type" => "application/octet-stream",
"width" => nil
},
"storage" => "store"
)
end
it "updates attributes when valid" do
path = temp_dir.join "test.bin"
path.binwrite [123].pack("N")
@@ -185,19 +160,26 @@ RSpec.describe Terminus::Structs::Firmware, :db do
)
end
it "updates storage ID" do
id = firmware.attachment_id
instance = path.open { |io| firmware.upload io }
expect(id).not_to eq(instance.attachment_id)
end
it "doesn't upload file when invalid" do
upload = firmware.upload StringIO.new
expect(upload.data).to match(
"id" => /\h{32}/,
"metadata" => {
"filename" => nil,
"height" => nil,
"size" => 0,
"mime_type" => nil,
"width" => nil
expect(upload.attachment_attributes).to match(
id: /\h{32}/,
metadata: {
filename: nil,
height: nil,
size: 0,
mime_type: nil,
width: nil
},
"storage" => "store"
storage: "store"
)
end
end
+7 -62
View File
@@ -147,68 +147,6 @@ RSpec.describe Terminus::Structs::Screen, :db do
end
end
describe "#replace" do
it "replaces file when valid" do
instance = path.open { |io| screen.replace io }
expect(instance.image_attributes).to match(
id: /\h{32}\.png/,
metadata: {
bit_depth: 1,
checksum: match_md5_checksum,
filename: "test.png",
height: 1,
size: 81,
mime_type: "image/png",
width: 1
},
storage: "store"
)
end
it "updates attributes when valid" do
instance = path.open { |io| screen.replace io }
expect(instance.image_attributes).to match(
id: /\h{32}\.png/,
storage: "store",
metadata: {
bit_depth: 1,
checksum: match_md5_checksum,
filename: "test.png",
size: 81,
mime_type: "image/png",
width: 1,
height: 1
}
)
end
it "updates storage ID" do
id = screen.image_id
instance = path.open { |io| screen.replace io }
expect(id).not_to eq(instance.image_id)
end
it "doesn't replace file when invalid" do
instance = screen.replace StringIO.new
expect(instance.image_attributes).to match(
id: /\h{32}/,
metadata: {
bit_depth: nil,
checksum: match_md5_checksum,
filename: nil,
height: nil,
size: 0,
mime_type: nil,
width: nil
},
storage: "store"
)
end
end
describe "#upload" do
it "uploads file when valid" do
instance = path.open { |io| screen.upload io }
@@ -246,6 +184,13 @@ RSpec.describe Terminus::Structs::Screen, :db do
)
end
it "updates storage ID" do
id = screen.image_id
instance = path.open { |io| screen.upload io }
expect(id).not_to eq(instance.image_id)
end
it "doesn't upload file when invalid" do
upload = screen.upload StringIO.new