Skip to content
Closed
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
3 changes: 2 additions & 1 deletion app/admin/support/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
collection: -> { ticket_admins_collection(collection) },
label: -> { Support::Ticket.human_attribute_name(:admin) }
filter :priority, as: :select,
collection: -> { ticket_priority_marks_collection }
collection: -> { ticket_priority_filter_collection }
filter :last_activity_at, as: :date_range

includes :admin, :last_message
Expand Down Expand Up @@ -89,6 +89,7 @@
f.input :priority,
collection: ticket_priority_marks_collection,
include_blank: false,
required: false,
label: false,
wrapper_html: { class: "support-ticket-priority" },
input_html: {
Expand Down
12 changes: 9 additions & 3 deletions app/helpers/support_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ def ticket_admins_collection(relation = nil)

def ticket_priority_marks_collection
[
[ "", Support::Ticket.priorities[:normal] ],
[ Support::Ticket::PRIORITY_ICONS[:medium], Support::Ticket.priorities[:medium] ],
[ Support::Ticket::PRIORITY_ICONS[:high], Support::Ticket.priorities[:high] ]
[ "", :normal ],
[ Support::Ticket::PRIORITY_ICONS[:medium], :medium ],
[ Support::Ticket::PRIORITY_ICONS[:high], :high ]
]
end

def ticket_priority_filter_collection
ticket_priority_marks_collection.map { |label, key|
[ label, Support::Ticket.priorities[key] ]
}
end

def support_message_html(message)
html = if message.html.present?
message.html.to_s
Expand Down
2 changes: 1 addition & 1 deletion app/models/support/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Ticket < ApplicationRecord
include HasAttachments
has_rich_text :html

enum :priority, %i[normal medium high]
enum :priority, %i[normal medium high], validate: true

belongs_to :admin, optional: true
has_many :messages, class_name: "Support::Message",
Expand Down
40 changes: 37 additions & 3 deletions test/controllers/support_tickets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ def login(admin)
assert_select "label[for=support_ticket_priority]", count: 0
assert_select "#support_ticket_subject_input .inline-hints", count: 0
assert_select "select[name='support_ticket[priority]']" do
assert_select "option[value=?]", Support::Ticket.priorities[:normal], text: ""
assert_select "option[value=?]", Support::Ticket.priorities[:medium], text: Support::Ticket::PRIORITY_ICONS[:medium]
assert_select "option[value=?]", Support::Ticket.priorities[:high], text: Support::Ticket::PRIORITY_ICONS[:high]
assert_select "option[value=?]", "normal", text: ""
assert_select "option[value=?]", "medium", text: Support::Ticket::PRIORITY_ICONS[:medium]
assert_select "option[value=?]", "high", text: Support::Ticket::PRIORITY_ICONS[:high]
end

post support_tickets_path, params: {
Expand Down Expand Up @@ -145,6 +145,40 @@ def login(admin)
assert_equal "Checkout fails", ticket.messages.first.body
end

test "create accepts high priority from the form enum name" do
login admins(:external)

post support_tickets_path, params: {
support_ticket: {
priority: "high",
subject: "Broken shop",
html: "<div>Checkout fails</div>"
}
}

ticket = Support::Ticket.order(:id).last
assert_redirected_to support_ticket_path(ticket)
assert_equal "high", ticket.priority
end

test "create with a numeric priority string re-renders instead of raising" do
login admins(:external)

assert_no_difference "Support::Ticket.count" do
post support_tickets_path, params: {
support_ticket: {
priority: "2",
subject: "Broken shop",
html: "<div>Checkout fails</div>"
}
}
end

assert_response :unprocessable_entity
assert_select "#support_ticket_priority_input.error"
assert_includes response.body, I18n.t("errors.messages.inclusion")
end

test "show includes the thread and reply box" do
ticket = Support::Ticket.create!(
priority: :normal, subject: "Need help", content: "Opening", admin: admins(:external))
Expand Down
22 changes: 22 additions & 0 deletions test/models/support/ticket_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
require "test_helper"

class Support::TicketTest < ActiveSupport::TestCase
test "invalid priority is a validation error instead of ArgumentError" do
ticket = Support::Ticket.new(
priority: "2",
subject: "Test",
content: "Test",
admin: admins(:external))

assert_not ticket.valid?
assert_includes ticket.errors[:priority], I18n.t("errors.messages.inclusion")
end

test "priority accepts enum names" do
ticket = Support::Ticket.new(
subject: "Test",
content: "Test",
admin: admins(:external))

ticket.priority = "high"
assert ticket.high?
assert ticket.valid?
end

test "subject_decorated" do
ticket = Support::Ticket.new(
priority: :normal,
Expand Down
Loading