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 class CustomWizard::Field
include ActiveModel::SerializerSupport include ActiveModel::SerializerSupport
def self.attributes def self.attribute_map
%i{ {
raw raw: [],
id id: [:serializable],
index index: [:accessible, :serializable],
type type: [:serializable],
step step: [:accessible],
required required: [:serializable],
value value: [:serializable],
description description: [:serializable],
image image: [:serializable],
key key: [],
validations validations: [:serializable],
min_length min_length: [],
max_length max_length: [:serializable],
char_counter char_counter: [:serializable],
file_types file_types: [:serializable],
format format: [:serializable],
limit limit: [:serializable],
property property: [:serializable],
content 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 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 def self.accessible_attributes
%i{ type_attributes(:accessible)
index
step
}
end end
def self.excluded_attributes def self.excluded_attributes
%i{ type_attributes(:excluded)
label
}
end end
def self.readonly_attributes def self.readonly_attributes
attributes - accessible_attributes - excluded_attributes included_attributes - accessible_attributes
end end
def self.serializable_attributes def self.serializable_attributes
%i{ type_attributes(:serializable)
id
index
type
required
value
label
placeholder
description
image
file_types
format
limit
property
content
validations
max_length
char_counter
}
end end
attr_reader *readonly_attributes attr_reader *readonly_attributes
@ -71,7 +63,7 @@ class CustomWizard::Field
def initialize(attrs) def initialize(attrs)
attrs.each do |k, v| 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) instance_variable_set("@#{k}", v)
end end
end end