field_resolver.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class FormSchema::FieldResolver
  3. class << self
  4. def field_for_object_attribute(context:, attribute:)
  5. resolver_method = :"field_for_oa_type_#{attribute.data_type}"
  6. if respond_to?(resolver_method)
  7. return send(resolver_method, context: context, attribute: attribute)
  8. end
  9. raise "Cannot resolve object attribute field type #{attribute.data_type}."
  10. end
  11. def base_attributes(context:, attribute:)
  12. {
  13. context: context,
  14. name: attribute[:name],
  15. label: attribute[:display],
  16. }.tap do |result|
  17. default = attribute[:data_option]['default']
  18. result[:value] = default if !default.nil?
  19. end
  20. end
  21. def field_for_oa_type_input(context:, attribute:)
  22. case attribute[:data_option]['type']
  23. when 'password'
  24. FormSchema::Field::Password.new(
  25. **base_attributes(context: context, attribute: attribute),
  26. maxlength: attribute[:data_option]['maxlength']
  27. )
  28. when 'tel'
  29. FormSchema::Field::Telephone.new(
  30. **base_attributes(context: context, attribute: attribute),
  31. maxlength: attribute[:data_option]['maxlength']
  32. )
  33. when 'email'
  34. FormSchema::Field::Email.new(
  35. **base_attributes(context: context, attribute: attribute),
  36. )
  37. # TODO: what about the 'url' field type?
  38. # when 'url'
  39. else
  40. FormSchema::Field::Text.new(
  41. **base_attributes(context: context, attribute: attribute),
  42. maxlength: attribute[:data_option]['maxlength']
  43. # TODO: this and other field types have a 'link template' attribute, what to do about it?
  44. )
  45. end
  46. end
  47. def field_for_oa_type_textarea(context:, attribute:)
  48. FormSchema::Field::Textarea.new(
  49. **base_attributes(context: context, attribute: attribute),
  50. maxlength: attribute[:data_option]['maxlength']
  51. )
  52. end
  53. def field_for_oa_type_richtext(context:, attribute:)
  54. FormSchema::Field::Editor.new(
  55. **base_attributes(context: context, attribute: attribute),
  56. # TODO: the OA has a maxlength attribute, but Field::Editor does not support that yet.
  57. # maxlength: attribute[:data_option]['maxlength']
  58. )
  59. end
  60. def field_for_oa_type_select(context:, attribute:)
  61. options = attribute[:data_option][:options]
  62. mapped_options = if options.is_a? Array
  63. options.map { |e| { value: e['value'], label: e['name'] } }
  64. else
  65. options.keys.map { |key| { value: key, label: options[key] } }
  66. end
  67. additional_attributes = { options: mapped_options }
  68. additional_attributes[:multiple] = true if attribute.data_type.eql?('multiselect')
  69. FormSchema::Field::Select.new(
  70. **base_attributes(context: context, attribute: attribute),
  71. **additional_attributes,
  72. )
  73. end
  74. alias field_for_oa_type_multiselect field_for_oa_type_select
  75. # TODO: what about the tree (multi)select field type?
  76. def field_for_oa_type_boolean(context:, attribute:)
  77. options = attribute[:data_option][:options]
  78. FormSchema::Field::Select.new(
  79. **base_attributes(context: context, attribute: attribute),
  80. options: [
  81. { value: true, label: options[true] },
  82. { value: false, label: options[false] },
  83. ]
  84. )
  85. end
  86. def field_for_oa_type_integer(context:, attribute:)
  87. FormSchema::Field::Number.new(
  88. **base_attributes(context: context, attribute: attribute),
  89. min: attribute[:data_option][:min],
  90. max: attribute[:data_option][:max],
  91. )
  92. end
  93. def field_for_oa_type_date(context:, attribute:)
  94. FormSchema::Field::Date.new(
  95. **base_attributes(context: context, attribute: attribute),
  96. # TODO: what about the :diff attribute?
  97. )
  98. end
  99. def field_for_oa_type_datetime(context:, attribute:)
  100. FormSchema::Field::Datetime.new(
  101. **base_attributes(context: context, attribute: attribute),
  102. # TODO: there are also :diff, :future and :past attributes, what about them?
  103. )
  104. end
  105. end
  106. end