1
0
Fork 0
discourse-custom-wizard-unl.../serializers/custom_wizard/wizard_field_serializer.rb

124 Zeilen
2 KiB
Ruby

# frozen_string_literal: true
class CustomWizard::FieldSerializer < ::ApplicationSerializer
attributes :id,
:index,
:type,
:required,
:value,
:label,
:placeholder,
:description,
:image,
:file_types,
2020-07-16 05:26:56 +02:00
:format,
:limit,
2020-03-24 10:35:46 +01:00
:property,
:content,
2021-01-30 18:46:04 +01:00
:validations,
:max_length,
:char_counter
def id
object.id
end
def index
object.index
end
def type
object.type
end
def required
object.required
end
def value
object.value
end
def include_value?
object.value.present?
end
def i18n_key
@i18n_key ||= "wizard.step.#{object.step.id}.fields.#{object.id}".underscore
end
2019-12-05 09:05:21 +01:00
def label
return object.label if object.label.present?
I18n.t("#{object.key || i18n_key}.label", default: '')
end
def include_label?
label.present?
end
2019-12-05 09:05:21 +01:00
def description
return object.description if object.description.present?
I18n.t("#{object.key || i18n_key}.description", default: '', base_url: Discourse.base_url)
end
def include_description?
description.present?
end
2019-12-05 09:05:21 +01:00
def image
object.image
end
def include_image?
object.image.present?
end
def placeholder
I18n.t("#{object.key || i18n_key}.placeholder", default: '')
end
def include_placeholder?
placeholder.present?
end
2019-12-05 09:05:21 +01:00
def file_types
object.file_types
end
2021-03-11 07:30:15 +01:00
2020-07-16 05:26:56 +02:00
def format
object.format
end
2021-03-11 07:30:15 +01:00
2019-12-05 09:05:21 +01:00
def limit
object.limit
end
2021-03-11 07:30:15 +01:00
2019-12-05 09:05:21 +01:00
def property
object.property
end
2021-03-11 07:30:15 +01:00
2020-03-30 08:16:03 +02:00
def content
object.content
2020-03-24 10:35:46 +01:00
end
2021-03-11 07:30:15 +01:00
2021-01-30 18:46:04 +01:00
def validations
validations = {}
object.validations&.each do |type, props|
2021-01-30 18:46:04 +01:00
next unless props["status"]
validations[props["position"]] ||= {}
validations[props["position"]][type] = props.merge CustomWizard::RealtimeValidation.types[type.to_sym]
2021-01-30 18:46:04 +01:00
end
validations
end
def max_length
object.max_length
end
def char_counter
object.char_counter
end
2021-03-11 07:30:15 +01:00
end