Sha256: ce4eb6049367438ee96c7c57677edff054876baaefbeeba3c339234e7cb53977

Contents?: true

Size: 1.99 KB

Versions: 7

Compression:

Stored size: 1.99 KB

Contents

module SimpleNavigation
  module Renderer
    
    # This is the base class for all renderers.
    #
    # A renderer is responsible for rendering an ItemContainer and its containing items to HTML.
    class Base
      include ActionView::Helpers::UrlHelper
      include ActionView::Helpers::TagHelper
      
      attr_reader :controller, :options

      class << self
        
        # Delegates method calls to the controller.
        def controller_method(*methods)
          methods.each do |method|
            delegate method, :to => :controller
          end
        end
        
      end

      controller_method :form_authenticity_token, :protect_against_forgery?, :request_forgery_protection_token
      
      def initialize(options) #:nodoc:
        @options = options
        @controller = SimpleNavigation.controller
      end
            
      def expand_all?
        !!options[:expand_all]
      end
      
      def level
        options[:level] || :all
      end      
      
      def include_sub_navigation?(item)
        consider_sub_navigation?(item) && expand_sub_navigation?(item)
      end      
      
      def render_sub_navigation_for(item)
        item.sub_navigation.render(self.options)
      end
            
      # Renders the specified ItemContainer to HTML.
      #
      # When implementing a renderer, please consider to call include_sub_navigation? to determin
      # whether an item's sub_navigation should be rendered or not.
      #  
      def render(item_container)
        raise 'subclass responsibility'
      end
         
      protected
      
      def consider_sub_navigation?(item)
        return false if item.sub_navigation.nil?
        case level
        when :all
          return true
        when Integer
          return false
        when Range
          return item.sub_navigation.level <= level.max
        end
        false
      end
      
      def expand_sub_navigation?(item)
        expand_all? || item.selected?
      end
      
            
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
simple-navigation-2.4.2 lib/simple_navigation/renderer/base.rb
simple-navigation-2.4.1 lib/simple_navigation/renderer/base.rb
simple-navigation-2.4.0 lib/simple_navigation/renderer/base.rb
simple-navigation-2.2.3 lib/simple_navigation/renderer/base.rb
simple-navigation-2.2.2 lib/simple_navigation/renderer/base.rb
simple-navigation-2.2.1 lib/simple_navigation/renderer/base.rb
simple-navigation-2.2.0 lib/simple_navigation/renderer/base.rb