|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Plutonium |
| 4 | + module UI |
| 5 | + module Form |
| 6 | + module Concerns |
| 7 | + # Handles rendering of nested resource fields in forms |
| 8 | + # @api private |
| 9 | + module RendersNestedResourceFields |
| 10 | + extend ActiveSupport::Concern |
| 11 | + |
| 12 | + DEFAULT_NESTED_LIMIT = 10 |
| 13 | + NESTED_OPTION_KEYS = [:allow_destroy, :update_only, :macro, :class].freeze |
| 14 | + SINGULAR_MACROS = %i[belongs_to has_one].freeze |
| 15 | + |
| 16 | + class NestedInputsDefinition |
| 17 | + include Plutonium::Definition::DefineableProps |
| 18 | + |
| 19 | + defineable_props :field, :input |
| 20 | + end |
| 21 | + |
| 22 | + # Template object for new nested records |
| 23 | + class NotPersisted |
| 24 | + def persisted? |
| 25 | + false |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + private |
| 30 | + |
| 31 | + # Renders a nested resource field with associated inputs |
| 32 | + # @param [Symbol] name The name of the nested resource field |
| 33 | + # @raise [ArgumentError] if the nested input definition is missing required configuration |
| 34 | + def render_nested_resource_field(name) |
| 35 | + context = NestedFieldContext.new( |
| 36 | + name: name, |
| 37 | + definition: build_nested_definition(name), |
| 38 | + resource_class: resource_class, |
| 39 | + resource_definition: resource_definition |
| 40 | + ) |
| 41 | + |
| 42 | + render_nested_field_container(context) do |
| 43 | + render_nested_field_header(context) |
| 44 | + render_nested_field_content(context) |
| 45 | + render_nested_add_button(context) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + private |
| 50 | + |
| 51 | + class NestedFieldContext |
| 52 | + attr_reader :name, :definition, :options, :permitted_fields |
| 53 | + |
| 54 | + def initialize(name:, definition:, resource_class:, resource_definition:) |
| 55 | + @name = name |
| 56 | + @definition = definition |
| 57 | + @resource_definition = resource_definition |
| 58 | + @resource_class = resource_class |
| 59 | + @options = build_options |
| 60 | + @permitted_fields = build_permitted_fields |
| 61 | + end |
| 62 | + |
| 63 | + def nested_attribute_options |
| 64 | + @nested_attribute_options ||= @resource_class.all_nested_attributes_options[@name] || {} |
| 65 | + end |
| 66 | + |
| 67 | + def nested_input_param |
| 68 | + @options[:as] || :"#{@name}_attributes" |
| 69 | + end |
| 70 | + |
| 71 | + def multiple? |
| 72 | + @options[:multiple] |
| 73 | + end |
| 74 | + |
| 75 | + private |
| 76 | + |
| 77 | + def build_options |
| 78 | + options = @resource_definition.defined_nested_inputs[@name][:options].dup || {} |
| 79 | + merge_nested_options(options) |
| 80 | + set_nested_limits(options) |
| 81 | + options |
| 82 | + end |
| 83 | + |
| 84 | + def merge_nested_options(options) |
| 85 | + NESTED_OPTION_KEYS.each do |key| |
| 86 | + options.fetch(key) { options[key] = nested_attribute_options[key] } |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + def set_nested_limits(options) |
| 91 | + options.fetch(:limit) do |
| 92 | + options[:limit] = if SINGULAR_MACROS.include?(nested_attribute_options[:macro]) |
| 93 | + 1 |
| 94 | + else |
| 95 | + nested_attribute_options[:limit] || DEFAULT_NESTED_LIMIT |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + options.fetch(:multiple) do |
| 100 | + options[:multiple] = !SINGULAR_MACROS.include?(nested_attribute_options[:macro]) |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + def build_permitted_fields |
| 105 | + @options[:fields] || @definition.defined_inputs.keys |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + def build_nested_definition(name) |
| 110 | + nested_input_definition = resource_definition.defined_nested_inputs[name] |
| 111 | + |
| 112 | + if nested_input_definition[:options]&.fetch(:using, nil) |
| 113 | + nested_input_definition[:options][:using] |
| 114 | + elsif nested_input_definition[:block] |
| 115 | + build_definition_from_block(nested_input_definition[:block]) |
| 116 | + else |
| 117 | + raise_missing_nested_definition_error(name) |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + def build_definition_from_block(block) |
| 122 | + definition = NestedInputsDefinition.new |
| 123 | + block.call(definition) |
| 124 | + definition |
| 125 | + end |
| 126 | + |
| 127 | + def render_nested_field_container(context, &) |
| 128 | + div( |
| 129 | + class: "col-span-full space-y-2 my-4", |
| 130 | + data: { |
| 131 | + controller: "nested-resource-form-fields", |
| 132 | + nested_resource_form_fields_limit_value: context.options[:limit] |
| 133 | + }, |
| 134 | + & |
| 135 | + ) |
| 136 | + end |
| 137 | + |
| 138 | + def render_nested_field_header(context) |
| 139 | + div do |
| 140 | + h2(class: "text-lg font-semibold text-gray-900 dark:text-white") { context.name.to_s.humanize } |
| 141 | + render_description(context.options[:description]) if context.options[:description] |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + def render_description(description) |
| 146 | + p(class: "text-md font-normal text-gray-500 dark:text-gray-400") { description } |
| 147 | + end |
| 148 | + |
| 149 | + def render_nested_field_content(context) |
| 150 | + if context.multiple? |
| 151 | + render_multiple_nested_fields(context) |
| 152 | + else |
| 153 | + render_single_nested_field(context) |
| 154 | + end |
| 155 | + |
| 156 | + div(data_nested_resource_form_fields_target: :target, hidden: true) |
| 157 | + end |
| 158 | + |
| 159 | + def render_multiple_nested_fields(context) |
| 160 | + render_template_for_nested_fields(context, collection: {NEW_RECORD: NotPersisted.new}) |
| 161 | + render_existing_nested_fields(context) |
| 162 | + end |
| 163 | + |
| 164 | + def render_single_nested_field(context) |
| 165 | + render_template_for_nested_fields(context, object: NotPersisted.new) |
| 166 | + render_existing_nested_fields(context, single: true) |
| 167 | + end |
| 168 | + |
| 169 | + def render_template_for_nested_fields(context, field_options) |
| 170 | + template_tag data_nested_resource_form_fields_target: "template" do |
| 171 | + nesting_method = field_options[:collection] ? :nest_many : :nest_one |
| 172 | + send(nesting_method, context.name, as: context.nested_input_param, template: true, **field_options) do |nested| |
| 173 | + render_fieldset(nested, context) |
| 174 | + end |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + def render_existing_nested_fields(context, single: false) |
| 179 | + nesting_method = single ? :nest_one : :nest_many |
| 180 | + send(nesting_method, context.name, as: context.nested_input_param) do |nested| |
| 181 | + render_fieldset(nested, context) |
| 182 | + end |
| 183 | + end |
| 184 | + |
| 185 | + def render_fieldset(nested, context) |
| 186 | + fieldset( |
| 187 | + data_new_record: !nested.object&.persisted?, |
| 188 | + class: "nested-resource-form-fields border border-gray-200 dark:border-gray-700 rounded-lg p-4 space-y-4 relative" |
| 189 | + ) do |
| 190 | + render_fieldset_content(nested, context) |
| 191 | + render_delete_button(nested, context.options) |
| 192 | + end |
| 193 | + end |
| 194 | + |
| 195 | + def render_fieldset_content(nested, context) |
| 196 | + div(class: "grid grid-cols-1 md:grid-cols-2 2xl:grid-cols-4 gap-4 grid-flow-row-dense") do |
| 197 | + render_hidden_fields(nested, context) |
| 198 | + render_input_fields(nested, context) |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + def render_hidden_fields(nested, context) |
| 203 | + if !context.options[:update_only] && context.options[:class]&.respond_to?(:primary_key) |
| 204 | + render nested.field(context.options[:class].primary_key).hidden_tag |
| 205 | + end |
| 206 | + render nested.field(:_destroy).hidden_tag if context.options[:allow_destroy] |
| 207 | + end |
| 208 | + |
| 209 | + def render_input_fields(nested, context) |
| 210 | + context.permitted_fields.each do |input| |
| 211 | + render_simple_resource_field(input, context.definition, nested) |
| 212 | + end |
| 213 | + end |
| 214 | + |
| 215 | + def render_delete_button(nested, options) |
| 216 | + return unless !nested.object&.persisted? || options[:allow_destroy] |
| 217 | + |
| 218 | + render_delete_button_content |
| 219 | + end |
| 220 | + |
| 221 | + def render_delete_button_content |
| 222 | + div(class: "flex items-center justify-end") do |
| 223 | + label(class: "inline-flex items-center text-md font-medium text-red-900 cursor-pointer") do |
| 224 | + plain "Delete" |
| 225 | + render_delete_checkbox |
| 226 | + end |
| 227 | + end |
| 228 | + end |
| 229 | + |
| 230 | + def render_delete_checkbox |
| 231 | + input( |
| 232 | + type: :checkbox, |
| 233 | + class: "w-4 h-4 ms-2 text-red-600 bg-red-100 border-red-300 rounded focus:ring-red-500 dark:focus:ring-red-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600 cursor-pointer", |
| 234 | + data_action: "nested-resource-form-fields#remove" |
| 235 | + ) |
| 236 | + end |
| 237 | + |
| 238 | + def render_nested_add_button(context) |
| 239 | + div do |
| 240 | + button( |
| 241 | + type: :button, |
| 242 | + class: "inline-block", |
| 243 | + data: { |
| 244 | + action: "nested-resource-form-fields#add", |
| 245 | + nested_resource_form_fields_target: "addButton" |
| 246 | + } |
| 247 | + ) do |
| 248 | + render_add_button_content(context.name) |
| 249 | + end |
| 250 | + end |
| 251 | + end |
| 252 | + |
| 253 | + def render_add_button_content(name) |
| 254 | + span(class: "bg-secondary-700 text-white hover:bg-secondary-800 focus:ring-secondary-300 dark:bg-secondary-600 dark:hover:bg-secondary-700 dark:focus:ring-secondary-800 flex items-center justify-center px-4 py-1.5 text-sm font-medium rounded-lg focus:outline-none focus:ring-4") do |
| 255 | + render Phlex::TablerIcons::Plus.new(class: "w-4 h-4 mr-1") |
| 256 | + span { "Add #{name.to_s.singularize.humanize}" } |
| 257 | + end |
| 258 | + end |
| 259 | + |
| 260 | + def raise_missing_nested_definition_error(name) |
| 261 | + raise ArgumentError, %( |
| 262 | + `nested_input :#{name}` is missing a definition |
| 263 | +
|
| 264 | + you can either pass in a block: |
| 265 | + ```ruby |
| 266 | + nested_input :#{name} do |definition| |
| 267 | + input :city |
| 268 | + input :country |
| 269 | + end |
| 270 | + ``` |
| 271 | +
|
| 272 | + or pass in options: |
| 273 | + ```ruby |
| 274 | + nested_input :#{name}, using: #{name.to_s.classify}Definition, fields: %i[city country] |
| 275 | + ``` |
| 276 | + ) |
| 277 | + end |
| 278 | + end |
| 279 | + end |
| 280 | + end |
| 281 | + end |
| 282 | +end |
0 commit comments