2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
class KnowledgeBase::MenuItem < ApplicationModel
|
|
|
|
belongs_to :kb_locale, class_name: 'KnowledgeBase::Locale', inverse_of: :menu_items, touch: true
|
|
|
|
|
2020-05-13 17:01:42 +00:00
|
|
|
validates :title, presence: true, length: { maximum: 100 }
|
|
|
|
validates :url, presence: true, length: { maximum: 500 }
|
|
|
|
validates :location, presence: true, inclusion: { in: %w[header footer] }
|
2019-06-04 03:40:48 +00:00
|
|
|
|
2020-05-13 17:01:42 +00:00
|
|
|
acts_as_list scope: %i[kb_locale_id location], top_of_list: 0
|
2019-06-04 03:40:48 +00:00
|
|
|
|
2020-05-13 17:01:42 +00:00
|
|
|
scope :sorted, -> { order(position: :asc) }
|
2021-07-16 13:38:01 +00:00
|
|
|
scope :using_locale, ->(locale) { locale.present? ? joins(:kb_locale).where(knowledge_base_locales: { system_locale_id: locale.id }) : none }
|
2020-05-13 17:01:42 +00:00
|
|
|
scope :location, ->(location) { sorted.where(location: location) }
|
|
|
|
|
|
|
|
scope :location_header, -> { location(:header) }
|
|
|
|
scope :location_footer, -> { location(:footer) }
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def add_protocol_prefix
|
2020-05-13 17:01:42 +00:00
|
|
|
return if url.blank?
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
url.strip!
|
|
|
|
|
2020-06-08 07:17:29 +00:00
|
|
|
return if url.match? %r{^\S+://}
|
2019-06-04 03:40:48 +00:00
|
|
|
return if url[0] == '/'
|
|
|
|
|
|
|
|
self.url = "http://#{url}"
|
|
|
|
end
|
|
|
|
|
|
|
|
before_validation :add_protocol_prefix
|
|
|
|
end
|