menu_item.rb 1.0 KB

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