|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Alchemy |
| 4 | + module Ingredients |
| 5 | + class BaseEditor < ViewComponent::Base |
| 6 | + delegate :definition, |
| 7 | + :element, |
| 8 | + :id, |
| 9 | + :linked?, |
| 10 | + :partial_name, |
| 11 | + :role, |
| 12 | + :settings, |
| 13 | + :value, |
| 14 | + to: :ingredient |
| 15 | + |
| 16 | + delegate :alchemy, |
| 17 | + :dom_id, |
| 18 | + :hint_with_tooltip, |
| 19 | + :render_hint_for, |
| 20 | + :render_icon, |
| 21 | + :warning, |
| 22 | + to: :helpers |
| 23 | + |
| 24 | + attr_reader :ingredient, :html_options |
| 25 | + |
| 26 | + def initialize(ingredient, html_options: {}) |
| 27 | + raise ArgumentError, "Ingredient missing!" if ingredient.nil? |
| 28 | + |
| 29 | + @ingredient = ingredient |
| 30 | + @html_options = html_options |
| 31 | + end |
| 32 | + |
| 33 | + def call |
| 34 | + tag.div(class: css_classes, data: data_attributes, id: dom_id(ingredient)) do |
| 35 | + concat ingredient_id_field |
| 36 | + concat ingredient_label |
| 37 | + concat input_field |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + # Returns a string to be passed to Rails form field helpers. |
| 42 | + # |
| 43 | + # === Example: |
| 44 | + # |
| 45 | + # <%= text_field_tag text_editor.form_field_name, text_editor.value %> |
| 46 | + # |
| 47 | + # === Options: |
| 48 | + # |
| 49 | + # You can pass an Ingredient column_name. Default is 'value' |
| 50 | + # |
| 51 | + # ==== Example: |
| 52 | + # |
| 53 | + # <%= text_field_tag text_editor.form_field_name(:link), text_editor.value %> |
| 54 | + # |
| 55 | + def form_field_name(column = "value") |
| 56 | + "element[ingredients_attributes][#{form_field_counter}][#{column}]" |
| 57 | + end |
| 58 | + |
| 59 | + # Returns a unique string to be passed to a form field id. |
| 60 | + # |
| 61 | + # @param column [String] A Ingredient column_name. Default is 'value' |
| 62 | + # |
| 63 | + def form_field_id(column = "value") |
| 64 | + "element_#{element.id}_ingredient_#{ingredient.id}_#{column}" |
| 65 | + end |
| 66 | + |
| 67 | + private |
| 68 | + |
| 69 | + # Returns the translated role for displaying in labels |
| 70 | + # |
| 71 | + # Translate it in your locale yml file: |
| 72 | + # |
| 73 | + # alchemy: |
| 74 | + # ingredient_roles: |
| 75 | + # foo: Bar |
| 76 | + # |
| 77 | + # Optionally you can scope your ingredient role to an element: |
| 78 | + # |
| 79 | + # alchemy: |
| 80 | + # ingredient_roles: |
| 81 | + # article: |
| 82 | + # foo: Baz |
| 83 | + # |
| 84 | + def translated_role |
| 85 | + Alchemy.t( |
| 86 | + role, |
| 87 | + scope: "ingredient_roles.#{element.name}", |
| 88 | + default: Alchemy.t("ingredient_roles.#{role}", default: role.humanize) |
| 89 | + ) |
| 90 | + end |
| 91 | + |
| 92 | + def css_classes |
| 93 | + [ |
| 94 | + "ingredient-editor", |
| 95 | + partial_name, |
| 96 | + ingredient.deprecated? ? "deprecated" : nil, |
| 97 | + settings[:linkable] ? "linkable" : nil, |
| 98 | + settings[:anchor] ? "with-anchor" : nil |
| 99 | + ].compact |
| 100 | + end |
| 101 | + |
| 102 | + def data_attributes |
| 103 | + { |
| 104 | + ingredient_id: ingredient.id, |
| 105 | + ingredient_role: role |
| 106 | + } |
| 107 | + end |
| 108 | + |
| 109 | + def has_warnings? |
| 110 | + definition.blank? || ingredient.deprecated? |
| 111 | + end |
| 112 | + |
| 113 | + def warnings |
| 114 | + return unless has_warnings? |
| 115 | + |
| 116 | + if definition.blank? |
| 117 | + Logger.warn("ingredient #{role} is missing its definition", caller(1..1)) |
| 118 | + Alchemy.t(:ingredient_definition_missing) |
| 119 | + else |
| 120 | + definition.deprecation_notice(element_name: element&.name) |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + def validations |
| 125 | + definition.validate |
| 126 | + end |
| 127 | + |
| 128 | + def format_validation |
| 129 | + format = validations.select { _1.is_a?(Hash) }.find { _1[:format] }&.fetch(:format) |
| 130 | + return nil unless format |
| 131 | + |
| 132 | + # If format is a string or symbol, resolve it from config format_matchers |
| 133 | + if format.is_a?(String) || format.is_a?(Symbol) |
| 134 | + Alchemy.config.format_matchers.get(format) |
| 135 | + else |
| 136 | + format |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + def length_validation |
| 141 | + validations.select { _1.is_a?(Hash) }.find { _1[:length] }&.fetch(:length) |
| 142 | + end |
| 143 | + |
| 144 | + def presence_validation? |
| 145 | + validations.any? do |validation| |
| 146 | + case validation |
| 147 | + when :presence, "presence" |
| 148 | + true |
| 149 | + when Hash |
| 150 | + validation[:presence] == true || validation["presence"] == true |
| 151 | + else |
| 152 | + false |
| 153 | + end |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + def form_field_counter |
| 158 | + element.definition.ingredients.index { |i| i.role == role } |
| 159 | + end |
| 160 | + |
| 161 | + # Renders the translated role of ingredient. |
| 162 | + # |
| 163 | + # Displays a warning icon if ingredient is missing its definition. |
| 164 | + # |
| 165 | + # Displays a mandatory field indicator, if the ingredient has validations. |
| 166 | + # |
| 167 | + def ingredient_role |
| 168 | + content = translated_role |
| 169 | + |
| 170 | + if has_warnings? |
| 171 | + icon = hint_with_tooltip(warnings) |
| 172 | + content = "#{icon} #{content}".html_safe |
| 173 | + end |
| 174 | + |
| 175 | + if ingredient.has_validations? |
| 176 | + "#{content}<span class='validation_indicator'>*</span>".html_safe |
| 177 | + else |
| 178 | + content |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + # Renders the label and hint for a ingredient. |
| 183 | + def ingredient_label(column = :value) |
| 184 | + label_tag form_field_id(column) do |
| 185 | + concat ingredient_role |
| 186 | + concat render_hint_for(ingredient, size: "1x", fixed_width: false) |
| 187 | + end |
| 188 | + end |
| 189 | + |
| 190 | + # Renders a hidden field with the ingredient's ID. |
| 191 | + # This allows Rails to identify which ingredient to update |
| 192 | + # when processing nested attributes. |
| 193 | + # |
| 194 | + def ingredient_id_field |
| 195 | + hidden_field_tag(form_field_name(:id), ingredient.id, id: nil) |
| 196 | + end |
| 197 | + |
| 198 | + # Renders the input field for the ingredient. |
| 199 | + # Override this method in subclasses to provide custom input fields. |
| 200 | + # For example a text area or a select box. |
| 201 | + # |
| 202 | + def input_field |
| 203 | + tag.div(class: "input-field") do |
| 204 | + text_field_tag(form_field_name, |
| 205 | + value, |
| 206 | + class: "full_width", |
| 207 | + id: form_field_id, |
| 208 | + minlength: length_validation&.fetch(:minimum, nil), |
| 209 | + maxlength: length_validation&.fetch(:maximum, nil), |
| 210 | + required: presence_validation?, |
| 211 | + pattern: format_validation) |
| 212 | + end |
| 213 | + end |
| 214 | + end |
| 215 | + end |
| 216 | +end |
0 commit comments