| Module | Admin::ArticleCategoriesHelper |
| In: |
app/helpers/admin/article_categories_helper.rb
|
HTML chunks for ArticleCategories UL tree
# File app/helpers/admin/article_categories_helper.rb, line 3
3: def display_categories(categories, parent_id)
4: ret = "<ul>"
5: for category in categories
6: if category.parent_id == nil
7: category.parent_id = 0
8: elsif category.parent_id == parent_id
9: ret << display_category(category)
10: end
11: end
12: ret << "</ul>"
13: end
# File app/helpers/admin/article_categories_helper.rb, line 15
15: def display_category(category)
16: ret = "<li>"
17: ret << link_to(h(category.name), :action => "edit", :id => category)
18: ret << " - " << h(category.description)
19: ret << display_categories(category.children, category.id) if category.children.any?
20: ret << "</li>"
21: end
# File app/helpers/admin/article_categories_helper.rb, line 23
23: def tree_select(categories, model, name, selected=0, allow_root = true, level = 0, init = true)
24: html = ""
25: if init
26: html << "<select name=\"#{model}[#{name}]\" id=\"#{model}_#{name}\">\n"
27: if allow_root
28: # The "Root" option is added
29: # so the user can choose a parent_id of 0
30: html << "\t<option value=\"0\""
31: html << " selected=\"selected\"" if selected.parent_id == 0
32: html << ">Root</option>\n"
33: end
34: end
35:
36: if categories.length > 0
37: level += 1 # keep position
38: categories.collect do |cat|
39: html << "\t<option value=\"#{cat.id}\" style=\"padding-left:#{level * 10}px\""
40: # TODO: we need to be able to tell this routine what field to look at to determine parent ID
41: # with articles it is article_category_id
42: # with article_categories it is parent_id
43: if model == "article"
44: html << ' selected="selected"' if cat.id == selected.article_category_id
45: else
46: html << ' selected="selected"' if cat.id == selected.parent_id
47: end
48: html << ">#{cat.name}</option>\n"
49: html << tree_select(cat.children, model, name, selected, allow_root, level, false)
50: end
51: end
52: html << "</select>\n" if init
53: return html
54: end