Module: LcpRuby::ModelFactory::LabelMethodBuilder

Defined in:
lib/lcp_ruby/model_factory/label_method_builder.rb

Overview

Shared by Builder, ApiBuilder, and Engine’s bind_to path so all three strategies resolve label_method identically. Dot-paths short-circuit to nil when an intermediate association is missing (rather than raising).

When ‘model_def:` is supplied (always passed by the engine + builders), enum-typed terminal fields route the raw value through `Metadata::EnumLabelResolver` so `to_label` returns localized labels (“Otec”) instead of raw enum keys (“father”). The metadata chain is walked at definition time and captured in the closure — runtime cost is one extra conditional and (for enum hits) one I18n lookup.

Class Method Summary collapse

Class Method Details

.define(model_class, label_attr, model_def: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lcp_ruby/model_factory/label_method_builder.rb', line 16

def define(model_class, label_attr, model_def: nil)
  return if label_attr.nil?

  attr_str = label_attr.to_s
  # to_s defers to AR default; to_label would recurse into itself.
  return if attr_str == "to_s" || attr_str == "to_label"

  if attr_str.include?(".")
    define_dot_path(model_class, attr_str, model_def)
  else
    define_single_segment(model_class, attr_str, model_def)
  end
end