diff options
Diffstat (limited to 'app/models/route.rb')
-rw-r--r-- | app/models/route.rb | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/app/models/route.rb b/app/models/route.rb index dd171fdb069..4eec3e73d5b 100644 --- a/app/models/route.rb +++ b/app/models/route.rb @@ -8,16 +8,22 @@ class Route < ActiveRecord::Base presence: true, uniqueness: { case_sensitive: false } - after_update :rename_descendants, if: :path_changed? + after_update :rename_descendants def rename_descendants - # We update each row separately because MySQL does not have regexp_replace. - # rubocop:disable Rails/FindEach - Route.where('path LIKE ?', "#{path_was}/%").each do |route| - # Note that update column skips validation and callbacks. - # We need this to avoid recursive call of rename_descendants method - route.update_column(:path, route.path.sub(path_was, path)) + if path_changed? || name_changed? + descendants = Route.where('path LIKE ?', "#{path_was}/%") + + descendants.each do |route| + attributes = { + path: route.path.sub(path_was, path), + name: route.name.sub(name_was, name) + } + + # Note that update_columns skips validation and callbacks. + # We need this to avoid recursive call of rename_descendants method + route.update_columns(attributes) + end end - # rubocop:enable Rails/FindEach end end |