Refactored action response refinement to default to 200 status

Necessary to reduce unnecessary typing and setup by defaulting to 200 status (this mimics Hanami's own response status which is 200 if you don't supply a different value). Now, only when the status isn't 200, do you need to supply a code.

Milestone: patch
This commit is contained in:
Brooke Kuhlmann
2025-08-07 14:31:28 -06:00
parent 06ad168621
commit ec498a5afb
11 changed files with 11 additions and 25 deletions
+2 -2
View File
@@ -42,7 +42,7 @@ module Terminus
end
def success device, image_attributes, response
response.with body: build_payload(device, image_attributes).to_json, status: 200
response.body = build_payload(device, image_attributes).to_json
end
def error_for device, message, response
@@ -80,7 +80,7 @@ module Terminus
**device.as_api_display
]
response.with body: payload.to_json, status: 200
response.body = payload.to_json
end
def not_found response
+1 -1
View File
@@ -46,7 +46,7 @@ module Terminus
def find_model_id = model_repository.find_by(name: "og_png").then { it.id if it }
def render_success device, response
response.with body: payload.for(device).to_json, status: 200
response.body = payload.for(device).to_json
end
def not_found error, response
-2
View File
@@ -8,8 +8,6 @@ module Terminus
class Delete < Terminus::Action
include Deps[repository: "repositories.firmware"]
using Refines::Actions::Response
def handle _request, response
repository.delete_all
response.render view, layout: false
+1 -3
View File
@@ -7,8 +7,6 @@ module Terminus
class Delete < Terminus::Action
include Deps[repository: "repositories.device"]
using Refines::Actions::Response
params { required(:id).filled :integer }
def handle request, response
@@ -17,7 +15,7 @@ module Terminus
halt :unprocessable_entity unless parameters.valid?
repository.delete parameters[:id]
response.with body: "", status: 200
response.body = ""
end
end
end
+1 -3
View File
@@ -8,8 +8,6 @@ module Terminus
class Delete < Terminus::Action
include Deps[repository: "repositories.device_log"]
using Refines::Actions::Response
params do
required(:device_id).filled :integer
required(:id).filled :integer
@@ -21,7 +19,7 @@ module Terminus
halt :unprocessable_entity unless parameters.valid?
repository.delete_by_device(*parameters.to_h.values_at(:device_id, :id))
response.with body: "", status: 200
response.body = ""
end
end
end
+1 -3
View File
@@ -7,8 +7,6 @@ module Terminus
class Delete < Terminus::Action
include Deps[repository: "repositories.firmware"]
using Refines::Actions::Response
params { required(:id).filled :integer }
def handle request, response
@@ -17,7 +15,7 @@ module Terminus
halt :unprocessable_entity unless parameters.valid?
repository.delete parameters[:id]
response.with body: "", status: 200
response.body = ""
end
end
end
+1 -3
View File
@@ -7,8 +7,6 @@ module Terminus
class Delete < Terminus::Action
include Deps[repository: "repositories.playlist"]
using Refines::Actions::Response
params { required(:id).filled :integer }
def handle request, response
@@ -17,7 +15,7 @@ module Terminus
halt :unprocessable_entity unless parameters.valid?
repository.delete parameters[:id]
response.with body: "", status: 200
response.body = ""
end
end
end
+1 -3
View File
@@ -8,8 +8,6 @@ module Terminus
class Delete < Terminus::Action
include Deps[repository: "repositories.playlist_item"]
using Refines::Actions::Response
params do
required(:playlist_id).filled :integer
required(:id).filled :integer
@@ -22,7 +20,7 @@ module Terminus
item = repository.find_by(**parameters)
repository.delete item.id
response.with body: "", status: 200
response.body = ""
end
end
end
+1 -3
View File
@@ -7,8 +7,6 @@ module Terminus
class Delete < Terminus::Action
include Deps[repository: "repositories.screen"]
using Refines::Actions::Response
params { required(:id).filled :integer }
def handle request, response
@@ -17,7 +15,7 @@ module Terminus
halt :unprocessable_entity unless parameters.valid?
repository.delete parameters[:id]
response.with body: "", status: 200
response.body = ""
end
end
end
+1 -1
View File
@@ -6,7 +6,7 @@ module Terminus
# Modifies and enhances default Hanami action response behavior.
module Response
refine Hanami::Action::Response do
def with body:, status:, format: nil
def with body:, format: nil, status: 200
@body = [body]
@status = status
@@ -16,7 +16,7 @@ RSpec.describe Terminus::Refines::Actions::Response do
describe "#with" do
it "answers response with required body and status" do
expect(response.with(body: "A test.", status: 200)).to have_attributes(
expect(response.with(body: "A test.")).to have_attributes(
body: ["A test."],
format: nil,
status: 200