Module: AdminSuite::UI::ShowValueFormatter

Included in:
BaseHelper
Defined in:
lib/admin_suite/ui/show_value_formatter.rb

Overview

Overrides ‘format_show_value` to use a registry of show value formatters, while leaving the legacy implementation available via `super`.

Instance Method Summary collapse

Instance Method Details

#format_show_value(record, field_name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/admin_suite/ui/show_value_formatter.rb', line 10

def format_show_value(record, field_name)
  value = record.public_send(field_name) rescue nil

  if (field_def = admin_suite_field_definition(field_name))
    case field_def.type
    when :markdown
      rendered =
        if defined?(::MarkdownRenderer)
          ::MarkdownRenderer.render(value.to_s)
        else
          simple_format(value.to_s)
        end
      return (:div, rendered, class: "prose dark:prose-invert max-w-none")
    when :json
      begin
        parsed =
          if value.is_a?(Hash) || value.is_a?(Array)
            value
          elsif value.present?
            JSON.parse(value.to_s)
          end
        return render_json_block(parsed) if parsed
      rescue JSON::ParserError
        # fall through
      end
    when :label
      return render_label_badge(value, color: field_def.label_color, size: field_def.label_size, record: record)
    end
  end

  # If the field isn't in the form config, fall back to index column config
  # so show pages can still render labels consistently.
  if respond_to?(:resource_config, true) && (rc = resource_config) && rc.index_config&.columns_list
    col = rc.index_config.columns_list.find { |c| c.name.to_sym == field_name.to_sym }
    if col&.type == :label
      label_value = col.content.is_a?(Proc) ? col.content.call(record) : value
      return render_label_badge(label_value, color: col.label_color, size: col.label_size, record: record)
    end
  end

  if value.is_a?(ActiveStorage::Attached::One)
    return render_attachment_preview(value)
  elsif value.is_a?(ActiveStorage::Attached::Many)
    return render_attachments_preview(value)
  end

  formatted =
    AdminSuite::UI::ShowFormatterRegistry.format(
      value,
      view: self,
      record: record,
      field_name: field_name
    )

  return formatted unless formatted.nil?

  super
end