Class: LcpRuby::Import::Setup

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

Constant Summary collapse

STRATEGIES =
%w[create_only update_only upsert skip_existing].freeze

Class Method Summary collapse

Class Method Details

.apply!(loader) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/lcp_ruby/import/setup.rb', line 6

def self.apply!(loader)
  register_virtual_model(loader)
  register_dialog_presenter(loader)
  register_dialog_page(loader)

  Events::HandlerRegistry.register("import_config", "dialog_submit", ImportDialogHandler)

  install_blob_cleanup_callback!
  install_reverse_associations!(loader)
end

.blob_cleanup_for(job_execution) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/lcp_ruby/import/setup.rb', line 72

def self.blob_cleanup_for(job_execution)
  return unless job_execution.job_type == Import::JOB_TYPE

  blob_key = job_execution.params&.dig("source_blob_id")
  return unless blob_key.present?

  ActiveStorage::Blob.find_by(key: blob_key)&.purge
end

.install_blob_cleanup_callback!Object



17
18
19
20
21
22
23
24
25
# File 'lib/lcp_ruby/import/setup.rb', line 17

def self.install_blob_cleanup_callback!
  exec_model_name = LcpRuby.configuration.job_execution_model
  model_class = LcpRuby.registry.model_for(exec_model_name)
  model_class.before_destroy do
    LcpRuby::Import::Setup.blob_cleanup_for(self)
  end
rescue LcpRuby::Error
  # job_execution model not available (generator not run) — skip
end

.install_reverse_associations!(loader) ⇒ Object

Adds has_many :import_rows (polymorphic) to every model whose presenter has import enabled. Also patches ransackable_associations so Ransack exposes the association for advanced-search filtering.



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
# File 'lib/lcp_ruby/import/setup.rb', line 30

def self.install_reverse_associations!(loader)
  return unless loader.model_definitions.key?("import_row")

  import_row_class_name = "LcpRuby::Dynamic::ImportRow"

  target_models = Set.new
  loader.presenter_definitions.each_value do |presenter|
    next unless presenter.import_enabled?
    target_models << presenter.model
  end

  target_models.each do |model_name|
    model_def = loader.model_definitions[model_name]
    next unless model_def

    model_class = LcpRuby.registry.model_for(model_name)
    next if model_class.reflect_on_association(:import_rows)

    # 1. AR has_many
    model_class.has_many :import_rows,
                         as: :target_record,
                         class_name: import_row_class_name,
                         dependent: nil

    # 2. AssociationDefinition for FilterMetadataBuilder
    assoc_def = Metadata::AssociationDefinition.new(
      type: "has_many",
      name: "import_rows",
      target_model: "import_row",
      as: "target_record",
      owner_name: model_name,
      required: false
    )
    model_def.associations << assoc_def

    # 3. Patch ransackable_associations (closure was frozen at boot)
    patch_ransackable_associations!(model_class, model_def)
  end
rescue StandardError => e
  Rails.logger.warn("[LcpRuby] Import: could not install reverse associations: #{e.message}")
end