debian-mirror-gitlab/lib/gitlab/import_export/attribute_cleaner.rb

38 lines
934 B
Ruby
Raw Normal View History

2018-12-23 12:14:25 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
module Gitlab
module ImportExport
class AttributeCleaner
ALLOWED_REFERENCES = RelationFactory::PROJECT_REFERENCES + RelationFactory::USER_REFERENCES + ['group_id']
def self.clean(*args)
new(*args).clean
end
2018-06-03 19:52:53 +05:30
def initialize(relation_hash:, relation_class:, excluded_keys: [])
2016-11-03 12:29:30 +05:30
@relation_hash = relation_hash
@relation_class = relation_class
2018-06-03 19:52:53 +05:30
@excluded_keys = excluded_keys
2016-11-03 12:29:30 +05:30
end
def clean
@relation_hash.reject do |key, _value|
2018-06-03 19:52:53 +05:30
prohibited_key?(key) || !@relation_class.attribute_method?(key) || excluded_key?(key)
2016-11-03 12:29:30 +05:30
end.except('id')
end
private
def prohibited_key?(key)
key.end_with?('_id') && !ALLOWED_REFERENCES.include?(key)
end
2018-06-03 19:52:53 +05:30
def excluded_key?(key)
return false if @excluded_keys.empty?
@excluded_keys.include?(key)
end
2016-11-03 12:29:30 +05:30
end
end
end