Module: LcpRuby::DisplayHelper

Defined in:
app/helpers/lcp_ruby/display_helper.rb

Instance Method Summary collapse

Instance Method Details

#compute_item_classes(record, presenter) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/lcp_ruby/display_helper.rb', line 49

def compute_item_classes(record, presenter)
  rules = presenter.item_classes
  return "" if rules.empty?

  ctx = condition_context
  matching = rules.filter_map do |rule|
    ConditionEvaluator.evaluate_any(record, rule["when"], context: ctx) ? rule["class"] : nil
  rescue ConditionError => e
    raise unless Rails.env.production?
    LcpRuby.record_error(e, subsystem: "condition_eval", presenter: presenter.name)
    nil
  end
  matching.join(" ")
end

#current_model_nameObject



77
78
79
# File 'app/helpers/lcp_ruby/display_helper.rb', line 77

def current_model_name
  current_model_definition&.name if respond_to?(:current_model_definition)
end

#display_association_value(value) ⇒ Object

Default rendering for association-valued show fields / index columns (plain belongs_to, terminal-belongs_to dot-path) without an explicit renderer or partial. Non-AR values pass through untouched.



84
85
86
87
88
# File 'app/helpers/lcp_ruby/display_helper.rb', line 84

def display_association_value(value)
  return value unless value.is_a?(ActiveRecord::Base)

  LcpRuby::Presenter::FieldValueResolver.association_label(value)
end

#empty_value_placeholder(value, presenter = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/lcp_ruby/display_helper.rb', line 64

def empty_value_placeholder(value, presenter = nil)
  return value if value == false || value == 0

  if value.nil? || (value.respond_to?(:empty?) && value.empty?) || (value.is_a?(String) && value.strip.empty?)
    text = presenter&.options&.dig("empty_value") ||
           LcpRuby.configuration.empty_value ||
           I18n.t("lcp_ruby.empty_value", default: "\u2014")
    (:span, text, class: "lcp-empty-value")
  else
    value
  end
end

#format_enum_display(value, field_def, model_name = nil) ⇒ Object

Humanize an enum value for display. Scalars route through ‘enum_label_for`; Arrays (has_many → terminal enum dot-path) humanize each element so the collection renderer / clipboard copy show labels, not raw keys (issue #18). Non-enum field defs and blank values pass through unchanged.



35
36
37
38
39
# File 'app/helpers/lcp_ruby/display_helper.rb', line 35

def format_enum_display(value, field_def, model_name = nil)
  return value unless field_def&.enum? && value.present?
  return humanize_enum_array(value, field_def, model_name) if value.is_a?(Array)
  field_def.enum_label_for(value, model_name: model_name)
end

#humanize_enum_array(values, field_def, model_name) ⇒ Object

Map each element of an enum-valued Array through ‘enum_label_for`, leaving blanks untouched. Shared by `render_display_value` and `format_enum_display` so on-table, in-card, and clipboard rendering of has_many enum dot-paths stay consistent.



45
46
47
# File 'app/helpers/lcp_ruby/display_helper.rb', line 45

def humanize_enum_array(values, field_def, model_name)
  values.map { |v| v.blank? ? v : field_def.enum_label_for(v, model_name: model_name) }
end

#render_display_value(value, renderer_key, options = {}, field_def = nil, record: nil, model_name: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/lcp_ruby/display_helper.rb', line 3

def render_display_value(value, renderer_key, options = {}, field_def = nil, record: nil, model_name: nil)
  return value if renderer_key.blank?

  renderer = LcpRuby::Display::RendererRegistry.renderer_for(renderer_key.to_s)
  return value unless renderer

  effective_options = options || {}
  if field_def&.enum?
    # For dot-path columns the i18n namespace is the target model, not the
    # presenter's current model. Caller passes model_name: explicitly when
    # the field def comes from a different model than the page is showing.
    enum_model = model_name || current_model_name
    if value.is_a?(Array)
      # has_many → terminal enum dot-path (e.g. `tasks.status`) resolves to
      # an Array of raw keys; humanize each element so list renderers
      # (`collection`) emit labels, not raw keys (issue #18). The scalar
      # `label` injection below can't help here — list renderers render
      # each element, not a single pre-labeled value.
      value = humanize_enum_array(value, field_def, enum_model)
    elsif !effective_options.key?("label")
      effective_options = effective_options.merge("label" => field_def.enum_label_for(value, model_name: enum_model))
    end
  end

  renderer.render(value, effective_options, record: record, view_context: self)
end