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
diff options
context:
space:
mode:
authorMichael Kozono <mkozono@gmail.com>2017-05-03 20:14:30 +0300
committerMichael Kozono <mkozono@gmail.com>2017-05-05 22:11:58 +0300
commit72872ee2136436e48ce394268fc8bfb8a2118810 (patch)
treeb72060fed4a7458f480991de18ec5a21b9b634bd /app/models
parenta0368e91310a3b2c0e9e0b717f931a482eb0b90a (diff)
Delete conflicting redirects
Diffstat (limited to 'app/models')
-rw-r--r--app/models/redirect_route.rb2
-rw-r--r--app/models/route.rb21
2 files changed, 16 insertions, 7 deletions
diff --git a/app/models/redirect_route.rb b/app/models/redirect_route.rb
index e36ca988701..99812bcde53 100644
--- a/app/models/redirect_route.rb
+++ b/app/models/redirect_route.rb
@@ -7,4 +7,6 @@ class RedirectRoute < ActiveRecord::Base
length: { within: 1..255 },
presence: true,
uniqueness: { case_sensitive: false }
+
+ scope :matching_path_and_descendants, -> (path) { where('redirect_routes.path = ? OR redirect_routes.path LIKE ?', path, "#{sanitize_sql_like(path)}/%") }
end
diff --git a/app/models/route.rb b/app/models/route.rb
index df801714b5f..e0d85ff7db7 100644
--- a/app/models/route.rb
+++ b/app/models/route.rb
@@ -8,7 +8,8 @@ class Route < ActiveRecord::Base
presence: true,
uniqueness: { case_sensitive: false }
- after_update :create_redirect_if_path_changed
+ after_save :delete_conflicting_redirects
+ after_update :create_redirect_for_old_path
after_update :rename_direct_descendant_routes
scope :inside_path, -> (path) { where('routes.path LIKE ?', "#{sanitize_sql_like(path)}/%") }
@@ -34,13 +35,19 @@ class Route < ActiveRecord::Base
end
end
- def create_redirect_if_path_changed
- if path_changed?
- create_redirect(path_was)
- end
+ def delete_conflicting_redirects
+ conflicting_redirects.delete_all
+ end
+
+ def conflicting_redirects
+ RedirectRoute.matching_path_and_descendants(path)
+ end
+
+ def create_redirect_for_old_path
+ create_redirect(path_was) if path_changed?
end
- def create_redirect(old_path)
- source.redirect_routes.create(path: old_path)
+ def create_redirect(path)
+ RedirectRoute.create(source: source, path: path)
end
end