Class: ForestAdminDatasourceMambuPayments::Plugins::Relations::TwoStepLinkPlugin

Inherits:
ForestAdminDatasourceCustomizer::Plugins::Plugin
  • Object
show all
Defined in:
lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_link_plugin.rb

Overview

Base for relations whose foreign key is not native: the ‘filtered` collection gets a virtual (always-nil) FK column plus a two-step operator filter that rewrites EQUAL/IN predicates, and the `owner` collection gets the reciprocal OneToMany. Subclasses declare the shape and provide the concrete filter install:

class LinkInternalAccountToBalances < TwoStepLinkPlugin
  link owner: 'MambuInternalAccount', filtered: 'MambuBalance',
       name: 'balances', fk: 'internal_account_id'
  def install_source_filter(collection)
    TwoStepConnectedAccountFilter.install(collection, target_field: 'connected_account_id')
  end
end

Install at the datasource level: @agent.use(plugin, {}).

Constant Summary collapse

ComputedDefinition =
ForestAdminDatasourceCustomizer::Decorators::Computed::ComputedDefinition

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_link_plugin.rb', line 23

def config
  @config
end

Class Method Details



26
27
28
# File 'lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_link_plugin.rb', line 26

def self.link(owner:, filtered:, name:, foreign_key:)
  @config = { owner: owner, filtered: filtered, name: name, fk: foreign_key }
end

.virtual_fkObject

The virtual FK is nil per record: a reverse lookup would require scanning the pivot/intermediate collection. Only EQUAL/IN filters are meaningful, and those are rewritten by the source filter.



33
34
35
36
37
38
39
# File 'lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_link_plugin.rb', line 33

def self.virtual_fk
  ComputedDefinition.new(
    column_type: 'String',
    dependencies: ['id'],
    values: proc { |records, _ctx| records.map { nil } }
  )
end

Instance Method Details

#install_source_filter(_collection) ⇒ Object

Installs the operator filter that rewrites the virtual FK predicate.

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_link_plugin.rb', line 58

def install_source_filter(_collection)
  raise NotImplementedError, "#{self.class} must implement #install_source_filter"
end

#run(datasource_customizer, _collection_customizer = nil, _options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/forest_admin_datasource_mambu_payments/plugins/relations/two_step_link_plugin.rb', line 41

def run(datasource_customizer, _collection_customizer = nil, _options = {})
  Plugins::Helpers.require_datasource!(datasource_customizer, self.class)

  cfg = self.class.config
  plugin = self
  datasource_customizer.customize_collection(cfg[:filtered]) do |c|
    c.add_field(cfg[:fk], TwoStepLinkPlugin.virtual_fk)
    plugin.install_source_filter(c)
  end

  datasource_customizer.customize_collection(cfg[:owner]) do |c|
    c.add_one_to_many_relation(cfg[:name], cfg[:filtered],
                               origin_key: cfg[:fk], origin_key_target: 'id')
  end
end