Class: Mutations::Hosts::AssignAnsibleRoles

Inherits:
UpdateMutation
  • Object
show all
Defined in:
app/graphql/mutations/hosts/assign_ansible_roles.rb

Instance Method Summary collapse

Instance Method Details

#resolve(id:, ansible_role_ids:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/graphql/mutations/hosts/assign_ansible_roles.rb', line 10

def resolve(id:, ansible_role_ids:)
  host = load_object_by(id: id)
  authorize!(host, :edit)

  existing = host.host_ansible_roles
  updated_ids = []
  attrs = []

  ansible_role_ids.each do |role_id|
    current = existing.find_by :ansible_role_id => role_id
    attrs << { :id => current&.id, :position => attrs.count + 1, :ansible_role_id => role_id }
    updated_ids << current.id if current
  end

  existing.where.not(:id => updated_ids).each do |item|
    attrs << { :id => item.id, :position => attrs.count + 1, :_destroy => true }
  end

  host.host_ansible_roles_attributes = attrs
  save_object(host)
end

#result_keyObject



32
33
34
# File 'app/graphql/mutations/hosts/assign_ansible_roles.rb', line 32

def result_key
  :host
end