0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/plugins/discourse-chat-integration/app/models/plugin_model.rb
Jarek Radosz 8baf4d5d4c
DEV: Enable Style/RedundantSelf rubocop rule (#40098)
(to be enabled in the shared config)
2026-05-19 19:27:45 +02:00

41 lines
937 B
Ruby
Vendored

# frozen_string_literal: true
class DiscourseChatIntegration::PluginModel < PluginStoreRow
PLUGIN_NAME = "discourse-chat-integration"
default_scope { default_scope }
after_initialize :init_plugin_model
before_save :set_key
def self.default_scope
where(type_name: "JSON").where(plugin_name: self::PLUGIN_NAME).where(
"key LIKE ?",
"#{key_prefix}%",
)
end
def self.key_prefix
raise "Not implemented"
end
private
def set_key
self.key ||= self.class.alloc_key
end
def init_plugin_model
self.type_name ||= "JSON"
self.plugin_name ||= PLUGIN_NAME
end
def self.alloc_key
DistributedMutex.synchronize("#{self::PLUGIN_NAME}_#{key_prefix}_id") do
max_id = PluginStore.get(self::PLUGIN_NAME, "#{key_prefix}_id")
max_id = 1 unless max_id
PluginStore.set(self::PLUGIN_NAME, "#{key_prefix}_id", max_id + 1)
"#{key_prefix}#{max_id}"
end
end
end