Class: NestedSelect::Item

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

Direct Known Subclasses

List

Constant Summary collapse

@@use_spaces =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NestedSelect

#parameterized_name

Constructor Details

#initialize(name, id) ⇒ Item



11
12
13
14
15
# File 'lib/nested_list/nested_select.rb', line 11

def initialize(name, id)
  @name = name.to_s.strip
  @id = id.to_s
  @parent = nil
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/nested_list/nested_select.rb', line 9

def id
  @id
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/nested_list/nested_select.rb', line 9

def name
  @name
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/nested_list/nested_select.rb', line 9

def parent
  @parent
end

Instance Method Details

#find_by_name(name_tmp) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/nested_list/nested_select.rb', line 25

def find_by_name(name_tmp)
  if !self.name.empty? && self.full_name == name_tmp
    self.parent
  else
    false
  end
end

#full_nameObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nested_list/nested_select.rb', line 46

def full_name
  unless @full_name_str
    item = self
    full_name = []
    full_name << name
    while item = item.parent
      full_name << item.name
    end
    # Remove last general for all element
    full_name.pop
    full_name_str = parameterized_name(full_name.reverse!)
  end
  @full_name_str ||= full_name_str
end

#items_countObject



42
43
44
# File 'lib/nested_list/nested_select.rb', line 42

def items_count
  1
end

#parents_countObject



33
34
35
36
37
38
39
40
# File 'lib/nested_list/nested_select.rb', line 33

def parents_count
  item = self
  count = 0
  while item = item.parent
    count += 1
  end
  count
end

#spacesObject



17
18
19
20
21
22
23
# File 'lib/nested_list/nested_select.rb', line 17

def spaces
  space = ""
  if @@use_spaces
    (parents_count-1).to_i.times{|i| space+='&nbsp;&nbsp;&nbsp;&nbsp;'}
  end
  space
end

#wrapObject



61
62
63
# File 'lib/nested_list/nested_select.rb', line 61

def wrap
  self.id && !self.id.empty? ? "<option value='#{self.id}' class='level_#{parents_count} item'>#{spaces}#{name.humanize}</option>" : ""
end