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

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Instance Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Instance Method Details

#conflicting_redirectsObject


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

def conflicting_redirects
  RedirectRoute.matching_path_and_descendants(path)
end

#create_redirect(path) ⇒ Object


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

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

#delete_conflicting_redirectsObject


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

def delete_conflicting_redirects
  conflicting_redirects.delete_all
end

#rename_descendantsObject


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
55
# File 'app/models/route.rb', line 27

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