2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class AttributesFinder
|
2021-06-08 01:23:25 +05:30
|
|
|
attr_reader :tree, :included_attributes, :excluded_attributes, :methods, :preloads, :export_reorders
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def initialize(config:)
|
|
|
|
@tree = config[:tree] || {}
|
|
|
|
@included_attributes = config[:included_attributes] || {}
|
|
|
|
@excluded_attributes = config[:excluded_attributes] || {}
|
|
|
|
@methods = config[:methods] || {}
|
|
|
|
@preloads = config[:preloads] || {}
|
2021-06-08 01:23:25 +05:30
|
|
|
@export_reorders = config[:export_reorders] || {}
|
2022-10-11 01:57:18 +05:30
|
|
|
@include_if_exportable = config[:include_if_exportable] || {}
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def find_root(model_key)
|
|
|
|
find(model_key, @tree[model_key])
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def find_relations_tree(model_key)
|
|
|
|
@tree[model_key]
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def find_excluded_keys(klass_name)
|
|
|
|
@excluded_attributes[klass_name.to_sym]&.map(&:to_s) || []
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def find(model_key, model_tree)
|
|
|
|
{
|
|
|
|
only: @included_attributes[model_key],
|
|
|
|
except: @excluded_attributes[model_key],
|
|
|
|
methods: @methods[model_key],
|
|
|
|
include: resolve_model_tree(model_tree),
|
2021-06-08 01:23:25 +05:30
|
|
|
preload: resolve_preloads(model_key, model_tree),
|
2022-10-11 01:57:18 +05:30
|
|
|
export_reorder: @export_reorders[model_key],
|
|
|
|
include_if_exportable: @include_if_exportable[model_key]
|
2019-12-04 20:38:33 +05:30
|
|
|
}.compact
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def resolve_preloads(model_key, model_tree)
|
|
|
|
model_tree
|
|
|
|
.map { |submodel_key, submodel_tree| resolve_preload(model_key, submodel_key, submodel_tree) }
|
2020-04-22 19:07:51 +05:30
|
|
|
.tap { |entries| entries.compact! }
|
2019-12-04 20:38:33 +05:30
|
|
|
.to_h
|
|
|
|
.deep_merge(@preloads[model_key].to_h)
|
|
|
|
.presence
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def resolve_preload(parent_model_key, model_key, model_tree)
|
|
|
|
return if @methods[parent_model_key]&.include?(model_key)
|
|
|
|
|
|
|
|
[model_key, resolve_preloads(model_key, model_tree)]
|
2018-06-03 19:52:53 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def resolve_model_tree(model_tree)
|
|
|
|
return unless model_tree
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
model_tree
|
|
|
|
.map(&method(:resolve_model))
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def resolve_model(model_key, model_tree)
|
|
|
|
{ model_key => find(model_key, model_tree) }
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|