Class: Generator::NestedList

Inherits:
Object
  • Object
show all
Defined in:
lib/nested_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(nested_names_arr) ⇒ NestedList

Get Array of nested names: [“All Categories”, id: ‘all’, “Audio”, id: ‘1’, “Audio>Accessorize”, id: ‘1424’, “Audio>Accessorize>Pillow”, id: ‘45255’ “Audio>DVD”, id: ‘234245’, “Baby”, id: ‘44245tr5’]



14
15
16
# File 'lib/nested_list.rb', line 14

def initialize(nested_names_arr)
  @nested_names_arr = nested_names_arr
end

Instance Method Details

#html_optionsObject

Return a list of nested html_options from nested names array



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nested_list.rb', line 19

def html_options
@wraped_list =  {}
list = NestedSelect::List.new("root", nil)

@nested_names_arr.each do |nested_names|
  @names = nested_names[:name].split(">")
  point = list
  length = @names.length - 1
  @names.each_with_index do |name, level|
     id = nested_names[:id]
     full_name = @names[0..level].join(" ").parameterize

     if  exist_node=list.find_by_name(full_name)
       # Use existed name
       if  exist_node.full_name!=full_name && length != level

         found_item = exist_node.find_item(name)
         moved_item = found_item

         exist_node.remove_item(found_item)
         item = NestedSelect::List.new(name, nil)
         item.add_item(moved_item)
         exist_node.add_item(item)
         point = item
       else
         point = exist_node
       end

     else
      # Create new node
      unless (level == length)
        item = NestedSelect::List.new(name, nil)
        item.add_item(NestedSelect::Item.new(name, nil))
      else
        item = NestedSelect::Item.new(name, id)
      end

      point.add_item(item)
      point = item
     end
 end

end

@wraped_list = list.wrap
end