2017-05-01 20:46:30 +00:00
|
|
|
class RedirectRoute < ActiveRecord::Base
|
2017-06-02 12:29:30 +00:00
|
|
|
belongs_to :source, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2017-05-01 20:46:30 +00:00
|
|
|
|
|
|
|
validates :source, presence: true
|
|
|
|
|
|
|
|
validates :path,
|
|
|
|
length: { within: 1..255 },
|
|
|
|
presence: true,
|
|
|
|
uniqueness: { case_sensitive: false }
|
2017-05-03 17:14:30 +00:00
|
|
|
|
2017-08-08 00:16:38 +00:00
|
|
|
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
|
2017-08-15 17:44:37 +00:00
|
|
|
|
2017-08-08 00:16:38 +00:00
|
|
|
where(wheres, path, "#{sanitize_sql_like(path)}/%")
|
|
|
|
end
|
2017-12-08 17:42:43 +00:00
|
|
|
|
|
|
|
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
|
2017-05-01 20:46:30 +00:00
|
|
|
end
|