* schema file undelete description * update with main * update with origin * Add "section" enum to navlink model * create migration for section column * create migration and data-update script * update related rake tasks * add scopes for default_links and other_links * update E2E seeds * ran migration; correct enum reference * fix requests and model specs * write DUS spec; fix factory and DUS * yarn install * address PR review comments * fix migration; add null: false * Remove yarn.lock from commit * remove yarn.lock changes from commit * sync yarn.lock with main * newline
35 lines
1.1 KiB
Ruby
35 lines
1.1 KiB
Ruby
class NavigationLink < ApplicationRecord
|
|
SVG_REGEXP = /<svg .*>/i.freeze
|
|
|
|
before_validation :allow_relative_url, if: :url?
|
|
before_save :strip_local_hostname, if: :url?
|
|
|
|
enum section: { default: 0, other: 1 }, _suffix: true
|
|
|
|
validates :name, :url, :icon, presence: true
|
|
validates :url, url: { schemes: %w[https http] }, uniqueness: { scope: :name }
|
|
validates :icon, format: SVG_REGEXP
|
|
validates :display_only_when_signed_in, inclusion: { in: [true, false] }
|
|
|
|
scope :ordered, -> { order(position: :asc, name: :asc) }
|
|
|
|
private
|
|
|
|
# We want to allow relative URLs (e.g. /contact) for navigation links while
|
|
# still going through the normal validation process.
|
|
def allow_relative_url
|
|
parsed_url = URI.parse(url)
|
|
return unless parsed_url.relative? && url.starts_with?("/")
|
|
|
|
self.url = URI.parse(URL.url).merge(parsed_url).to_s
|
|
end
|
|
|
|
# When persisting to the database we store local links as relative URLs which
|
|
# makes it easier to switch from a forem.cloud subdomain to the live domain.
|
|
def strip_local_hostname
|
|
parsed_url = URI.parse(url)
|
|
return unless url.match?(/^#{URL.url}/i)
|
|
|
|
self.url = parsed_url.path
|
|
end
|
|
end
|