Sha256: 1e1b9817ec1b352682abd10742732f68c856dbda4ccc2969a0dd89e1d0318582

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Refinery
  class MenuItem < HashWithIndifferentAccess

    (ATTRIBUTES = [:id, :title, :parent_id, :lft, :rgt, :depth, :url, :menu_id, :menu_match, :type]).each do |attribute|
      class_eval %{
        def #{attribute}
          @#{attribute} ||= self[:#{attribute}]
        end

        def #{attribute}=(attr)
          @#{attribute} = attr
        end
      }
    end

    def ancestors
      return @ancestors if @ancestors
      @ancestors = []
      p = self
      @ancestors << p until(p = p.parent).nil?

      @ancestors
    end

    def children
      @children ||= if has_children?
        menu.select{|item| item.type == type && item.parent_id == id}
      else
        []
      end
    end

    def descendants
      @descendants ||= if has_descendants?
        menu.select{|item| item.type == type && item.lft > lft && item.rgt < rgt}
      else
        []
      end
    end

    def has_children?
      @has_children ||= (rgt > lft + 1)
    end
    # really, they're the same.
    alias_method :has_descendants?, :has_children?

    def has_parent?
      !parent_id.nil?
    end

    def inspect
      hash = {}

      ATTRIBUTES.each do |attribute|
        hash[attribute] = self[attribute]
      end

      hash
    end

    def menu
      ::Refinery.menus[menu_id]
    end

    def parent
      @parent ||= (menu.detect{|item| item.type == type && item.id == parent_id} if has_parent?)
    end

    def siblings
      @siblings ||= ((has_parent? ? parent.children : menu.roots) - [self])
    end
    alias_method :shown_siblings, :siblings

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
refinerycms-core-1.0.11 lib/refinery/menu_item.rb
refinerycms-core-1.0.10 lib/refinery/menu_item.rb
refinerycms-core-1.0.9 lib/refinery/menu_item.rb