Class: BulkImports::Common::Transformers::UserReferenceTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/bulk_imports/common/transformers/user_reference_transformer.rb

Constant Summary collapse

DEFAULT_REFERENCE =
'user'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UserReferenceTransformer

Returns a new instance of UserReferenceTransformer.


14
15
16
17
# File 'lib/bulk_imports/common/transformers/user_reference_transformer.rb', line 14

def initialize(options = {})
  @reference = options[:reference].to_s.presence || DEFAULT_REFERENCE
  @suffixed_reference = "#{@reference}_id"
end

Instance Method Details

#transform(context, data) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'lib/bulk_imports/common/transformers/user_reference_transformer.rb', line 19

def transform(context, data)
  return unless data

  user = find_user(context, data&.dig(@reference, 'public_email')) || context.current_user

  data
    .except(@reference)
    .merge(@suffixed_reference => user.id)
end