trabajo-afectivo/app/models/knowledge_base/menu_item.rb

32 lines
1 KiB
Ruby
Raw Normal View History

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
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
acts_as_list scope: %i[kb_locale_id location], top_of_list: 0
2019-06-04 03:40:48 +00:00
scope :sorted, -> { order(position: :asc) }
scope :using_locale, ->(locale) { locale.present? ? joins(:kb_locale).where(knowledge_base_locales: { system_locale_id: locale.id } ) : none }
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
return if url.blank?
2019-06-04 03:40:48 +00:00
url.strip!
return if url.match? %r{^\S+\:\/\/}
return if url[0] == '/'
self.url = "http://#{url}"
end
before_validation :add_protocol_prefix
end