2021-03-11 07:30:15 +01:00
|
|
|
# frozen_string_literal: true
|
2021-04-20 19:58:19 +02:00
|
|
|
|
2017-10-09 07:52:09 +02:00
|
|
|
class CustomWizard::Field
|
2021-04-20 19:58:19 +02:00
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
2021-06-01 07:27:49 +02:00
|
|
|
##
|
|
|
|
# type: step
|
|
|
|
# number: 3
|
|
|
|
# title: Add the field name to attribute map
|
|
|
|
# description: The attribute map serves as a global registry for field attributes. Set the
|
2021-06-01 07:49:39 +02:00
|
|
|
# key as the attribute name and value as an array of properties. Use the properties according to the
|
2021-06-01 07:27:49 +02:00
|
|
|
# your usecase. Here's a list and description of each of the properties.
|
|
|
|
# accessible: The attribute is passed to attr_accessor
|
|
|
|
# serializable: The attribute is serialized to the client
|
|
|
|
# permitted: The attribute is permitted to be passed to the admin side controller for saving
|
|
|
|
# mapped: The attribute is a mapped attributed and is permitted to be passed to the admin side controller for saving
|
|
|
|
# excluded: The attribute is not initialized in the constructor. This is used
|
|
|
|
# if the attribute needs a method
|
|
|
|
##
|
|
|
|
|
2021-05-23 19:41:46 +02:00
|
|
|
def self.attribute_map
|
|
|
|
{
|
|
|
|
raw: [],
|
2021-05-24 03:33:30 +02:00
|
|
|
id: [:serializable, :permitted],
|
|
|
|
index: [:accessible, :serializable, :permitted, :mapped],
|
|
|
|
type: [:serializable, :permitted],
|
2021-05-23 19:41:46 +02:00
|
|
|
step: [:accessible],
|
2021-05-24 03:33:30 +02:00
|
|
|
required: [:serializable, :permitted],
|
2021-05-23 19:41:46 +02:00
|
|
|
value: [:serializable],
|
2021-05-24 03:33:30 +02:00
|
|
|
description: [:serializable, :permitted],
|
|
|
|
image: [:serializable, :permitted],
|
|
|
|
key: [:permitted],
|
2021-05-23 19:41:46 +02:00
|
|
|
validations: [:serializable],
|
2021-05-24 03:33:30 +02:00
|
|
|
min_length: [:permitted],
|
|
|
|
max_length: [:serializable, :permitted],
|
|
|
|
char_counter: [:serializable, :permitted],
|
|
|
|
file_types: [:serializable, :permitted],
|
|
|
|
format: [:serializable, :permitted],
|
|
|
|
limit: [:serializable, :permitted],
|
|
|
|
property: [:serializable, :permitted],
|
2021-05-23 19:41:46 +02:00
|
|
|
# label is excluded so that it isn't initialized and the value
|
|
|
|
# returned by `label` method is used for serialization
|
2021-05-24 03:33:30 +02:00
|
|
|
label: [:excluded, :serializable, :permitted],
|
|
|
|
content: [:serializable, :permitted, :mapped],
|
|
|
|
prefill: [:permitted, :mapped],
|
|
|
|
condition: [:permitted, :mapped],
|
2021-05-16 21:51:55 +02:00
|
|
|
}
|
2021-05-16 13:52:46 +02:00
|
|
|
end
|
|
|
|
|
2021-05-23 19:41:46 +02:00
|
|
|
def self.all_attributes
|
|
|
|
attribute_map.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.included_attributes
|
|
|
|
all_attributes - excluded_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.type_attributes(type)
|
|
|
|
attribute_map.map { |attribute, props| props.include?(type.to_sym) ? attribute : nil }.compact
|
|
|
|
end
|
|
|
|
|
2021-05-16 13:52:46 +02:00
|
|
|
def self.accessible_attributes
|
2021-05-23 19:41:46 +02:00
|
|
|
type_attributes(:accessible)
|
2021-05-16 13:52:46 +02:00
|
|
|
end
|
2021-04-20 19:58:19 +02:00
|
|
|
|
2021-05-16 13:52:46 +02:00
|
|
|
def self.excluded_attributes
|
2021-05-23 19:41:46 +02:00
|
|
|
type_attributes(:excluded)
|
2021-05-16 13:52:46 +02:00
|
|
|
end
|
|
|
|
|
2021-05-16 13:56:27 +02:00
|
|
|
def self.readonly_attributes
|
2021-05-23 19:41:46 +02:00
|
|
|
included_attributes - accessible_attributes
|
2021-05-16 13:56:27 +02:00
|
|
|
end
|
|
|
|
|
2021-05-16 21:51:55 +02:00
|
|
|
def self.serializable_attributes
|
2021-05-23 19:41:46 +02:00
|
|
|
type_attributes(:serializable)
|
2021-05-16 21:51:55 +02:00
|
|
|
end
|
|
|
|
|
2021-05-16 13:56:27 +02:00
|
|
|
attr_reader *readonly_attributes
|
2021-05-16 13:52:46 +02:00
|
|
|
attr_accessor *accessible_attributes
|
2021-04-20 19:58:19 +02:00
|
|
|
|
|
|
|
def initialize(attrs)
|
2021-05-16 13:52:46 +02:00
|
|
|
attrs.each do |k, v|
|
2021-05-23 19:41:46 +02:00
|
|
|
if self.singleton_class.included_attributes.include?(k.to_sym)
|
2021-05-16 13:52:46 +02:00
|
|
|
instance_variable_set("@#{k}", v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-20 19:58:19 +02:00
|
|
|
@raw = attrs || {}
|
|
|
|
@required = !!attrs[:required]
|
2021-04-30 19:29:06 +02:00
|
|
|
@value = attrs[:value] || default_value
|
2021-04-20 19:58:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def label
|
|
|
|
@label ||= PrettyText.cook(@raw[:label])
|
|
|
|
end
|
|
|
|
|
2021-04-30 19:29:06 +02:00
|
|
|
def default_value
|
|
|
|
if @type == 'checkbox'
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-09 07:52:09 +02:00
|
|
|
def self.types
|
2020-04-16 04:04:27 +02:00
|
|
|
@types ||= {
|
|
|
|
text: {
|
2020-07-16 09:50:09 +02:00
|
|
|
min_length: nil,
|
2020-12-08 08:49:31 +01:00
|
|
|
max_length: nil,
|
2021-01-12 11:46:24 +01:00
|
|
|
prefill: nil,
|
2021-01-30 18:46:04 +01:00
|
|
|
char_counter: nil,
|
|
|
|
validations: nil
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
textarea: {
|
2020-07-16 09:50:09 +02:00
|
|
|
min_length: nil,
|
2020-12-08 08:49:31 +01:00
|
|
|
max_length: nil,
|
2021-01-12 11:46:24 +01:00
|
|
|
prefill: nil,
|
|
|
|
char_counter: nil
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
composer: {
|
2020-12-08 08:14:37 +01:00
|
|
|
min_length: nil,
|
2020-12-08 08:49:31 +01:00
|
|
|
max_length: nil,
|
2021-01-12 11:46:24 +01:00
|
|
|
char_counter: nil
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
text_only: {},
|
2020-07-16 05:26:56 +02:00
|
|
|
date: {
|
|
|
|
format: "YYYY-MM-DD"
|
|
|
|
},
|
|
|
|
time: {
|
|
|
|
format: "HH:mm"
|
|
|
|
},
|
|
|
|
date_time: {
|
|
|
|
format: ""
|
|
|
|
},
|
2020-04-16 04:04:27 +02:00
|
|
|
number: {},
|
|
|
|
checkbox: {},
|
|
|
|
url: {
|
|
|
|
min_length: nil
|
|
|
|
},
|
|
|
|
upload: {
|
2021-03-12 11:12:26 +01:00
|
|
|
file_types: '.jpg,.jpeg,.png'
|
2020-04-16 04:04:27 +02:00
|
|
|
},
|
|
|
|
dropdown: {
|
|
|
|
prefill: nil,
|
|
|
|
content: nil
|
|
|
|
},
|
|
|
|
tag: {
|
|
|
|
limit: nil,
|
|
|
|
prefill: nil,
|
|
|
|
content: nil
|
|
|
|
},
|
|
|
|
category: {
|
|
|
|
limit: 1,
|
|
|
|
property: 'id',
|
|
|
|
prefill: nil,
|
|
|
|
content: nil
|
|
|
|
},
|
|
|
|
group: {
|
|
|
|
prefill: nil,
|
|
|
|
content: nil
|
|
|
|
},
|
|
|
|
user_selector: {}
|
|
|
|
}
|
2017-10-09 07:52:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.require_assets
|
|
|
|
@require_assets ||= {}
|
|
|
|
end
|
|
|
|
|
2021-03-11 07:30:15 +01:00
|
|
|
def self.register(type, plugin = nil, asset_paths = [], opts = {})
|
2017-10-09 07:52:09 +02:00
|
|
|
if type
|
2020-11-03 01:24:20 +01:00
|
|
|
types[type.to_sym] ||= {}
|
|
|
|
types[type.to_sym] = opts[:type_opts] if opts[:type_opts].present?
|
2017-10-09 07:52:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if plugin && asset_paths
|
|
|
|
require_assets[plugin] = asset_paths
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|