Module: Caboose::CategoriesHelper

Defined in:
app/helpers/caboose/categories_helper.rb

Instance Method Summary collapse

Instance Method Details

#category_checkboxes(top_categories, selected_ids = nil) ⇒ Object


54
55
56
57
58
59
60
61
# File 'app/helpers/caboose/categories_helper.rb', line 54

def category_checkboxes(top_categories, selected_ids = nil)
  str = "<ul>"
  top_categories.each do |cat|
    category_checkboxes_helper(cat, selected_ids, str)
  end
  str << "</ul>"
  return str
end

#category_checkboxes_helper(cat, selected_ids, str, prefix = "") ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/caboose/categories_helper.rb', line 63

def category_checkboxes_helper(cat, selected_ids, str, prefix = "")
  str << "<li>"
  if cat.children && cat.children.count > 0
    str << "<input type='checkbox' id='cat_#{cat.id}' value='#{cat.id}'"
    str << " checked='true'" if selected_ids && selected_ids.include?(cat.id)
    str << "> <label for='#{cat.id}'><h3>#{cat.name}</h3></label>"
  else
    str << "<input type='checkbox' id='cat_#{cat.id}' value='#{cat.id}'"
    str << " checked='true'" if selected_ids && selected_ids.include?(cat.id)
    str << "> <label for='#{cat.id}'>#{cat.name}</label>"
  end
  cat.children.each do |cat2|
    str << "<ul>"
    category_checkboxes_helper(cat2, selected_ids, str, "#{prefix}&nbsp;&nbsp;")
    str << "</ul>"
  end
  str << "</li>"
end

#category_list(category = root_category) ⇒ Object


11
12
13
# File 'app/helpers/caboose/categories_helper.rb', line 11

def category_list(category=root_category)
   :ul, category_list_items(category)
end

#category_list_items(category) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/caboose/categories_helper.rb', line 15

def category_list_items(category)
  
  # Link to category
  link = link_to(category.name, "/admin/categories/#{category.id}")
  
  # Recursively find category children
  children =  :ul, category.children.collect { |child| category_list_items(child) }.join.to_s.html_safe if category.children.any?
  
  # Return the list item
   :li, link.concat(children)
end

#category_options(category, selected_id, prefix = "") ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/caboose/categories_helper.rb', line 39

def category_options(category, selected_id, prefix="")
  
  # Array to hold options
  options = Array.new
  
  # Recusively tterate over all child categories
  category.children.collect do |child|
    options << ["#{prefix} - #{child.name}", child.id]
    options.concat category_options(child, selected_id, "#{prefix} -")
  end
  
  # Return the options array
  return options
end

#category_select(form_name, category = root_category, selected_id = nil) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/caboose/categories_helper.rb', line 27

def category_select(form_name, category=root_category, selected_id=nil)
  
  # Collect all recursive options from specified category down
  options = category_options(category, selected_id)
  
  # Prepend the root category
  options.unshift([category.name, category.id])
  
  # Create select tag
  select_tag form_name, options_for_select(options)
end

#root_categoryObject


3
4
5
# File 'app/helpers/caboose/categories_helper.rb', line 3

def root_category
  Caboose::Category.root
end

#top_level_categoriesObject


7
8
9
# File 'app/helpers/caboose/categories_helper.rb', line 7

def top_level_categories
  Caboose::Category.top_level
end