Skip to content
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ Metrics/MethodLength:
- 'packages/forest_admin_datasource_rpc/lib/forest_admin_datasource_rpc/collection.rb'
- 'packages/forest_admin_datasource_rpc/lib/forest_admin_datasource_rpc.rb'
- 'packages/forest_admin_datasource_rpc/lib/forest_admin_datasource_rpc/Utils/sse_client.rb'
- 'packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/agent.rb'
- 'packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/routes/sse.rb'
- 'packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/http/router.rb'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module ForestAdminDatasourceCustomizer
class CompositeDatasource
attr_reader :datasources

def initialize
@datasources = []
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ForestAdminDatasourceCustomizer
class DatasourceCustomizer
include DSL::DatasourceHelpers
attr_reader :stack, :datasources
attr_reader :stack, :composite_datasource

def initialize(_db_config = {})
@composite_datasource = ForestAdminDatasourceCustomizer::CompositeDatasource.new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def self.build(options)
'Fatal: Unable to build RPC datasource - no introspection schema was provided and schema fetch failed'
end

options.delete(:introspection)
ForestAdminDatasourceRpc::Datasource.new(options, schema, schema_polling)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ForestAdminDatasourceRpc
class Datasource < ForestAdminDatasourceToolkit::Datasource
include ForestAdminDatasourceRpc::Utils

attr_reader :shared_rpc_client
attr_reader :shared_rpc_client, :rpc_relations

def initialize(options, introspection, schema_polling_client = nil)
super()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module ForestAdminDatasourceRpc
class ReconciliateRpc < ForestAdminDatasourceCustomizer::Plugins::Plugin
def run(datasource_customizer, _collection_customizer = nil, options = {})
datasource_customizer.composite_datasource.datasources.each do |datasource|
real_datasource = get_datasource(datasource)
next unless real_datasource.is_a?(ForestAdminDatasourceRpc::Datasource)

# Disable search for non-searchable collections
real_datasource.collections.each_value do |collection|
unless collection.schema[:searchable]
cz = datasource_customizer.get_collection(get_collection_name(options[:rename], collection.name))
cz.disable_search
end
end

# Add relations from rpc_relations
(real_datasource.rpc_relations || {}).each do |collection_name, relations|
collection_name = get_collection_name(options[:rename], collection_name)
cz = datasource_customizer.get_collection(collection_name)

relations.each do |relation_name, relation_definition|
add_relation(cz, options[:rename], relation_name.to_s, relation_definition)
end
end
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function with high complexity (count = 7): run [qlty:function-complexity]

end

private

def get_datasource(datasource)
# can be publication -> rename deco or a custom one
while datasource.is_a?(ForestAdminDatasourceToolkit::Decorators::DatasourceDecorator)
datasource = datasource.child_datasource
end

datasource
end

def get_collection_name(renames, collection_name)
name = collection_name

if renames.is_a?(Proc)
name = renames.call(collection_name)
elsif renames.is_a?(Hash) && renames.key?(collection_name.to_s)
name = renames[collection_name.to_s]
end

name
end

def add_relation(collection_customizer, renames, relation_name, relation_definition)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function with many parameters (count = 4): add_relation [qlty:function-parameters]

relation = relation_definition.transform_keys(&:to_sym)
foreign_collection = get_collection_name(renames, relation[:foreign_collection])
options = relation.except(:type, :foreign_collection, :through_collection)

case relation[:type]
when 'ManyToMany'
through_collection = get_collection_name(renames, relation[:through_collection])
collection_customizer.add_many_to_many_relation(relation_name, foreign_collection, through_collection, options)
when 'OneToMany'
collection_customizer.add_one_to_many_relation(relation_name, foreign_collection, options)
when 'OneToOne'
collection_customizer.add_one_to_one_relation(relation_name, foreign_collection, options)
when 'ManyToOne'
collection_customizer.add_many_to_one_relation(relation_name, foreign_collection, options)
else
raise ForestAdminDatasourceToolkit::Exceptions::ForestException, "Unsupported relation type: #{relation[:type]}"
end
end
end
end
Loading