Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions app/models/print_host.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/views/print_hosts/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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] } %>
<hr>
<div>
<%= form.submit class: "btn btn-primary" %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/print_hosts/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ 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 }
th
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}) }
Expand Down
8 changes: 8 additions & 0 deletions spec/models/print_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading