Class: NestedSelect::List

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

Instance Attribute Summary

Attributes inherited from Item

#id, #name, #parent

Instance Method Summary collapse

Methods inherited from Item

#full_name, #parents_count, #spaces

Methods included from NestedSelect

#parameterized_name

Constructor Details

#initialize(name, id) ⇒ List

Returns a new instance of List.



69
70
71
72
# File 'lib/nested_list/nested_select.rb', line 69

def initialize(name, id)
  super(name, id)
  @items = []
end

Instance Method Details

#add_item(item) ⇒ Object



82
83
84
85
# File 'lib/nested_list/nested_select.rb', line 82

def add_item(item)
  @items << item
  item.parent = self
end

#branchesObject



78
79
80
# File 'lib/nested_list/nested_select.rb', line 78

def branches
  @branches = @items[1..@items.size]
end

#find_by_name(name_tmp) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/nested_list/nested_select.rb', line 103

def find_by_name(name_tmp)
  existed_item = nil
  existed_items = []

  if self.full_name == name_tmp
    return self
  end

  @items.each do |item|
    existed_item = item.find_by_name(name_tmp)
    existed_items << existed_item if existed_item
  end
  return nil if existed_items.size == 0
  return existed_items[0]
end

#find_item(tmp_name) ⇒ Object



92
93
94
95
96
97
# File 'lib/nested_list/nested_select.rb', line 92

def find_item(tmp_name)
  tmp_name = tmp_name.to_s.strip
  @items.find do |item|
    item.name == tmp_name
  end
end

#items_countObject



99
100
101
# File 'lib/nested_list/nested_select.rb', line 99

def items_count
   @items.inject(0){|acc,i| acc+i.items_count}
end

#nodeObject



74
75
76
# File 'lib/nested_list/nested_select.rb', line 74

def node
  @items.first
end

#remove_item(item) ⇒ Object



87
88
89
90
# File 'lib/nested_list/nested_select.rb', line 87

def remove_item(item)
  @items.delete(item)
  item.parent = nil
end

#wrapObject



119
120
121
122
123
# File 'lib/nested_list/nested_select.rb', line 119

def wrap
  result = (name == 'root') ? "" : "<option value='#{full_name}:group' class='level_#{(n = parents_count.modulo(4))>0 || parents_count==0 ? n : 4} group'>#{spaces}#{name.humanize}</option>"
  @items.each {|item| result += item.wrap }
  result
end