Skip to content

Commit

Permalink
refactor: to reduce cognitive complexity
Browse files Browse the repository at this point in the history
Signed-off-by: gardar <[email protected]>
  • Loading branch information
gardar committed May 15, 2024
1 parent d4f7e1a commit ce46349
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions app/controllers/api/v2/ansible_override_values_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class AnsibleOverrideValuesController < ::Api::V2::BaseController
param_group :ansible_override_value, :as => :create

def create
@ansible_variable = AnsibleVariable.authorized(:edit_ansible_variables).
find_by(:id => params[:ansible_variable_id].to_i)
@ansible_variable = find_ansible_variable
return not_found("Ansible variable not found") unless @ansible_variable

@override_value = @ansible_variable.lookup_values.create!(lookup_value_params['override_value'])
@ansible_variable.update_attribute(:override, true)
render 'api/v2/ansible_override_values/show'
Expand All @@ -36,11 +37,10 @@ def create
param_group :ansible_override_value, :as => :update

def update
@ansible_variable = AnsibleVariable.authorized(:edit_ansible_variables).
find_by(:id => params[:ansible_variable_id].to_i)
@ansible_variable = find_ansible_variable
return not_found("Ansible variable not found") unless @ansible_variable

@override_value = @ansible_variable.lookup_values.find_by(match: lookup_value_params['override_value']['match'])
@override_value = find_override_value
return not_found("Override value not found") unless @override_value

if @override_value.update(lookup_value_params['override_value'])
Expand All @@ -64,6 +64,20 @@ def destroy
end
end

private

def find_ansible_variable
AnsibleVariable.authorized(:edit_ansible_variables).find_by(id: params[:ansible_variable_id].to_i)
end

def find_override_value
@ansible_variable.lookup_values.find_by(match: lookup_value_params['override_value']['match'])
end

def lookup_value_params
params.permit(:ansible_variable_id, override_value: [:match, :value])
end

def resource_name
'ansible_variable'
end
Expand Down

0 comments on commit ce46349

Please sign in to comment.