Sha256: ac8a5ed3b29db3b086491b9cd19256656f43c4c268701327d62fe0fd262885df

Contents?: true

Size: 1.26 KB

Versions: 11

Compression:

Stored size: 1.26 KB

Contents

module Trestle
  class Navigation
    class Block
      attr_reader :block, :admin

      def initialize(admin=nil, &block)
        @admin = admin
        @block = block
      end

      def items
        context = Context.new(@admin)
        context.instance_exec(@admin, &block)
        context.items
      end

      class Context
        include Rails.application.routes.url_helpers if Rails.application

        attr_reader :items

        def initialize(admin=nil)
          @admin = admin
          @items = []
        end

        def default_path
          @admin ? @admin.path : nil
        end

        def item(name, path=nil, options={})
          if path.is_a?(Hash)
            options = path
            path = nil
          end

          if options[:group]
            group = Group.new(options[:group])
          elsif @current_group
            group = @current_group
          end

          options = options.merge(group: group) if group
          options = options.merge(admin: @admin) if @admin

          items << Item.new(name, path || default_path, options)
        end

        def group(name, options={})
          @current_group = Group.new(name, options)
          yield
        ensure
          @current_group = nil
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
trestle-0.8.11 lib/trestle/navigation/block.rb
trestle-0.8.10 lib/trestle/navigation/block.rb
trestle-0.8.9 lib/trestle/navigation/block.rb
trestle-0.8.8 lib/trestle/navigation/block.rb
trestle-0.8.7 lib/trestle/navigation/block.rb
trestle-0.8.6 lib/trestle/navigation/block.rb
trestle-0.8.5 lib/trestle/navigation/block.rb
trestle-0.8.4 lib/trestle/navigation/block.rb
trestle-0.8.3 lib/trestle/navigation/block.rb
trestle-0.8.2 lib/trestle/navigation/block.rb
trestle-0.8.0 lib/trestle/navigation/block.rb