discourse/plugins/automation/lib/discourse_automation/triggers/topic.rb
Osama Sayegh 3be4924b99
DEV: Move array type custom fields to JSON type in automation (#26939)
The automation plugin has 4 custom field types that are array typed. However, array typed custom fields are deprecated and should be migrated to JSON type.

This commit does a couple of things:

1. Migrate all four custom fields to JSON
2. Fix a couple of small bugs that have been discovered while migrating the custom fields to JSON (see the comments on this commit's PR for details https://github.com/discourse/discourse/pull/26939)
2024-05-10 18:47:12 +03:00

32 lines
924 B
Ruby

# frozen_string_literal: true
DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::TOPIC) do
field :restricted_topic, component: :text
on_update do |automation, metadata, previous_metadata|
ActiveRecord::Base.transaction do
previous_topic_id = previous_metadata.dig("restricted_topic", "value")
topic_id = metadata.dig("restricted_topic", "value")
if previous_topic_id && previous_topic_id != topic_id
previous_topic = Topic.find_by(id: previous_topic_id)
if previous_topic
automation.remove_id_from_custom_field(
previous_topic,
DiscourseAutomation::AUTOMATION_IDS_CUSTOM_FIELD,
)
end
end
if topic_id
topic = Topic.find_by(id: topic_id)
next if !topic
automation.add_id_to_custom_field(topic, DiscourseAutomation::AUTOMATION_IDS_CUSTOM_FIELD)
end
end
end
end