diff --git a/app/models/print_host.rb b/app/models/print_host.rb index 52333436f..c772d7ec1 100644 --- a/app/models/print_host.rb +++ b/app/models/print_host.rb @@ -1,11 +1,17 @@ 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"), 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} + + def service + PROTOCOLS[protocol].new(print_host: self) + end + + def input_types + PROTOCOLS[protocol]::INPUT_TYPES + end 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] } %>