Refactor #18982

Refactor params hash iteration in Api::CompatibilityChecker#check_create_host_nested

Added by Dominic Cleal about 1 year ago. Updated 6 months ago.

Status:Closed
Priority:Low
Assigned To:Dominic Cleal
Category:Rails
Target version:-
Difficulty: Bugzilla link:
Found in release: Pull request:https://github.com/theforeman/foreman/pull/4399
Story points-
Velocity based estimate-
Release1.16.0Release relationshipAuto

Description

Api::CompatibilityChecker#check_create_host_nested iterates over nested attributes in a params hash and should work for both arrays and hash styles of nested attrs, but it does so using a single each call yielding a single argument.

This works on Rails 4 where ActionController::Parameters inherits each from a Hash, as an array of [key, value] would be yielded instead, which the block checks for (attribute[1] if attribute.is_a?(Array)).

In Rails 5.0, AC::Parameters has its own each implementation that only yields to two arguments. Using each do |attribute| will instead be yielded only the key name. This causes:

Api::V2::HostsControllerTest#test_0010_should create host with host_parameters_attributes sent in a hash [test/controllers/api/v2/hosts_controller_test.rb:186]:
"Host.count" didn't change by 1.
Expected: 2
  Actual: 1

The iteration should be handled differently for arrays and AC::Parameters/hashes.


Related issues

Blocks Foreman - Feature #19050: Add support for running under Ruby on Rails 5.0 Closed 03/28/2017

Associated revisions

Revision c1685291
Added by Dominic Cleal about 1 year ago

fixes #18982 - replace AC::Params#each using one-arg block

ActionController::Parameters#each works differently in Rails 5.0: it no
longer yields an array of [key, value] entries for hashes, only the
key when given a block with arity of one. This method now iterates over
only the values when passed a hash or AC::Parameters instance.

History

#1 Updated by The Foreman Bot about 1 year ago

  • Status changed from Assigned to Ready For Testing
  • Pull request https://github.com/theforeman/foreman/pull/4399 added

#2 Updated by Dominic Cleal about 1 year ago

  • Blocks Feature #19050: Add support for running under Ruby on Rails 5.0 added

#3 Updated by Dominic Cleal about 1 year ago

  • Status changed from Ready For Testing to Closed
  • % Done changed from 0 to 100

#4 Updated by Marek Hulán 6 months ago

  • Release set to 1.16.0

Also available in: Atom PDF