From ce463498ea4e691ec5f5be08015b78271e7c925b Mon Sep 17 00:00:00 2001 From: gardar Date: Wed, 15 May 2024 19:41:33 +0000 Subject: [PATCH] refactor: to reduce cognitive complexity Signed-off-by: gardar --- .../v2/ansible_override_values_controller.rb | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v2/ansible_override_values_controller.rb b/app/controllers/api/v2/ansible_override_values_controller.rb index 38a4c988..ec79e4e5 100644 --- a/app/controllers/api/v2/ansible_override_values_controller.rb +++ b/app/controllers/api/v2/ansible_override_values_controller.rb @@ -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' @@ -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']) @@ -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