Skip to content

Commit

Permalink
fix codeclimate bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevelito committed Mar 11, 2025
1 parent 0278fca commit 750dd25
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
83 changes: 54 additions & 29 deletions app/controllers/avo/bulk_update_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def handle
if params_to_apply.blank?
flash[:warning] = t("avo.no_changes_made")
redirect_to after_bulk_update_path
return
end

updated_count, failed_records = update_records
Expand All @@ -37,51 +38,71 @@ def handle

def params_to_apply
prefilled_params = params[:prefilled] || {}

resource_key = @resource_name.downcase.to_sym
current_params = params[resource_key] || {}

progress_fields = @resource
.get_field_definitions
.select { |field| field.is_a?(Avo::Fields::ProgressBarField) }
.map(&:id)
.map(&:to_sym)
current_params = current_resource_params
progress_fields = progress_bar_fields

current_params.reject do |key, value|
key_sym = key.to_sym
prefilled_value = prefilled_params[key_sym]

next true if progress_fields.include?(key_sym) && prefilled_value == "" && value.to_s == "50"

prefilled_value.to_s == value.to_s
progress_field_with_default?(progress_fields, key_sym, prefilled_value, value) ||
prefilled_value.to_s == value.to_s
end
end

def current_resource_params
resource_key = @resource_name.downcase.to_sym
params[resource_key] || {}
end

def progress_bar_fields
@resource.get_field_definitions
.select { |field| field.is_a?(Avo::Fields::ProgressBarField) }

Check failure on line 60 in app/controllers/avo/bulk_update_controller.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Layout/MultilineMethodCallIndentation: Use 2 (not 9) spaces for indenting an expression spanning multiple lines. Raw Output: app/controllers/avo/bulk_update_controller.rb:60:16: C: [Corrected] Layout/MultilineMethodCallIndentation: Use 2 (not 9) spaces for indenting an expression spanning multiple lines. .select { |field| field.is_a?(Avo::Fields::ProgressBarField) } ^^^^^^^
.map(&:id)

Check failure on line 61 in app/controllers/avo/bulk_update_controller.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Layout/MultilineMethodCallIndentation: Use 2 (not 9) spaces for indenting an expression spanning multiple lines. Raw Output: app/controllers/avo/bulk_update_controller.rb:61:16: C: [Corrected] Layout/MultilineMethodCallIndentation: Use 2 (not 9) spaces for indenting an expression spanning multiple lines. .map(&:id) ^^^^
.map(&:to_sym)

Check failure on line 62 in app/controllers/avo/bulk_update_controller.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Layout/MultilineMethodCallIndentation: Use 2 (not 9) spaces for indenting an expression spanning multiple lines. Raw Output: app/controllers/avo/bulk_update_controller.rb:62:16: C: [Corrected] Layout/MultilineMethodCallIndentation: Use 2 (not 9) spaces for indenting an expression spanning multiple lines. .map(&:to_sym) ^^^^
end

def progress_field_with_default?(progress_fields, key_sym, prefilled_value, value)
progress_fields.include?(key_sym) && prefilled_value == "" && value.to_s == "50"
end

def update_records
updated_count = 0
failed_records = []

@query.each do |record|
params_to_apply.each do |key, value|
record.public_send(:"#{key}=", value)
rescue => e
puts "Błąd przypisywania pola #{key}: #{e.message}"
end

@resource.fill_record(record, params)

update_record(record, params_to_apply)
if record.save
updated_count += 1
else
failed_records << {record: record, errors: record.errors.full_messages}
add_failed_record(failed_records, record)
end
rescue => e
failed_records << {record: record, errors: [e.message]}
add_failed_record(failed_records, record, e.message)
end

[updated_count, failed_records]
end

def update_record(record, params_to_apply)
params_to_apply.each do |key, value|
record.public_send(:"#{key}=", value)
rescue => e
log_field_assignment_error(key, e.message)
end

@resource.fill_record(record, params)
end

def log_field_assignment_error(key, error_message)
puts "Błąd przypisywania pola #{key}: #{error_message}"
end

def add_failed_record(failed_records, record, error_message = nil)
errors = error_message ? [error_message] : record.errors.full_messages
failed_records << { record: record, errors: errors }

Check failure on line 103 in app/controllers/avo/bulk_update_controller.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside { detected. Raw Output: app/controllers/avo/bulk_update_controller.rb:103:26: C: [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside { detected. failed_records << { record: record, errors: errors } ^

Check failure on line 103 in app/controllers/avo/bulk_update_controller.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside } detected. Raw Output: app/controllers/avo/bulk_update_controller.rb:103:57: C: [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside } detected. failed_records << { record: record, errors: errors } ^
end

def after_bulk_update_path
resources_path(resource: @resource)
end
Expand All @@ -95,17 +116,21 @@ def prefill_fields(records, fields)
end

def set_query
if params[:query].present?
@query = @resource.find_record(params[:query], params: params)
else
resource_ids = action_params[:fields]&.dig(:avo_resource_ids)&.split(",") || []
@query = decrypted_query || (resource_ids.any? ? @resource.find_record(resource_ids, params: params) : [])
end
@query = if params[:query].present?
@resource.find_record(params[:query], params: params)

Check failure on line 120 in app/controllers/avo/bulk_update_controller.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Layout/IndentationWidth: Use 2 (not 11) spaces for indentation. Raw Output: app/controllers/avo/bulk_update_controller.rb:120:7: C: [Corrected] Layout/IndentationWidth: Use 2 (not 11) spaces for indentation. @resource.find_record(params[:query], params: params) ^^^^^^^^^^^
else

Check failure on line 121 in app/controllers/avo/bulk_update_controller.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Layout/ElseAlignment: Align else with @query. Raw Output: app/controllers/avo/bulk_update_controller.rb:121:16: C: [Corrected] Layout/ElseAlignment: Align else with @query. else ^^^^
find_records_by_resource_ids

Check failure on line 122 in app/controllers/avo/bulk_update_controller.rb

View workflow job for this annotation

GitHub Actions / lint / runner / standardrb

[rubocop] reported by reviewdog 🐶 [Corrected] Layout/IndentationWidth: Use 2 (not 11) spaces for indentation. Raw Output: app/controllers/avo/bulk_update_controller.rb:122:7: C: [Corrected] Layout/IndentationWidth: Use 2 (not 11) spaces for indentation. find_records_by_resource_ids ^^^^^^^^^^^
end
end

def find_records_by_resource_ids
resource_ids = action_params[:fields]&.dig(:avo_resource_ids)&.split(",") || []
decrypted_query || (resource_ids.any? ? @resource.find_record(resource_ids, params: params) : [])
end

def set_fields
if @query.blank?
flash[:error] = "Bulk update cannot be performed without records."
flash[:error] = I18n.t("avo.bulk_update_no_records")
redirect_to after_bulk_update_path
else
@fields = @query.first.attributes.keys.index_with { nil }
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ en:
attachment_failed: Failed to attach %{attachment_class}
bulk_update_success: "%{count} records were successfully updated."
bulk_update_failure: "Failed to update %{count} records. Errors: %{errors}"
bulk_update_no_records: Bulk update cannot be performed without records.
cancel: Cancel
choose_a_country: Choose a country
choose_an_option: Choose an option
Expand Down

0 comments on commit 750dd25

Please # to comment.