Refactor #16792

Don't modify AR record attributes in-place during normalisation

Added by Dominic Cleal over 1 year ago. Updated over 1 year ago.

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

Description

Rails 5 now prefers to call #freeze on any string passed in as an attribute during validation, but this means attributes must not be edited in-place with gsub! and similar methods. Some of the pre-validation methods used to normalise hostnames, OS names etc will attempt to modify attributes in-place with gsub!, e.g.

HostsControllerTest::Fog.mock!#test_0001_#process_hostgroup changes compute attributes:
RuntimeError: can't modify frozen String
app/models/nic/interface.rb:101:in `gsub!'
app/models/nic/interface.rb:101:in `normalize_name'
app/models/concerns/orchestration.rb:77:in `valid?'
app/models/concerns/orchestration.rb:77:in `valid?'
app/models/concerns/foreman/sti.rb:29:in `save_with_type'
app/models/host.rb:15:in `method_missing'
test/functional/hosts_controller_test.rb:1271:in `initialize_host'

Prefer instead to retrieve and set the attribute through the regular attribute setter API (resource.foo = resource.foo.gsub(..)).

https://github.com/rails/rails/issues/24185 and related tickets have more info.

Associated revisions

Revision 976bf6e0
Added by Dominic Cleal over 1 year ago

fixes #16792 - don't modify AR record attributes in-place

Rails 5 may call #freeze on strings passed in as an attribute during
validation, so attributes must not be edited in-place with gsub! and
similar methods.

History

#1 Updated by The Foreman Bot over 1 year ago

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

#2 Updated by Dominic Cleal over 1 year ago

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

#3 Updated by Dominic Cleal over 1 year ago

  • Release set to 1.15.0

Also available in: Atom PDF