discourse-chat-integration/app/models/plugin_model.rb

42 lines
967 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class DiscourseChatIntegration::PluginModel < PluginStoreRow
PLUGIN_NAME = "discourse-chat-integration"
2017-09-24 23:06:27 -04:00
default_scope { self.default_scope }
2017-09-28 04:32:38 -04:00
after_initialize :init_plugin_model
before_save :set_key
2017-08-01 15:53:39 -04:00
def self.default_scope
where(type_name: "JSON").where(plugin_name: self::PLUGIN_NAME).where(
"key LIKE ?",
"#{self.key_prefix}%",
)
end
def self.key_prefix
raise "Not implemented"
end
2017-08-01 15:53:39 -04:00
private
2017-09-24 23:06:27 -04:00
2018-06-07 21:52:35 -04:00
def set_key
self.key ||= self.class.alloc_key
end
2018-06-07 21:52:35 -04:00
def init_plugin_model
self.type_name ||= "JSON"
2018-06-07 21:52:35 -04:00
self.plugin_name ||= PLUGIN_NAME
end
2017-09-28 04:32:38 -04:00
2018-06-07 21:52:35 -04:00
def self.alloc_key
DistributedMutex.synchronize("#{self::PLUGIN_NAME}_#{self.key_prefix}_id") do
max_id = PluginStore.get(self::PLUGIN_NAME, "#{self.key_prefix}_id")
max_id = 1 unless max_id
PluginStore.set(self::PLUGIN_NAME, "#{self.key_prefix}_id", max_id + 1)
"#{self.key_prefix}#{max_id}"
end
2018-06-07 21:52:35 -04:00
end
end