Class: LcpRuby::Import::FieldTreeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/import/field_tree_builder.rb

Overview

Builds the field metadata tree for import target fields. Returns writable fields (unlike Export::FieldTreeBuilder which returns readable fields).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_definition:, evaluator:, presenter:, current_user:) ⇒ FieldTreeBuilder

Returns a new instance of FieldTreeBuilder.



10
11
12
13
14
15
# File 'lib/lcp_ruby/import/field_tree_builder.rb', line 10

def initialize(model_definition:, evaluator:, presenter:, current_user:)
  @model_definition = model_definition
  @evaluator = evaluator
  @presenter = presenter
  @current_user = current_user
end

Class Method Details

.build(model_definition:, evaluator:, presenter:, current_user:) ⇒ Object



6
7
8
# File 'lib/lcp_ruby/import/field_tree_builder.rb', line 6

def self.build(model_definition:, evaluator:, presenter:, current_user:)
  new(model_definition:, evaluator:, presenter:, current_user:).build
end

Instance Method Details

#buildObject



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lcp_ruby/import/field_tree_builder.rb', line 17

def build
  writable = @evaluator.writable_fields
  fields = []
  custom_fields = []

  @model_definition.fields.each do |field_def|
    next unless writable.include?(field_def.name)
    next if skip_field?(field_def)

    entry = build_field_entry(field_def)

    if field_def.name.start_with?("cf_")
      custom_fields << entry
    else
      fields << entry
    end
  end

  # Add FK fields for writable belongs_to associations
  @model_definition.associations.each do |assoc|
    next unless assoc.type == "belongs_to" && assoc.foreign_key
    next unless writable.include?(assoc.foreign_key) || @evaluator.field_writable?(assoc.foreign_key)

    fields << build_association_entry(assoc)
  end

  # Add fields from has_one associations with nested_attributes
  nested_whitelist = @presenter&.import_nested_associations

  @model_definition.associations.each do |assoc|
    next unless assoc.type == "has_one" && assoc.nested_attributes && !assoc.through?
    next if nested_whitelist && !nested_whitelist.include?(assoc.name)

    target_model_def = begin
      LcpRuby.loader.model_definition(assoc.target_model)
    rescue LcpRuby::MetadataError
      next
    end

    target_perm = LcpRuby.loader.permission_definition(assoc.target_model)
    target_evaluator = Authorization::PermissionEvaluator.new(
      target_perm, @current_user, assoc.target_model
    )

    nested_fields = target_model_def.fields
      .reject { |f| skip_field?(f) }
      .select { |f| target_evaluator.field_writable?(f.name) }
      .reject { |f| assoc.foreign_key && f.name == assoc.foreign_key }
      .reject { |f| assoc.as && f.name == "#{assoc.as}_type" }
      .map do |f|
        entry = build_field_entry(f)
        entry["name"] = "#{assoc.name}.#{f.name}"
        assoc_label = I18n.t("lcp_ruby.models.#{assoc.target_model}.one", default: assoc.name.humanize)
        nested_label = f.resolved_label(model_name: assoc.target_model).presence || f.name.humanize
        entry["label"] = "#{assoc_label}#{nested_label}"
        entry["nested_association"] = assoc.name
        entry
      end

    fields.concat(nested_fields) if nested_fields.any?
  end

  # Add dynamic custom fields from registry
  if @model_definition.custom_fields_enabled? && CustomFields::Registry.available?
    cf_writable = writable.include?("custom_data") || writable == "all"
    CustomFields::Registry.for_model(@model_definition.name).each do |cf_def|
      next unless cf_writable || writable.include?(cf_def.field_name)

      custom_fields << {
        "name" => cf_def.field_name,
        "type" => cf_def.custom_type,
        "label" => synthetic_label(cf_def.field_name) ||
                   cf_def.label.presence ||
                   cf_def.field_name.humanize
      }
    end
  end

  result = { "fields" => fields }
  result["custom_fields"] = custom_fields if custom_fields.any?
  result
end