From 2696adfbb16075e73691e98d901ca4b2b199191a Mon Sep 17 00:00:00 2001 From: James Smith Date: Wed, 17 Jun 2026 00:07:53 +0100 Subject: [PATCH 1/5] get protocol string from print services --- app/models/print_host.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/models/print_host.rb b/app/models/print_host.rb index 52333436f..14c3f0701 100644 --- a/app/models/print_host.rb +++ b/app/models/print_host.rb @@ -1,9 +1,7 @@ class PrintHost < ApplicationRecord # i18n-tasks-use t("activerecord.models.print_host") - PROTOCOLS = [ - "moonraker" # i18n-tasks-use t("print_hosts.protocols.moonraker") - ].freeze + PROTOCOLS = Print.constants.map { const_get("Print::#{it}::PROTOCOL") }.freeze validates :name, presence: true validates :endpoint, presence: true From d625a4cf83049440f72c594478a6e5cd5e42e1f5 Mon Sep 17 00:00:00 2001 From: James Smith Date: Wed, 17 Jun 2026 00:25:33 +0100 Subject: [PATCH 2/5] change protocol list to hash lookup --- app/models/print_host.rb | 4 ++-- app/views/print_hosts/_form.html.erb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/print_host.rb b/app/models/print_host.rb index 14c3f0701..39fb618ef 100644 --- a/app/models/print_host.rb +++ b/app/models/print_host.rb @@ -1,9 +1,9 @@ class PrintHost < ApplicationRecord # i18n-tasks-use t("activerecord.models.print_host") - PROTOCOLS = Print.constants.map { const_get("Print::#{it}::PROTOCOL") }.freeze + PROTOCOLS = Print.constants.map { [const_get("Print::#{it}::PROTOCOL"), const_get("Print::#{it}")] }.to_h.freeze validates :name, presence: true validates :endpoint, presence: true - validates :protocol, presence: true, inclusion: {in: PROTOCOLS} + validates :protocol, presence: true, inclusion: {in: PROTOCOLS.keys} end diff --git a/app/views/print_hosts/_form.html.erb b/app/views/print_hosts/_form.html.erb index c2b377861..9d02bf1dd 100644 --- a/app/views/print_hosts/_form.html.erb +++ b/app/views/print_hosts/_form.html.erb @@ -3,7 +3,7 @@ <%= TextInputRow(form: form, attribute: :name, label: PrintHost.human_attribute_name(:name), help: t(".name.help")) %> <%= TextInputRow(form: form, attribute: :endpoint, label: PrintHost.human_attribute_name(:endpoint), help: t(".endpoint.help")) %> <%= SelectInputRow form: form, attribute: :protocol, label: PrintHost.human_attribute_name(:protocol), help: t(".protocol.help"), - select_options: Naturally.sort(PrintHost::PROTOCOLS).map { [translate("print_hosts.protocols.%{protocol}" % {protocol: it}), it] } %> + select_options: Naturally.sort(PrintHost::PROTOCOLS.keys).map { [translate("print_hosts.protocols.%{protocol}" % {protocol: it}), it] } %>
<%= form.submit class: "btn btn-primary" %> From 4f599a576344010a592ae8455f867691228d70b1 Mon Sep 17 00:00:00 2001 From: James Smith Date: Wed, 17 Jun 2026 00:25:50 +0100 Subject: [PATCH 3/5] Add service factory method to print host --- app/models/print_host.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/print_host.rb b/app/models/print_host.rb index 39fb618ef..81d14886a 100644 --- a/app/models/print_host.rb +++ b/app/models/print_host.rb @@ -6,4 +6,8 @@ class PrintHost < ApplicationRecord validates :name, presence: true validates :endpoint, presence: true validates :protocol, presence: true, inclusion: {in: PROTOCOLS.keys} + + def service + PROTOCOLS[protocol].new(print_host: self) + end end From d1b2b64fe6398b9af40c0399d0967939b9714d88 Mon Sep 17 00:00:00 2001 From: James Smith Date: Wed, 17 Jun 2026 00:26:20 +0100 Subject: [PATCH 4/5] add ok? indicator for each print host --- app/views/print_hosts/index.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/print_hosts/index.rb b/app/views/print_hosts/index.rb index b79b12433..27bf9bf99 100644 --- a/app/views/print_hosts/index.rb +++ b/app/views/print_hosts/index.rb @@ -10,6 +10,7 @@ def view_template p { t("views.print_hosts.index.description") } table class: "table table-striped" do tr do + th th { PrintHost.human_attribute_name :name } th { PrintHost.human_attribute_name :endpoint } th { PrintHost.human_attribute_name :protocol } @@ -17,6 +18,7 @@ def view_template end @print_hosts.each do |print_host| tr do + td { print_host.service.ok? ? "✅" : "❌" } td { print_host.name } td { code { print_host.endpoint } } td { translate("print_hosts.protocols.%{protocol}" % {protocol: print_host.protocol}) } From 14930eda7923278aee1d5e0e666a838bcaf21421 Mon Sep 17 00:00:00 2001 From: James Smith Date: Wed, 17 Jun 2026 11:27:40 +0100 Subject: [PATCH 5/5] get input types from print services --- app/models/print_host.rb | 4 ++++ spec/models/print_host_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/app/models/print_host.rb b/app/models/print_host.rb index 81d14886a..c772d7ec1 100644 --- a/app/models/print_host.rb +++ b/app/models/print_host.rb @@ -10,4 +10,8 @@ class PrintHost < ApplicationRecord def service PROTOCOLS[protocol].new(print_host: self) end + + def input_types + PROTOCOLS[protocol]::INPUT_TYPES + end end diff --git a/spec/models/print_host_spec.rb b/spec/models/print_host_spec.rb index 19f287273..23699f830 100644 --- a/spec/models/print_host_spec.rb +++ b/spec/models/print_host_spec.rb @@ -24,4 +24,12 @@ expect(described_class.create(attributes.merge(protocol: :nope))).not_to be_valid end end + + context "with a valid print host" do + let(:print_host) { create(:print_host) } + + it "can list input types" do + expect(print_host.input_types.map(&:to_s)).to eq ["text/x-gcode"] + end + end end