Class: LcpRuby::Presenter::FieldValueResolver

Inherits:
Object
  • Object
show all
Includes:
MetadataLookup
Defined in:
lib/lcp_ruby/presenter/field_value_resolver.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_definition, permission_evaluator) ⇒ FieldValueResolver

Returns a new instance of FieldValueResolver.



8
9
10
11
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 8

def initialize(model_definition, permission_evaluator)
  @model_definition = model_definition
  @permission_evaluator = permission_evaluator
end

Instance Attribute Details

#model_definitionObject (readonly)

Returns the value of attribute model_definition.



6
7
8
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 6

def model_definition
  @model_definition
end

#permission_evaluatorObject (readonly)

Returns the value of attribute permission_evaluator.



6
7
8
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 6

def permission_evaluator
  @permission_evaluator
end

Class Method Details

.association_label(record) ⇒ Object

Coerce an associated record to a display string. Callers that get back an AR instance (FK resolution, terminal-belongs_to dot-path, plain belongs_to field) run through this to get a human-readable label.



66
67
68
69
70
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 66

def self.association_label(record)
  return nil if record.nil?

  record.respond_to?(:to_label) ? record.to_label : record.to_s
end

.dot_path?(field) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 55

def self.dot_path?(field)
  field.to_s.include?(".") && !field.to_s.include?("{")
end

.template_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 59

def self.template_field?(field)
  field.to_s.include?("{") && field.to_s.include?("}")
end

Instance Method Details

#resolve(record, field_path, fk_map: {}, humanize_enums: false) ⇒ Object?

Resolve a field value from a record using various path types.

Enum fields return the raw stored key (e.g. ‘“mcp_server”`) by default — not the humanized label. Renderers that need the label read it from `options`, injected by `DisplayHelper#render_display_value`; the no-renderer display path runs through `format_enum_display`. This separation lets renderers like `badge` look up `color_map` without reverse-mapping a humanized string. (Audit #18.)

Callers that want the humanized enum label directly — display templates (‘display_template “status”`), interpolated template strings — pass `humanize_enums: true`. `resolve_template` does this internally regardless of the keyword passed at the top level, because template interpolation is always a display context.

Parameters:

  • record (ActiveRecord::Base)

    the record to resolve from

  • field_path (String)

    field name, dot-path, or template

  • fk_map (Hash) (defaults to: {})

    FK field name => AssociationDefinition (for backward compat)

  • humanize_enums (Boolean) (defaults to: false)

    when true, route enum-typed terminal values through ‘Metadata::EnumLabelResolver` (returns the localized label). Default false — renderers and view dispatch get the raw key.

Returns:

  • (Object, nil)

    resolved value



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lcp_ruby/presenter/field_value_resolver.rb', line 36

def resolve(record, field_path, fk_map: {}, humanize_enums: false)
  field_path = field_path.to_s
  return nil if field_path.blank?

  if self.class.template_field?(field_path)
    resolve_template(record, field_path, fk_map: fk_map)
  elsif self.class.dot_path?(field_path)
    resolve_dot_path(record, field_path, humanize_enums: humanize_enums)
  elsif virtual_column_field?(field_path)
    resolve_virtual_column(record, field_path)
  elsif fk_map.key?(field_path)
    resolve_fk(record, fk_map[field_path])
  elsif (assoc = model_definition.find_belongs_to(field_path))
    resolve_association_record(record, assoc.name)
  else
    resolve_simple(record, field_path, humanize_enums: humanize_enums)
  end
end