summaryrefslogtreecommitdiff
path: root/app/models/redirect_route.rb
blob: 2053252734647d8a1fa5714d97bb4ed096c3ca0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class RedirectRoute < ActiveRecord::Base
  belongs_to :source, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations

  validates :source, presence: true

  validates :path,
    length: { within: 1..255 },
    presence: true,
    uniqueness: { case_sensitive: false }

  scope :matching_path_and_descendants, -> (path) do
    wheres = if Gitlab::Database.postgresql?
               'LOWER(redirect_routes.path) = LOWER(?) OR LOWER(redirect_routes.path) LIKE LOWER(?)'
             else
               'redirect_routes.path = ? OR redirect_routes.path LIKE ?'
             end

    where(wheres, path, "#{sanitize_sql_like(path)}/%")
  end

  scope :permanent, -> do
    if column_permanent_exists?
      where(permanent: true)
    else
      none
    end
  end

  scope :temporary, -> do
    if column_permanent_exists?
      where(permanent: [false, nil])
    else
      all
    end
  end

  default_value_for :permanent, false

  def permanent=(value)
    if self.class.column_permanent_exists?
      super
    end
  end

  def self.column_permanent_exists?
    ActiveRecord::Base.connection.column_exists?(:redirect_routes, :permanent)
  end
end