Sha256: 55549a8637096b346caff4fbb15214868aceaa3f485b63a879cb284143f7f7c4

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

module Amalgam
  module Tree
    class Exportable
      def self.export(attributes = [])
        tmp_hash = []
        @pages = Page.all
        children_nodes.each do |node|
          render_tree node, tmp_hash, attributes
        end
        tmp_hash
      end

      protected

      def self.render_tree node,hash,attributes
        self_hash = self.create_item(node,attributes)
        hash << self_hash
        if children_nodes(node).present?
          self_hash[node.slug]['children'] = []
          children_nodes(node).each do |child|
            render_tree child, self_hash[node.slug]['children'], attributes
          end
        end
      end

      def self.children_nodes(node=nil)
        node ? @pages.select{ |x| x.parent_id == node.id} : @pages.select{ |x| x.parent_id == nil}
      end

      def self.create_item(node,attributes)
        hash = {node.slug => {}}
        unless attributes.empty?
          attributes.each do |attribute|
            hash[node.slug][attribute] = node.send attribute if node.respond_to?(attribute) and node.send attribute
          end
        else
          Page.column_names.each do |column|
            hash[node.slug][column] = node.send column if node.respond_to?(column) and node.send column
          end
        end
        hash
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
amalgam-2.1.4.1 lib/amalgam/tree/exportable.rb
amalgam-2.1.4 lib/amalgam/tree/exportable.rb
amalgam-2.1.3.1 lib/amalgam/tree/exportable.rb
amalgam-2.1.3 lib/amalgam/tree/exportable.rb
amalgam-2.1.2 lib/amalgam/tree/exportable.rb
amalgam-2.1.1 lib/amalgam/tree/exportable.rb