Sha256: 9727a2de70f3b68b6456d01aa6039b3efc8fe6dd249e7b9640ece5ae04777469

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

module Ksk::Navigation
  extend ActiveSupport::Concern

  included do
    validates_uniqueness_of :slug
    validates_presence_of :static

    before_validation :static_setter

    default_scope -> {order 'position ASC, created_at DESC'}

    belongs_to :static
    belongs_to :navigation_type
    belongs_to :parent, foreign_key: 'parent_id', class_name: 'Navigation', optional: true
    has_many   :children, foreign_key: 'parent_id', class_name: 'Navigation', dependent: :delete_all

    scope :top_level, -> {where(parent_id: 0)}

    scope :not_hidden, -> {where(hidden: false)}

    before_save :set_slug, :set_link
  end

  def set_slug
    return if !slug.blank?
    write_attribute(:slug, title.to_url)
  end

  def static_setter
    write_attribute(:static_id, (self.static && self.static.id) || ::Static.all.first.id)
  end

  def set_link
    write_attribute(:link, get_link)
  end

  def get_link(x = '')
    a = '/'+slug+x
    if parent
      a = parent.get_link(a)
    end
    a
  end
  
  module ClassMethods
    def sort_items(ids)
      ids.each_pair do |i, id|
        find(id).update_attribute(:position, i.to_i)
      end
    end

    def show_bread_crum_desc(navi)
      a = [navi]
      if navi.parent
        a << show_bread_crum_desc(navi.parent)
      end
      a.flatten
    end

    def show_bread_crum(navi)
      show_bread_crum_desc(navi).reverse
    end
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ksk-0.4.11 app/models/concerns/ksk/navigation.rb
ksk-0.4.10 app/models/concerns/ksk/navigation.rb
ksk-0.4.9 app/models/concerns/ksk/navigation.rb
ksk-0.4.8 app/models/concerns/ksk/navigation.rb