2020-11-08 04:24:20 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-10-20 07:40:23 +02:00
|
|
|
class ::CustomWizard::CustomField
|
2020-10-17 03:31:07 +02:00
|
|
|
include HasErrors
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
attr_reader :id
|
|
|
|
|
2020-10-20 07:40:23 +02:00
|
|
|
ATTRS ||= ["name", "klass", "type", "serializers"]
|
2020-11-08 04:24:20 +01:00
|
|
|
REQUIRED ||= ["name", "klass"]
|
|
|
|
NAMESPACE ||= "custom_wizard_custom_fields"
|
|
|
|
NAME_MIN_LENGTH ||= 3
|
2020-10-17 03:31:07 +02:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
CLASSES ||= {
|
|
|
|
topic: ["topic_view", "topic_list_item"],
|
|
|
|
group: ["basic_group"],
|
|
|
|
category: ["basic_category"],
|
|
|
|
post: ["post"]
|
|
|
|
}
|
|
|
|
TYPES ||= ["string", "boolean", "integer", "json"]
|
|
|
|
|
|
|
|
def self.serializers
|
|
|
|
CLASSES.values.flatten.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(id, data)
|
|
|
|
@id = id
|
2020-10-17 03:31:07 +02:00
|
|
|
data = data.with_indifferent_access
|
|
|
|
|
|
|
|
ATTRS.each do |attr|
|
2020-11-08 04:24:20 +01:00
|
|
|
self.class.class_eval { attr_accessor attr }
|
|
|
|
|
|
|
|
value = data[attr]
|
|
|
|
|
|
|
|
if value.present?
|
|
|
|
send("#{attr}=", value)
|
|
|
|
end
|
2020-10-17 03:31:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
validate
|
|
|
|
|
|
|
|
if valid?
|
|
|
|
data = {}
|
2020-11-08 04:24:20 +01:00
|
|
|
key = name
|
2020-10-17 03:31:07 +02:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
(ATTRS - ['name']).each do |attr|
|
|
|
|
data[attr] = send(attr)
|
2020-10-17 03:31:07 +02:00
|
|
|
end
|
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
if self.class.save_to_store(id, key, data)
|
|
|
|
self.class.reset
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2020-10-17 03:31:07 +02:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
|
|
|
ATTRS.each do |attr|
|
|
|
|
value = send(attr)
|
2020-11-08 04:24:20 +01:00
|
|
|
i18n_key = "wizard.custom_field.error"
|
2020-10-20 07:40:23 +02:00
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
if REQUIRED.include?(attr) && value.blank?
|
|
|
|
I18n.t("#{i18n_key}.required_attribute", attr: attr)
|
2020-10-20 07:40:23 +02:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
if (attr == 'klass' && CLASSES.keys.exclude?(value.to_sym)) ||
|
|
|
|
(attr == 'serializers' && CLASSES[klass.to_sym].blank?)
|
|
|
|
add_error(I18n.t("#{i18n_key}.unsupported_class", class: value))
|
|
|
|
next
|
2020-10-20 07:40:23 +02:00
|
|
|
end
|
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
if attr == 'serializers' && (unsupported = value - CLASSES[klass.to_sym]).length > 0
|
|
|
|
add_error(I18n.t("#{i18n_key}.unsupported_serializers",
|
|
|
|
class: klass,
|
|
|
|
serializers: unsupported.join(", ")
|
|
|
|
))
|
2020-10-20 07:40:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if attr == 'type' && TYPES.exclude?(value)
|
2020-11-08 04:24:20 +01:00
|
|
|
add_error(I18n.t("#{i18n_key}.unsupported_type", type: value))
|
2020-10-20 07:40:23 +02:00
|
|
|
end
|
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
|
|
|
|
if attr == 'name'
|
|
|
|
unless value.is_a?(String)
|
|
|
|
add_error(I18n.t("#{i18n_key}.name_invalid", name: value))
|
|
|
|
end
|
|
|
|
|
|
|
|
if value.length < NAME_MIN_LENGTH
|
|
|
|
add_error(I18n.t("#{i18n_key}.name_too_short", name: value, min_length: NAME_MIN_LENGTH))
|
|
|
|
end
|
|
|
|
|
|
|
|
if new? && self.class.exists?(name)
|
|
|
|
add_error(I18n.t("#{i18n_key}.name_already_taken", name: value))
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
@name = value.parameterize(separator: '_')
|
|
|
|
rescue
|
|
|
|
add_error(I18n.t("#{i18n_key}.name_invalid", name: value))
|
|
|
|
end
|
2020-10-20 07:40:23 +02:00
|
|
|
end
|
2020-10-17 03:31:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
def new?
|
|
|
|
id.blank?
|
|
|
|
end
|
|
|
|
|
2020-10-17 03:31:07 +02:00
|
|
|
def valid?
|
|
|
|
errors.blank?
|
|
|
|
end
|
|
|
|
|
2020-11-08 04:24:20 +01:00
|
|
|
def self.reset
|
|
|
|
@list = nil
|
|
|
|
end
|
|
|
|
|
2020-10-17 03:31:07 +02:00
|
|
|
def self.list
|
2020-11-08 04:24:20 +01:00
|
|
|
@list ||= PluginStoreRow.where(plugin_name: NAMESPACE)
|
|
|
|
.map { |record| create_from_store(record) }
|
2020-10-17 03:31:07 +02:00
|
|
|
end
|
2020-10-20 11:02:13 +02:00
|
|
|
|
2020-10-23 02:53:39 +02:00
|
|
|
def self.list_by(attr, value)
|
|
|
|
self.list.select do |cf|
|
2020-11-08 04:24:20 +01:00
|
|
|
if attr == :serializers
|
2020-10-23 02:53:39 +02:00
|
|
|
cf.send(attr).include?(value)
|
|
|
|
else
|
|
|
|
cf.send(attr) == value
|
2020-10-20 11:02:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-08 04:24:20 +01:00
|
|
|
|
|
|
|
def self.exists?(name)
|
|
|
|
PluginStoreRow.where(plugin_name: NAMESPACE, key: name).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find(name)
|
|
|
|
records = PluginStoreRow.where(plugin_name: NAMESPACE, key: name)
|
|
|
|
|
|
|
|
if records.exists?
|
|
|
|
create_from_store(records.first)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_from_store(record)
|
|
|
|
data = JSON.parse(record.value)
|
|
|
|
data[:name] = record.key
|
|
|
|
new(record.id, data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.save_to_store(id = nil, key, data)
|
|
|
|
if id
|
|
|
|
record = PluginStoreRow.find_by(id: id, plugin_name: NAMESPACE, key: key)
|
|
|
|
return false if !record
|
|
|
|
record.value = data.to_json
|
|
|
|
record.save
|
|
|
|
else
|
|
|
|
record = PluginStoreRow.new(plugin_name: NAMESPACE, key: key)
|
|
|
|
record.type_name = "JSON"
|
|
|
|
record.value = data.to_json
|
|
|
|
record.save
|
|
|
|
end
|
|
|
|
end
|
2020-10-17 03:31:07 +02:00
|
|
|
end
|