Class: Gitlab::ImportExport::AttributeCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/import_export/attribute_cleaner.rb

Constant Summary collapse

ALLOWED_REFERENCES =
[
  *Gitlab::ImportExport::Project::RelationFactory::PROJECT_REFERENCES,
  *Gitlab::ImportExport::Project::RelationFactory::USER_REFERENCES,
  'group_id',
  'commit_id',
  'discussion_id',
  'custom_attributes'
].freeze
PROHIBITED_REFERENCES =
Regexp.union(
  /\Acached_markdown_version\Z/,
  /_id\Z/,
  /_ids\Z/,
  /_html\Z/,
  /attributes/,
  /\Aremote_\w+_(url|urls|request_header)\Z/ # carrierwave automatically creates these attribute methods for uploads
).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation_hash:, relation_class:, excluded_keys: []) ⇒ AttributeCleaner

Returns a new instance of AttributeCleaner.


27
28
29
30
31
# File 'lib/gitlab/import_export/attribute_cleaner.rb', line 27

def initialize(relation_hash:, relation_class:, excluded_keys: [])
  @relation_hash = relation_hash
  @relation_class = relation_class
  @excluded_keys = excluded_keys
end

Class Method Details

.clean(*args) ⇒ Object


23
24
25
# File 'lib/gitlab/import_export/attribute_cleaner.rb', line 23

def self.clean(*args)
  new(*args).clean
end

Instance Method Details

#cleanObject


33
34
35
36
37
# File 'lib/gitlab/import_export/attribute_cleaner.rb', line 33

def clean
  @relation_hash.reject do |key, _value|
    prohibited_key?(key) || !@relation_class.attribute_method?(key) || excluded_key?(key)
  end.except('id')
end