Sha256: 0339259528244bba6903b47668793d5c53e1ca5f372d1defd3697de32e19e0e7

Contents?: true

Size: 1.96 KB

Versions: 8

Compression:

Stored size: 1.96 KB

Contents

module Refinery
  class MenuItem < HashWithIndifferentAccess

    class << self
      def attributes
        [:title, :parent_id, :lft, :rgt, :depth, :url, :menu, :menu_match]
      end

      def apply_attributes!
        attributes.each do |attribute|
          class_eval %{
            def #{attribute}
              @#{attribute} ||= self[:#{attribute}]
            end
          } unless self.respond_to?(attribute)
          class_eval %{
            def #{attribute}=(attr)
              @#{attribute} = attr
            end
          } unless self.respond_to?(:"#{attribute}=")
        end
      end
    end

    def original_id
      @original_id ||= self[:id]
    end

    def original_type
      @original_type ||= self[:type]
    end

    apply_attributes!

    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.original_type == original_type && item.parent_id == original_id }
      else
        []
      end
    end

    def descendants
      @descendants ||= if has_descendants?
        menu.select{|item| item.original_type == original_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 = {}

      self.class.attributes.each do |attribute|
        hash[attribute] = self[attribute]
      end

      hash.inspect
    end

    def parent
      @parent ||= (menu.detect{|item| item.original_type == original_type && item.original_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

8 entries across 8 versions & 1 rubygems

Version Path
refinerycms-core-2.0.10 lib/refinery/menu_item.rb
refinerycms-core-2.0.9 lib/refinery/menu_item.rb
refinerycms-core-2.0.8 lib/refinery/menu_item.rb
refinerycms-core-2.0.7 lib/refinery/menu_item.rb
refinerycms-core-2.0.6 lib/refinery/menu_item.rb
refinerycms-core-2.0.5 lib/refinery/menu_item.rb
refinerycms-core-2.0.4 lib/refinery/menu_item.rb
refinerycms-core-2.0.3 lib/refinery/menu_item.rb