Sha256: 5b6e5b565d142af8882acb2d250dbcb12f90a9a977d71a8d8b7586868582a195

Contents?: true

Size: 1.83 KB

Versions: 16

Compression:

Stored size: 1.83 KB

Contents

module HowTo
  module FaqHelper
    def nested_li(objects, wrapper_attributes = '', &block)
      objects = objects.order(:lft) if objects.is_a? Class

      return '' if objects.size == 0

      output = "<ul #{wrapper_attributes}><li>"
      path = [nil]

      objects.each_with_index do |o, i|
        if o.parent_id != path.last
          # We are on a new level, did we decend or ascend?
          if path.include?(o.parent_id)
            # Remove wrong wrong tailing paths elements
            while path.last != o.parent_id
              path.pop
              output << '</li></ul>'
            end
            output << '</li><li>'
          else
            path << o.parent_id
            output << '<ul><li>'
          end
        elsif i != 0
          output << '</li><li>'
        end
        output << capture(o, path.size - 1, &block)
      end

      output << '</li></ul>' * path.length
      output.html_safe
    end

    def sorted_nested_li(objects, order, wrapper_attributes = '', &block)
      nested_li sort_list(objects, order), wrapper_attributes, &block
    end

    private

    def sort_list(objects, order)
      objects = objects.order(:lft) if objects.is_a? Class

      # Partition the results
      children_of = {}
      objects.each do |o|
        children_of[o.parent_id] ||= []
        children_of[o.parent_id] << o
      end

      # Sort each sub-list individually
      children_of.each_value do |children|
        children.sort_by! &order
      end

      # Re-join them into a single list
      results = []
      recombine_lists(results, children_of, nil)

      results
    end

    def recombine_lists(results, children_of, parent_id)
      if children_of[parent_id]
        children_of[parent_id].each do |o|
          results << o
          recombine_lists(results, children_of, o.id)
        end
      end
    end

  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
how_to-2.0.5 app/helpers/how_to/faq_helper.rb
how_to-2.0.4 app/helpers/how_to/faq_helper.rb
how_to-2.0.3 app/helpers/how_to/faq_helper.rb
how_to-2.0.2 app/helpers/how_to/faq_helper.rb
how_to-2.0.1 app/helpers/how_to/faq_helper.rb
how_to-2.0.0 app/helpers/how_to/faq_helper.rb
how_to-1.0.3 app/helpers/how_to/faq_helper.rb
how_to-1.0.2 app/helpers/how_to/faq_helper.rb
how_to-0.0.8 app/helpers/how_to/faq_helper.rb
how_to-0.0.7 app/helpers/how_to/faq_helper.rb
how_to-0.0.6 app/helpers/how_to/faq_helper.rb
how_to-0.0.5 app/helpers/how_to/faq_helper.rb
how_to-0.0.4 app/helpers/how_to/faq_helper.rb
how_to-0.0.3 app/helpers/how_to/faq_helper.rb
how_to-0.0.2 app/helpers/how_to/faq_helper.rb
how_to-0.0.1 app/helpers/how_to/faq_helper.rb