REFACTOR: Remove hardcoded base `KEY_PREFIX`.

This commit is contained in:
Guo Xiang Tan 2017-10-03 15:35:27 +08:00
parent d77afa4c98
commit 2d6737ce5c
3 changed files with 17 additions and 11 deletions

View File

@ -1,6 +1,4 @@
class DiscourseChat::Channel < DiscourseChat::PluginModel
KEY_PREFIX = 'channel:'
# Setup ActiveRecord::Store to use the JSON field to read/write these values
store :value, accessors: [ :provider, :error_key, :data ], coder: JSON
@ -12,6 +10,10 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel
validate :provider_valid?, :data_valid?
def self.key_prefix
'channel:'.freeze
end
def rules
DiscourseChat::Rule.with_channel_id(id).order_by_precedence
end

View File

@ -1,6 +1,5 @@
class DiscourseChat::PluginModel < PluginStoreRow
PLUGIN_NAME = 'discourse-chat-integration'
KEY_PREFIX = 'unimplemented'
default_scope { self.default_scope }
@ -10,7 +9,11 @@ class DiscourseChat::PluginModel < PluginStoreRow
def self.default_scope
where(type_name: 'JSON')
.where(plugin_name: self::PLUGIN_NAME)
.where("key LIKE ?", "#{self::KEY_PREFIX}%")
.where("key LIKE ?", "#{self.key_prefix}%")
end
def self.key_prefix
raise 'Not implemented'
end
private
@ -25,12 +28,11 @@ class DiscourseChat::PluginModel < PluginStoreRow
end
def self.alloc_key
raise "KEY_PREFIX must be defined" if self::KEY_PREFIX == 'unimplemented'
DistributedMutex.synchronize("#{self::PLUGIN_NAME}_#{self::KEY_PREFIX}_id") do
max_id = PluginStore.get(self::PLUGIN_NAME, "#{self::KEY_PREFIX}_id")
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}"
PluginStore.set(self::PLUGIN_NAME, "#{self.key_prefix}_id", max_id + 1)
"#{self.key_prefix}#{max_id}"
end
end

View File

@ -1,6 +1,4 @@
class DiscourseChat::Rule < DiscourseChat::PluginModel
KEY_PREFIX = 'rule:'
# Setup ActiveRecord::Store to use the JSON field to read/write these values
store :value, accessors: [ :channel_id, :type, :group_id, :category_id, :tags, :filter ], coder: JSON
@ -47,6 +45,10 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel
validate :channel_valid?, :category_valid?, :group_valid?, :tags_valid?
def self.key_prefix
'rule:'.freeze
end
# We never want an empty array, set it to nil instead
def tags=(array)
if array.nil? || array.empty?