Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocorrect for Rails/StrongParametersExpect fails for accepts_nested_attributes_for params #1429

Open
chaadow opened this issue Jan 25, 2025 · 1 comment · May be fixed by #1430
Open

Autocorrect for Rails/StrongParametersExpect fails for accepts_nested_attributes_for params #1429

chaadow opened this issue Jan 25, 2025 · 1 comment · May be fixed by #1430

Comments

@chaadow
Copy link

chaadow commented Jan 25, 2025


Expected behavior

I expect this :

    params.require(:company_tool).permit(
      :salary,
      :employees_count,
      rows_prices: {
        row_total_one_employee: [
          :price,
          :cell_id
        ],
        row_total_all_employees: [
          :price,
          :cell_id
        ]
      }

to be autocorrected with :

# Note the double array `[[ ...]]`
    params.expect(
      company_tool: [:salary,
                     :employees_count,
                     rows_prices: [
                        row_total_one_employee: [[ # double array
                         :price,
                         :cell_id
                        ]],
                        row_total_all_employees: [[
                           :price,
                           :cell_id
                        ]]
                     ]
           ]
    )

Actual behavior

it gets autocorrected to this :

    params.expect(
      company_tool: [:salary,
                     :employees_count,
                     {  # unnecessary opening of hash
                        rows_prices: { # Here it's better to open with `[` than `{` according to docs
                           row_total_one_employee: [ # no double array
                              :price,
                              :cell_id
                       ],
                       row_total_all_employees: [ # no double array
                         :price,
                         :cell_id
                       ]
                     }
               }
     ]
    )

which is false, and make row_prices empty when provided with an array

Steps to reproduce the problem

To reproduce you can put the examples above in any controller and you will get the same result

Example can also be found here

RuboCop version

$ [bundle exec] rubocop -V
1.71.0 (using Prism 1.3.0, rubocop-ast 1.37.0, analyzing as Ruby 3.4, running on ruby 3.4.1) [arm64-darwin24]
  - rubocop-capybara 2.21.0
  - rubocop-factory_bot 2.26.1
  - rubocop-graphql 1.5.4
  - rubocop-performance 1.23.1
  - rubocop-rails 2.29.1
  - rubocop-rspec 3.4.0
  - rubocop-rspec_rails 2.30.0

@chaadow
Copy link
Author

chaadow commented Jan 25, 2025

I also feel that using hashes in autocorrect is most of the time not necessary except for this case :

Example 4: Imagine a scenario where you have parameters representing a product name, and a hash of arbitrary data associated with that product, and you want to permit the product name attribute and also the whole data hash:

def product_params
  params.expect(product: [ :name, data: {} ])
end

chaadow added a commit to chaadow/rubocop-rails that referenced this issue Jan 26, 2025
Fixes rubocop#1429

Use a recursive approach to autocorrect nested parameters
- Top level hash keys do not get replaced by `[]`
- Hash values that are hashes get replaced by `[]`
- Arrays get replaced by double brackets `[[]]`
chaadow added a commit to chaadow/rubocop-rails that referenced this issue Jan 26, 2025
Fixes rubocop#1429

Use a recursive approach to autocorrect nested parameters
- Top level hash keys do not get replaced by `[]`
- Hash values that are hashes get replaced by `[]`
- Arrays get replaced by double brackets `[[]]`
@chaadow chaadow linked a pull request Jan 26, 2025 that will close this issue
9 tasks
chaadow added a commit to chaadow/rubocop-rails that referenced this issue Jan 26, 2025
Fixes rubocop#1429

Use a recursive approach to autocorrect nested parameters
- Top level hash keys do not get replaced by `[]`
- Hash values that are hashes get replaced by `[]`
- Arrays get replaced by double brackets `[[]]`
chaadow added a commit to chaadow/rubocop-rails that referenced this issue Jan 26, 2025
Fixes rubocop#1429

Use a recursive approach to autocorrect nested parameters
- Top level hash keys do not get replaced by `[]`
- Hash values that are hashes get replaced by `[]`
- Arrays get replaced by double brackets `[[]]`
chaadow added a commit to chaadow/rubocop-rails that referenced this issue Jan 26, 2025
Fixes rubocop#1429

Use a recursive approach to autocorrect nested parameters
- Top level hash keys do not get replaced by `[]`
- Hash values that are hashes get replaced by `[]`
- Arrays get replaced by double brackets `[[]]`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant