0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-15 14:22:53 +01:00

simple framework to compute field attributes at runtime

Dieser Commit ist enthalten in:
Faizaan Gagan 2021-05-23 23:11:46 +05:30
Ursprung b37bd54bcc
Commit ce705d32da

Datei anzeigen

@ -3,67 +3,59 @@
class CustomWizard::Field
include ActiveModel::SerializerSupport
def self.attributes
%i{
raw
id
index
type
step
required
value
description
image
key
validations
min_length
max_length
char_counter
file_types
format
limit
property
content
def self.attribute_map
{
raw: [],
id: [:serializable],
index: [:accessible, :serializable],
type: [:serializable],
step: [:accessible],
required: [:serializable],
value: [:serializable],
description: [:serializable],
image: [:serializable],
key: [],
validations: [:serializable],
min_length: [],
max_length: [:serializable],
char_counter: [:serializable],
file_types: [:serializable],
format: [:serializable],
limit: [:serializable],
property: [:serializable],
content: [:serializable],
# label is excluded so that it isn't initialized and the value
# returned by `label` method is used for serialization
label: [:excluded, :serializable]
}
end
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
def self.accessible_attributes
%i{
index
step
}
type_attributes(:accessible)
end
def self.excluded_attributes
%i{
label
}
type_attributes(:excluded)
end
def self.readonly_attributes
attributes - accessible_attributes - excluded_attributes
included_attributes - accessible_attributes
end
def self.serializable_attributes
%i{
id
index
type
required
value
label
placeholder
description
image
file_types
format
limit
property
content
validations
max_length
char_counter
}
type_attributes(:serializable)
end
attr_reader *readonly_attributes
@ -71,7 +63,7 @@ class CustomWizard::Field
def initialize(attrs)
attrs.each do |k, v|
if self.singleton_class.attributes.include?(k.to_sym)
if self.singleton_class.included_attributes.include?(k.to_sym)
instance_variable_set("@#{k}", v)
end
end