Renders a relation selector in a form
key: relation key (required)
options(optional):
type (:checkbox, :select, :autocomplete)
name_field (Used as the selector name)
autocomplete_style (:tag, :list)
collection_url(:url_string)
url_params(:hash_with_additional_url_params)
required(:true_false_param)
add_callback(:callback_string)
remove_callback(:callback_string)
related_object_id_field(:id_field)
options will have an additional parameter giving the related_object field human-readable identifier(s)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 22
22: def relation_selector(object_name, key, options = {}, html_options = {})
23: object = options[:object]
24:
25: if object.respond_to?(key)
26: # This part checks reflections
27: relation_type, object_class_name = discover_relation_by_reflections(
28: object,
29: key,
30: options
31: )
32: # This part is setting needed vars for url-craft and populates
33:
34: # This part populate needed vars for all selectors
35: humanized_field,
36: selector_type, relation_type = define_needed_controls(
37: object_class_name,
38: relation_type,
39: key,
40: object_name,
41: options
42: )
43: # array of possible values
44: related_objects = url_craft_settings object_class_name, selector_type, options
45: # Finally, output is generated
46: if selector_type.to_sym == :select
47: output = content_tag(:p, html_options) do
48: inst_name = options[:name] || object.class.human_attribute_name(key)
49: caption = options[:required] == true ? "#{inst_name} *" : inst_name
50: content_tag(:label, caption) +
51: send("relation_#{selector_type}_selector",
52: object, object_name, key, related_objects, humanized_field, relation_type, options)
53: end
54: else
55: output = content_tag(:fieldset, html_options) do
56: inst_name = options[:name] || object.class.human_attribute_name(key)
57: caption = options[:required] == true ? "#{inst_name} *" : inst_name
58: content_tag(:legend, caption) +
59: send("relation_#{selector_type}_selector",
60: object, object_name, key, related_objects, humanized_field, relation_type, options)
61: end
62: end
63: output
64: else
65: raise RelationSelector::RelationNotFound.new
66: end
67: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 71
71: def define_needed_controls class_name, relation_type, key, object_name, options = {}
72: if options[:name_field].blank?
73: sample_obj = class_name.constantize.new
74: if sample_obj.respond_to?(:name)
75: humanized_field = :name
76: elsif sample_obj.respond_to?(:title)
77: humanized_field = :title
78: else
79: raise RelationSelector::NeedNameField.new("Need a name_field for #{class_name} because no one convention name found")
80: end
81: else
82: humanized_field = options[:name_field]
83: end
84:
85: if options[:type].blank?
86: selector_type = :autocomplete
87: else
88: selector_type = options[:type]
89: end
90:
91: if relation_type == :has_many
92: options[:key_field] = "#{key.to_s.singularize}_ids"
93: options[:limited_elements] = nil
94: options[:initial_text_field_tag_name] = "#{object_name}[#{options[:key_field]}][]"
95: else
96: options[:key_field] = if options[:real_foreign_key].present?
97: options[:real_foreign_key]
98: else
99: "#{key.to_s.singularize}_id"
100: end
101: options[:limited_elements] = 1
102: options[:initial_text_field_tag_name] = "#{object_name}[#{options[:key_field]}]"
103: end
104: return humanized_field, selector_type, relation_type
105: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 127
127: def discover_relation_by_reflections object, key, options = {}
128: relation_type = nil
129: class_name = nil
130: object.class.reflections.each do |ref|
131: if ref[1].name == key.to_sym
132: relation_type = ref[1].macro
133: class_name = ref[1].class_name
134: if ref[1].options[:foreign_key].present?
135: options[:real_foreign_key] = ref[1].options[:foreign_key]
136: end
137: break
138: end
139: end
140: return [relation_type, class_name]
141: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 235
235: def new_relation_controls(type, object_name, key)
236: content_tag(:div, :class => "new_category_controls") do
237: link_to(t("ubiquo.category_selector.new_element"), '#',
238: :id => "link_new__#{type}__#{object_name}__#{key}",
239: :class => "category_selector_new") +
240: content_tag(:div, :class => "add_new_category", :style => "display:none") do
241: text_field_tag("new_#{object_name}_#{key}", "", :id => "new_#{object_name}_#{key}") +
242: link_to(
243: t("ubiquo.category_selector.add_element"),
244: "",
245: :class => "add_new_category_link"
246: )
247: end
248: end
249: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 224
224: def open_struct_from_model(objects, id_field, key_field)
225: ret = []
226: objects.to_a.each do |obj|
227: ret << OpenStruct.new(
228: id_field.to_sym => obj.send(id_field),
229: key_field.to_sym => obj.send(key_field)
230: )
231: end
232: ret.to_json
233: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 178
178: def relation_autocomplete_selector(object, object_name, key, related_objects, humanized_field, relation_type, options = {})
179: url_params = {:format => :js}
180: url_params.merge!(options[:url_params]) if options[:url_params].present?
181: autocomplete_options = {
182: :url => send(options[:collection_url], url_params),
183: :current_values => open_struct_from_model(
184: object.send(key),
185: options[:related_object_id_field] || 'id',
186: humanized_field
187: ),
188: :style => options[:autocomplete_style] || "tag"
189: }
190: options[:add_callback] = if options[:add_callback].blank?
191: 'undefined'
192: else
193: "'#{options[:add_callback]}'"
194: end
195: options[:remove_callback] = if options[:remove_callback].blank?
196: 'undefined'
197: else
198: "'#{options[:remove_callback]}'"
199: end
200: js_code ="document.observe('dom:loaded', function() {\nvar autocomplete = new RelationAutoCompleteSelector(\n'\#{autocomplete_options[:url]}',\n'\#{object_name}',\n'\#{options[:key_field]}',\n\#{autocomplete_options[:current_values]},\n'\#{autocomplete_options[:style]}',\n\#{options[:limited_elements] || 'undefined'},\n'\#{humanized_field}',\n'\#{options[:related_object_id_field]}',\n\#{options[:add_callback]},\n\#{options[:remove_callback]}\n)\n});\n"
201: output = javascript_tag(js_code) + text_field_tag(
202: options[:initial_text_field_tag_name], "",
203: :id => "#{object_name}_#{options[:key_field]}_autocomplete"
204: )
205: output << relation_controls(options)
206: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 143
143: def relation_checkbox_selector(object, object_name, key, related_objects, humanized_field, relation_type, options = {})
144: current_related_objects = object.send(key).to_a
145: if current_related_objects.length > 0
146: current_related_objects = current_related_objects.map(&:id).to_a
147: end
148: output = content_tag(:ul, :class => 'check_list') do
149: related_objects.map do |ro|
150: content_tag(:li) do
151: check_box_tag("#{object_name}[#{options[:key_field]}][]", ro.id,
152: current_related_objects.include?(ro.id),
153: :id => "#{object_name}_#{options[:key_field]}_#{ro.id}") +
154: label_tag(
155: "#{object_name}_#{options[:key_field]}_#{ro.id}",
156: ro.send(humanized_field)
157: )
158: end
159: end.join
160: end
161: output << hidden_field_tag("#{object_name}[#{options[:key_field]}][]", '')
162: output << relation_controls(options)
163: output
164: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 251
251: def relation_controls(options = {})
252: if options[:hide_controls] == true
253: ''
254: elsif options[:related_control].blank?
255: "<p class='relation_new'>#{link_to(I18n.t('ubiquo.new_relation'), options[:related_url], {:target => '_blank'})}</p>"
256: else
257: options[:related_control]
258: end
259: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 166
166: def relation_select_selector(object, object_name, key, related_objects, humanized_field, relation_type, options = {})
167: objects_for_select = related_objects.collect { |cat|
168: [cat.send(humanized_field), cat.id]
169: }
170: output = select_tag("#{object_name}[#{options[:key_field]}]",
171: options_for_select(objects_for_select,
172: :selected => (object.send(key).id rescue '')),
173: { :id => "#{object_name}_#{options[:key_field]}_select" })
174: output << relation_controls(options)
175: output
176: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/relation_selector/relation_selector.rb, line 107
107: def url_craft_settings class_name, selector_type, options = {}
108: related_objects = []
109: if options[:collection_url].blank?
110: options[:related_url] = send("new_ubiquo_#{class_name.tableize.singularize}_url")
111: options[:collection_url] = "ubiquo_#{class_name.tableize.pluralize}_url"
112: if selector_type != :autocomplete
113: related_objects = if class_name.constantize.respond_to?(:locale)
114: # TODO this should be in a connector
115: class_name.constantize.locale(current_locale, :all).all
116: else
117: class_name.constantize.all
118: end
119: end
120: else
121: options[:hide_controls] = true
122: end
123: options[:related_object_id_field] ||= 'id'
124: return related_objects
125: end
Disabled; run with $DEBUG to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.