Files

Class Index [+]

Quicksearch

UbiquoCategories::CategorySelector::Helper

Public Instance Methods

category_selector(object_name, key, options = {}, html_options = {}) click to toggle source

Renders a category selector in a form

  key: CategorySet key (required)
  options(optional):
    type (:checkbox, :select, :autocomplete)
    name (Used as the selector title)
    set  (CategorySet to obtains selector categories)
    include_blank (defaults to false, true to show a blank option if applicable)
    autocomplete_style (:tag, :list)
  html_options: options for the fieldset tag (optional)
    # File vendor/plugins/ubiquo_categories/lib/ubiquo_categories/category_selector/helper.rb, line 14
14:       def category_selector(object_name, key, options = {}, html_options = {})
15:         object = options[:object]
16:         categorize_options = object.class.categorize_options(key)
17:         raise UbiquoCategories::CategorizationNotFoundError unless categorize_options
18:         options[:set] ||= category_set(categorize_options[:from] || key.to_s.pluralize)
19:         categories = uhook_categories_for_set(options[:set], object)
20:         selector_type = options[:type]
21:         categorize_size = categorize_options[:size]
22:         max = Ubiquo::Config.context(:ubiquo_categories).get(:max_categories_simple_selector)
23:         selector_type ||= case categories.size
24:           when 0..max
25:             (categorize_size == :many || categorize_size > 1) ? :checkbox : :select
26:           else
27:             :autocomplete
28:         end
29:         output = content_tag(:fieldset, html_options) do
30:           content_tag(:legend, options[:name] || object.class.human_attribute_name(key)) +
31:             send("category_#{selector_type}_selector",
32:                  object, object_name, key, categories, options.delete(:set), options)
33:         end
34:         output
35:       end

Protected Instance Methods

category_autocomplete_selector(object, object_name, key, categories, set, options = {}) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_categories/lib/ubiquo_categories/category_selector/helper.rb, line 116
116:       def category_autocomplete_selector(object, object_name, key, categories, set, options = {})
117:         url_params = { :category_set_id => set.id, :format => :js }
118:         autocomplete_options = {
119:           :url => ubiquo_category_set_categories_path(url_params),
120:           :current_values => object.send(key).to_json(:only => [:id, :name]),
121:           :style => options[:autocomplete_style] || "tag"
122:         }
123: 
124:         obj_size = object.class.instance_variable_get(:@categorized_with_options)[key][:size] || :many
125: 
126:         size = (obj_size == :many ? 'null' : obj_size.to_i)
127: 
128:         js_code ="document.observe('dom:loaded', function() {\nvar autocomplete = new AutoCompleteSelector(\n'\#{autocomplete_options[:url]}',\n'\#{object_name}',\n'\#{key}',\n\#{autocomplete_options[:current_values]},\n'\#{autocomplete_options[:style]}',\n\#{set.is_editable?},\n\#{size}\n)\n});\n"
129:         javascript_tag(js_code) +
130:           text_field_tag("#{object_name}[#{key}][]", "",
131:                          :id => "#{object_name}_#{key}_autocomplete")
132:       end
category_checkbox_selector(object, object_name, key, categories, set, options = {}) click to toggle source

(Not documented)

    # File vendor/plugins/ubiquo_categories/lib/ubiquo_categories/category_selector/helper.rb, line 72
72:       def category_checkbox_selector(object, object_name, key, categories, set, options = {})
73:         tree = convert_to_tree(categories)
74:         output = ''
75:         if tree.keys.size == 1
76:           output = checkbox_area(object, object_name, key, categories)
77:         else
78:           tree.each_pair do |parent, children|
79:             output << checkbox_area(object, object_name, key,
80:               categories.select{|c| c.id == parent},
81:               {
82:                 :css_class => 'hierarchical_check_list',
83:                 :extra => checkbox_area(object, object_name, key, children,
84:                   :css_class => 'children_check_list check_list'
85:                 )
86:               }
87:             )
88:           end
89:         end
90: 
91:         # hidden field without value is required when you want remove
92:         # all your selection values
93:         output << hidden_field_tag("#{object_name}[#{key}][]", '')
94:         if set.is_editable?
95:           output << new_category_controls("checkbox", object_name, key)
96:         end
97:         output
98:       end
category_select_selector(object, object_name, key, categories, set, options = {}) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_categories/lib/ubiquo_categories/category_selector/helper.rb, line 100
100:       def category_select_selector(object, object_name, key, categories, set, options = {})
101:         blank_name = options[:include_blank] if options[:include_blank].kind_of?(String)
102:         categories_for_select = options[:include_blank] ? [[blank_name, nil]] : []
103:         categories_for_select += categories.collect { |cat| [cat.name, cat.name] }
104:         selected_value = object.send(key).name
105:         output = select_tag(
106:           "#{object_name}[#{key}][]",
107:           options_for_select(categories_for_select, :selected => selected_value),
108:           { :id => "#{object_name}_#{key}_select" }
109:         )
110:         if set.is_editable?
111:           output << new_category_controls("select", object_name, key)
112:         end
113:         output
114:       end
category_set(key) click to toggle source

(Not documented)

    # File vendor/plugins/ubiquo_categories/lib/ubiquo_categories/category_selector/helper.rb, line 39
39:       def category_set(key)
40:         CategorySet.find_by_key(key.to_s) ||
41:         CategorySet.find_by_key(key.singularize) ||
42:         raise(SetNotFoundError.new(key))
43:       end
checkbox_area(object, object_name, key, categories, options = {}) click to toggle source

Creates a set of checkboxes given for the set of given categories options can be

  css_class:  ul's css class. Defaults to 'check_list'
  extra:      string to add at the end of the li
    # File vendor/plugins/ubiquo_categories/lib/ubiquo_categories/category_selector/helper.rb, line 57
57:       def checkbox_area(object, object_name, key, categories, options = {})
58:         options.reverse_merge!({:css_class => 'check_list'})
59:         content_tag(:ul, :class => options[:css_class]) do
60:           categories.map do |category|
61:             content_tag(:li) do
62:               check_box_tag("#{object_name}[#{key}][]", category.name,
63:                 object.send(key).has_category?(category),
64:                 :id => "#{object_name}_#{key}_#{category.id}") + ' ' +
65:                 label_tag("#{object_name}_#{key}_#{category.id}", category) +
66:                 options[:extra].to_s
67:             end
68:           end.join
69:         end
70:       end
convert_to_tree(set) click to toggle source

(Not documented)

    # File vendor/plugins/ubiquo_categories/lib/ubiquo_categories/category_selector/helper.rb, line 45
45:       def convert_to_tree(set)
46:         (map = {}).tap do
47:           set.each do |element|
48:             (map[element.parent_id] ||= []) << element
49:           end
50:         end
51:       end
new_category_controls(type, object_name, key) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_categories/lib/ubiquo_categories/category_selector/helper.rb, line 147
147:       def new_category_controls(type, object_name, key)
148:         content_tag(:div, :class => "new_category_controls") do
149:           link_to(t("ubiquo.category_selector.new_element"), '#',
150:                   :id => "link_new__#{type}__#{object_name}__#{key}",
151:                   :class => "category_selector_new") +
152:           content_tag(:div, :class => "add_new_category", :style => "display:none") do
153:             text_field_tag("new_#{object_name}_#{key}", "", :id => "new_#{object_name}_#{key}") +
154:             link_to(t("ubiquo.category_selector.add_element"), "", :class => "add_new_category_link")
155:           end
156:         end
157:       end

Disabled; run with $DEBUG to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.