discourse/app/services/problem_check/problem.rb
Ted Johansson b4085c7b50
DEV: Support target in problem check problem constructor (#31922)
We didn't have support in the #problem constructor for multiple targets, forcing developers to manually construct a Problem instance. This involves a lot of details and is error prone.

This PR supports passing an optional target argument to the constructor. This will be passed on to the translation_data method to generate the correct message as well.
2025-03-24 10:10:41 +08:00

32 lines
742 B
Ruby

# frozen_string_literal: true
class ProblemCheck::Problem
PRIORITIES = %w[low high].freeze
attr_reader :message, :priority, :identifier, :target, :details
def initialize(message, priority: "low", identifier: nil, target: nil, details: {})
@message = message
@priority = PRIORITIES.include?(priority) ? priority : "low"
@identifier = identifier
@target = target
@details = details
end
def to_s
@message
end
def to_h
{ message:, priority:, identifier:, target: }
end
alias_method :attributes, :to_h
def self.from_h(h)
h = h.with_indifferent_access
return if h[:message].blank?
new(h[:message], priority: h[:priority], identifier: h[:identifier], target: h[:target])
end
end