Class: Route

Inherits:
ApplicationRecord show all
Includes:
CaseSensitivity, Gitlab::SQL::Pattern
Defined in:
app/models/route.rb

Constant Summary

Constants included from Gitlab::SQL::Pattern

Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_WORD

Instance Method Summary collapse

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, where_exists, with_fast_statement_timeout, without_order

Instance Method Details

#conflicting_redirectsObject


60
61
62
# File 'app/models/route.rb', line 60

def conflicting_redirects
  RedirectRoute.matching_path_and_descendants(path)
end

#create_redirect(path) ⇒ Object


64
65
66
# File 'app/models/route.rb', line 64

def create_redirect(path)
  RedirectRoute.create(source: source, path: path)
end

#delete_conflicting_redirectsObject


56
57
58
# File 'app/models/route.rb', line 56

def delete_conflicting_redirects
  conflicting_redirects.delete_all
end

#rename_descendantsObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/route.rb', line 26

def rename_descendants
  return unless saved_change_to_path? || saved_change_to_name?

  descendant_routes = self.class.inside_path(path_before_last_save)

  descendant_routes.each do |route|
    attributes = {}

    if saved_change_to_path? && route.path.present?
      attributes[:path] = route.path.sub(path_before_last_save, path)
    end

    if saved_change_to_name? && name_before_last_save.present? && route.name.present?
      attributes[:name] = route.name.sub(name_before_last_save, name)
    end

    if attributes.present?
      old_path = route.path

      # Callbacks must be run manually
      route.update_columns(attributes.merge(updated_at: Time.current))

      # We are not calling route.delete_conflicting_redirects here, in hopes
      # of avoiding deadlocks. The parent (self, in this method) already
      # called it, which deletes conflicts for all descendants.
      route.create_redirect(old_path) if attributes[:path]
    end
  end
end