Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorMichael Kozono <mkozono@gmail.com>2017-05-02 02:48:05 +0300
committerMichael Kozono <mkozono@gmail.com>2017-05-05 22:11:58 +0300
commita0368e91310a3b2c0e9e0b717f931a482eb0b90a (patch)
tree7286025822c9e53ed8ff660ecfe276a3ee818034 /app
parent7d02bcd2e0165a90a9f2c1edb34b064ff76afd69 (diff)
Create redirect routes on path change
Diffstat (limited to 'app')
-rw-r--r--app/models/route.rb24
1 files changed, 17 insertions, 7 deletions
diff --git a/app/models/route.rb b/app/models/route.rb
index 4b3efab5c3c..df801714b5f 100644
--- a/app/models/route.rb
+++ b/app/models/route.rb
@@ -8,15 +8,17 @@ class Route < ActiveRecord::Base
presence: true,
uniqueness: { case_sensitive: false }
- after_update :rename_descendants
+ after_update :create_redirect_if_path_changed
+ after_update :rename_direct_descendant_routes
scope :inside_path, -> (path) { where('routes.path LIKE ?', "#{sanitize_sql_like(path)}/%") }
+ scope :direct_descendant_routes, -> (path) { where('routes.path LIKE ? AND routes.path NOT LIKE ?', "#{sanitize_sql_like(path)}/%", "#{sanitize_sql_like(path)}/%/%") }
- def rename_descendants
+ def rename_direct_descendant_routes
if path_changed? || name_changed?
- descendants = self.class.inside_path(path_was)
+ direct_descendant_routes = self.class.direct_descendant_routes(path_was)
- descendants.each do |route|
+ direct_descendant_routes.each do |route|
attributes = {}
if path_changed? && route.path.present?
@@ -27,10 +29,18 @@ class Route < ActiveRecord::Base
attributes[:name] = route.name.sub(name_was, name)
end
- # Note that update_columns skips validation and callbacks.
- # We need this to avoid recursive call of rename_descendants method
- route.update_columns(attributes) unless attributes.empty?
+ route.update(attributes) unless attributes.empty?
end
end
end
+
+ def create_redirect_if_path_changed
+ if path_changed?
+ create_redirect(path_was)
+ end
+ end
+
+ def create_redirect(old_path)
+ source.redirect_routes.create(path: old_path)
+ end
end