menu_item.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::MenuItem < ApplicationModel
  3. belongs_to :kb_locale, class_name: 'KnowledgeBase::Locale', inverse_of: :menu_items, touch: true
  4. validates :title, presence: true, length: { maximum: 100 }
  5. validates :url, presence: true, length: { maximum: 500 }
  6. validates :location, presence: true, inclusion: { in: %w[header footer] }
  7. acts_as_list scope: %i[kb_locale_id location], top_of_list: 0
  8. scope :sorted, -> { order(position: :asc) }
  9. scope :using_locale, ->(locale) { locale.present? ? joins(:kb_locale).where(knowledge_base_locales: { system_locale_id: locale.id }) : none }
  10. scope :location, ->(location) { sorted.where(location: location) }
  11. scope :location_header, -> { location(:header) }
  12. scope :location_footer, -> { location(:footer) }
  13. private
  14. def add_protocol_prefix
  15. return if url.blank?
  16. url.strip!
  17. return if url.match? %r{^\S+://}
  18. return if url[0] == '/'
  19. self.url = "http://#{url}"
  20. end
  21. before_validation :add_protocol_prefix
  22. end