mirror of
https://github.com/discourse/discourse.git
synced 2024-12-19 23:06:00 +08:00
01e36cbb47
A previous refactor has prevented errors to show correctly. The guilt of the issue is that we were not calling the error variable correctly in the templates. This commit also adds a spec for this case, and removes the need for `I18n.backend.store_translations` in specs so we don't have to write too much boilerplate each time we write a spec.
129 lines
3.8 KiB
Ruby
129 lines
3.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseAutomation
|
|
class AutomationSerializer < ApplicationSerializer
|
|
attributes :id
|
|
attributes :name
|
|
attributes :enabled
|
|
attributes :script
|
|
attributes :trigger
|
|
attributes :updated_at
|
|
attributes :last_updated_by
|
|
attributes :next_pending_automation_at
|
|
attributes :placeholders
|
|
|
|
def last_updated_by
|
|
BasicUserSerializer.new(
|
|
User.find_by(id: object.last_updated_by_id) || Discourse.system_user,
|
|
root: false,
|
|
).as_json
|
|
end
|
|
|
|
def include_next_pending_automation_at?
|
|
object.pending_automations.exists?
|
|
end
|
|
|
|
def next_pending_automation_at
|
|
object&.pending_automations&.first&.execute_at
|
|
end
|
|
|
|
def placeholders
|
|
scriptable_placeholders =
|
|
DiscourseAutomation
|
|
.filter_by_trigger(scriptable&.placeholders || [], object.trigger)
|
|
.map { |placeholder| placeholder[:name] }
|
|
triggerable_placeholders = triggerable&.placeholders || []
|
|
|
|
(scriptable_placeholders + triggerable_placeholders).map do |placeholder|
|
|
placeholder.to_s.gsub(/\s+/, "_").underscore
|
|
end
|
|
end
|
|
|
|
def script
|
|
key = "discourse_automation.scriptables"
|
|
doc_key = "#{key}.#{object.script}.doc"
|
|
script_with_trigger_key = "#{key}.#{object.script}_with_#{object.trigger}.doc"
|
|
|
|
{
|
|
id: object.script,
|
|
version: scriptable.version,
|
|
name:
|
|
I18n.t(
|
|
"#{key}.#{object.script}.title",
|
|
default: "Missing translation for #{key}.#{object.script}.title",
|
|
),
|
|
description: I18n.t("#{key}.#{object.script}.description", default: ""),
|
|
doc: I18n.exists?(doc_key, :en) ? I18n.t(doc_key) : nil,
|
|
with_trigger_doc:
|
|
I18n.exists?(script_with_trigger_key, :en) ? I18n.t(script_with_trigger_key) : nil,
|
|
forced_triggerable: scriptable.forced_triggerable,
|
|
not_found: scriptable.not_found,
|
|
templates:
|
|
process_templates(filter_fields_with_priority(scriptable.fields, object.trigger&.to_sym)),
|
|
fields: process_fields(object.fields.where(target: "script")),
|
|
}
|
|
end
|
|
|
|
def trigger
|
|
key = "discourse_automation.triggerables"
|
|
doc_key = "#{key}.#{object.trigger}.doc"
|
|
|
|
{
|
|
id: object.trigger,
|
|
name:
|
|
I18n.t(
|
|
"#{key}.#{object.trigger}.title",
|
|
default: "Missing translation for #{key}.#{object.trigger}.title",
|
|
),
|
|
description: I18n.t("#{key}.#{object.trigger}.description", default: ""),
|
|
doc: I18n.exists?(doc_key, :en) ? I18n.t(doc_key) : nil,
|
|
not_found: triggerable&.not_found,
|
|
templates: process_templates(triggerable&.fields || []),
|
|
fields: process_fields(object.fields.where(target: "trigger")),
|
|
settings: triggerable&.settings,
|
|
}
|
|
end
|
|
|
|
private
|
|
|
|
def filter_fields_with_priority(arr, trigger)
|
|
unique_with_priority = {}
|
|
|
|
arr.each do |item|
|
|
name = item[:name]
|
|
if (item[:triggerable]&.to_sym == trigger&.to_sym || item[:triggerable].nil?) &&
|
|
(!unique_with_priority.key?(name) || unique_with_priority[name][:triggerable].nil?)
|
|
unique_with_priority[name] = item
|
|
end
|
|
end
|
|
|
|
unique_with_priority.values
|
|
end
|
|
|
|
def process_templates(fields)
|
|
ActiveModel::ArraySerializer.new(
|
|
fields,
|
|
each_serializer: DiscourseAutomation::TemplateSerializer,
|
|
scope: {
|
|
automation: object,
|
|
},
|
|
).as_json
|
|
end
|
|
|
|
def process_fields(fields)
|
|
ActiveModel::ArraySerializer.new(
|
|
fields || [],
|
|
each_serializer: DiscourseAutomation::FieldSerializer,
|
|
).as_json || []
|
|
end
|
|
|
|
def scriptable
|
|
object.scriptable
|
|
end
|
|
|
|
def triggerable
|
|
object.triggerable
|
|
end
|
|
end
|
|
end
|