Class: BulkImports::Groups::Loaders::GroupLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/bulk_imports/groups/loaders/group_loader.rb

Constant Summary collapse

GroupCreationError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#load(context, data) ⇒ Object

Raises:


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bulk_imports/groups/loaders/group_loader.rb', line 9

def load(context, data)
  path = data['path']
  current_user = context.current_user
  destination_namespace = context.entity.destination_namespace

  raise(GroupCreationError, 'Path is missing') unless path.present?
  raise(GroupCreationError, 'Destination is not a group') if user_namespace_destination?(destination_namespace)
  raise(GroupCreationError, 'User not allowed to create group') unless user_can_create_group?(current_user, data)
  raise(GroupCreationError, 'Group exists') if group_exists?(destination_namespace, path)

  group = ::Groups::CreateService.new(current_user, data).execute

  raise(GroupCreationError, group.errors.full_messages.to_sentence) if group.errors.any?

  context.entity.update!(group: group)

  group
end