Commit 4c6e563fb10b24118e026aeec8e9608da41735ab

Authored by MoisesMachado
1 parent f4fab949

ActionItem501: moved all form related helpers to forms_helper.rb


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2107 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/helpers/application_helper.rb
@@ -154,108 +154,6 @@ module ApplicationHelper @@ -154,108 +154,6 @@ module ApplicationHelper
154 @controller.send(:profile) || raise("There is no current profile") 154 @controller.send(:profile) || raise("There is no current profile")
155 end 155 end
156 156
157 - # create a form field structure (as if it were generated with  
158 - # labelled_form_for), but with a customized control and label.  
159 - #  
160 - # If +field_id+ is not given, the underlying implementation will try to guess  
161 - # it from +field_html+ using a regular expression. In this case, make sure  
162 - # that the first ocurrance of id=['"]([^'"]*)['"] in +field_html+ if the one  
163 - # you want (i.e. the correct id for the control )  
164 - def labelled_form_field(label, field_html, field_id = nil)  
165 - NoosferoFormBuilder::output_field(label, field_html, field_id)  
166 - end  
167 -  
168 - alias_method :display_form_field, :labelled_form_field  
169 -  
170 - def labelled_fields_for(name, object = nil, options = {}, &proc)  
171 - object ||= instance_variable_get("@#{name}")  
172 - fields_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc)  
173 - end  
174 -  
175 - def labelled_form_for(name, object = nil, options = {}, &proc)  
176 - object ||= instance_variable_get("@#{name}")  
177 - form_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc)  
178 - end  
179 -  
180 - class NoosferoFormBuilder < ActionView::Helpers::FormBuilder  
181 - include GetText  
182 - extend ActionView::Helpers::TagHelper  
183 -  
184 - def self.output_field(text, field_html, field_id = nil)  
185 - # try to guess an id if none given  
186 - if field_id.nil?  
187 - field_html =~ /id=['"]([^'"]*)['"]/  
188 - field_id = $1  
189 - end  
190 - field_html =~ /type=['"]([^'"]*)['"]/  
191 - field_html =~ /<(\w*)/ unless $1  
192 - field_type = $1  
193 - field_class = 'formfield type-' + field_type if field_type  
194 -  
195 - label_html = content_tag('label', _(text), :class => 'formlabel', :for => field_id)  
196 - control_html = content_tag('div', field_html, :class => field_class )  
197 -  
198 - content_tag('div', label_html + control_html, :class => 'formfieldline' )  
199 - end  
200 -  
201 - (field_helpers - %w(hidden_field)).each do |selector|  
202 - src = <<-END_SRC  
203 - def #{selector}(field, *args, &proc)  
204 - column = object.class.columns_hash[field.to_s]  
205 - text =  
206 - ( column ?  
207 - column.human_name :  
208 - _(field.to_s.humanize)  
209 - )  
210 -  
211 - NoosferoFormBuilder::output_field(text, super)  
212 - end  
213 - END_SRC  
214 - class_eval src, __FILE__, __LINE__  
215 - end  
216 -  
217 - # Create a formatable radio collection  
218 - # Tha values parameter is a array of [value, label] arrays like this:  
219 - # [ ['en',_('English')], ['pt',_('Portuguese')], ['es',_('Spanish')] ]  
220 - # The option :size will set how many radios will be showed in each line  
221 - # Example: use :size => 3 as option if you want 3 radios by line  
222 - def radio_group( object_name, method, values, options = {} )  
223 - line_size = options[:size] || 0  
224 - line_item = 0  
225 - html = "\n"  
226 - values.each { |val, h_val|  
227 - id = object_name.to_s() +'_'+ method.to_s() +'_'+ val.to_s()  
228 - # Não está apresentando o sexo selecionado ao revisitar  
229 - # http://localhost:3000/myprofile/manuel/profile_editor/edit :-(  
230 - html += self.class.content_tag( 'span',  
231 - @template.radio_button( object_name, method, val,  
232 - :id => id, :object => @object ) +  
233 - self.class.content_tag( 'label', h_val, :for => id ),  
234 - :class => 'lineitem' + (line_item+=1).to_s() ) +"\n"  
235 - if line_item == line_size  
236 - line_item = 0  
237 - html += "<br />\n"  
238 - end  
239 - }  
240 - html += "<br />\n" if line_size == 0 || ( values.size % line_size ) > 0  
241 - column = object.class.columns_hash[method.to_s]  
242 - text =  
243 - ( column ?  
244 - column.human_name :  
245 - _(method.to_s.humanize)  
246 - )  
247 - label_html = self.class.content_tag 'label', text,  
248 - :class => 'formlabel'  
249 - control_html = self.class.content_tag 'div', html,  
250 - :class => 'formfield type-radio '+  
251 - 'fieldgroup linesize'+line_size.to_s()  
252 -  
253 - self.class.content_tag 'div', label_html + control_html,  
254 - :class => 'formfieldline'  
255 - end  
256 -  
257 - end  
258 -  
259 def category_color 157 def category_color
260 if @category.nil? 158 if @category.nil?
261 "" 159 ""
@@ -314,21 +212,6 @@ module ApplicationHelper @@ -314,21 +212,6 @@ module ApplicationHelper
314 end 212 end
315 alias icon_button button_without_text 213 alias icon_button button_without_text
316 214
317 - def submit_button(type, label, html_options = {})  
318 - bt_cancel = html_options[:cancel] ? button(:cancel, _('Cancel'), html_options[:cancel]) : ''  
319 -  
320 - html_options[:class] = [html_options[:class], 'submit'].compact.join(' ')  
321 -  
322 - the_class = "button with-text icon-#{type}"  
323 - if html_options.has_key?(:class)  
324 - the_class << ' ' << html_options[:class]  
325 - end  
326 -  
327 - bt_submit = submit_tag(label, html_options.merge(:class => the_class))  
328 -  
329 - bt_submit + bt_cancel  
330 - end  
331 -  
332 def button_to_function(type, label, js_code, html_options = {}) 215 def button_to_function(type, label, js_code, html_options = {})
333 html_options[:class] = "" unless html_options[:class] 216 html_options[:class] = "" unless html_options[:class]
334 html_options[:class] << " button #{type}" 217 html_options[:class] << " button #{type}"
@@ -473,15 +356,6 @@ module ApplicationHelper @@ -473,15 +356,6 @@ module ApplicationHelper
473 :class => 'vcard' 356 :class => 'vcard'
474 end 357 end
475 358
476 - def text_field_with_local_autocomplete(name, choices, html_options = {})  
477 - id = html_options[:id] || name  
478 -  
479 - text_field_tag(name, '', html_options) +  
480 - content_tag('div', '', :id => "autocomplete-for-#{id}", :class => 'auto-complete', :style => 'display: none;') +  
481 - javascript_tag('new Autocompleter.Local(%s, %s, %s)' % [ id.to_json, "autocomplete-for-#{id}".to_json, choices.to_json ] )  
482 -  
483 - end  
484 -  
485 def gravatar_url_for(email, options = {}) 359 def gravatar_url_for(email, options = {})
486 # Ta dando erro de roteamento 360 # Ta dando erro de roteamento
487 url_for( { :gravatar_id => Digest::MD5.hexdigest(email), 361 url_for( { :gravatar_id => Digest::MD5.hexdigest(email),
app/helpers/forms_helper.rb
1 module FormsHelper 1 module FormsHelper
2 2
  3 + # create a form field structure (as if it were generated with
  4 + # labelled_form_for), but with a customized control and label.
  5 + #
  6 + # If +field_id+ is not given, the underlying implementation will try to guess
  7 + # it from +field_html+ using a regular expression. In this case, make sure
  8 + # that the first ocurrance of id=['"]([^'"]*)['"] in +field_html+ if the one
  9 + # you want (i.e. the correct id for the control )
  10 + def labelled_form_field(label, field_html, field_id = nil)
  11 + NoosferoFormBuilder::output_field(label, field_html, field_id)
  12 + end
  13 +
  14 + alias_method :display_form_field, :labelled_form_field
  15 +
  16 + def labelled_fields_for(name, object = nil, options = {}, &proc)
  17 + object ||= instance_variable_get("@#{name}")
  18 + fields_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc)
  19 + end
  20 +
  21 + def labelled_form_for(name, object = nil, options = {}, &proc)
  22 + object ||= instance_variable_get("@#{name}")
  23 + form_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc)
  24 + end
  25 +
  26 + class NoosferoFormBuilder < ActionView::Helpers::FormBuilder
  27 + include GetText
  28 + extend ActionView::Helpers::TagHelper
  29 +
  30 + def self.output_field(text, field_html, field_id = nil)
  31 + # try to guess an id if none given
  32 + if field_id.nil?
  33 + field_html =~ /id=['"]([^'"]*)['"]/
  34 + field_id = $1
  35 + end
  36 + field_html =~ /type=['"]([^'"]*)['"]/
  37 + field_html =~ /<(\w*)/ unless $1
  38 + field_type = $1
  39 + field_class = 'formfield type-' + field_type if field_type
  40 +
  41 + label_html = content_tag('label', _(text), :class => 'formlabel', :for => field_id)
  42 + control_html = content_tag('div', field_html, :class => field_class )
  43 +
  44 + content_tag('div', label_html + control_html, :class => 'formfieldline' )
  45 + end
  46 +
  47 + (field_helpers - %w(hidden_field)).each do |selector|
  48 + src = <<-END_SRC
  49 + def #{selector}(field, *args, &proc)
  50 + column = object.class.columns_hash[field.to_s]
  51 + text =
  52 + ( column ?
  53 + column.human_name :
  54 + _(field.to_s.humanize)
  55 + )
  56 +
  57 + NoosferoFormBuilder::output_field(text, super)
  58 + end
  59 + END_SRC
  60 + class_eval src, __FILE__, __LINE__
  61 + end
  62 +
  63 + # Create a formatable radio collection
  64 + # Tha values parameter is a array of [value, label] arrays like this:
  65 + # [ ['en',_('English')], ['pt',_('Portuguese')], ['es',_('Spanish')] ]
  66 + # The option :size will set how many radios will be showed in each line
  67 + # Example: use :size => 3 as option if you want 3 radios by line
  68 + def radio_group( object_name, method, values, options = {} )
  69 + line_size = options[:size] || 0
  70 + line_item = 0
  71 + html = "\n"
  72 + values.each { |val, h_val|
  73 + id = object_name.to_s() +'_'+ method.to_s() +'_'+ val.to_s()
  74 + # Não está apresentando o sexo selecionado ao revisitar
  75 + # http://localhost:3000/myprofile/manuel/profile_editor/edit :-(
  76 + html += self.class.content_tag( 'span',
  77 + @template.radio_button( object_name, method, val,
  78 + :id => id, :object => @object ) +
  79 + self.class.content_tag( 'label', h_val, :for => id ),
  80 + :class => 'lineitem' + (line_item+=1).to_s() ) +"\n"
  81 + if line_item == line_size
  82 + line_item = 0
  83 + html += "<br />\n"
  84 + end
  85 + }
  86 + html += "<br />\n" if line_size == 0 || ( values.size % line_size ) > 0
  87 + column = object.class.columns_hash[method.to_s]
  88 + text =
  89 + ( column ?
  90 + column.human_name :
  91 + _(method.to_s.humanize)
  92 + )
  93 + label_html = self.class.content_tag 'label', text,
  94 + :class => 'formlabel'
  95 + control_html = self.class.content_tag 'div', html,
  96 + :class => 'formfield type-radio '+
  97 + 'fieldgroup linesize'+line_size.to_s()
  98 +
  99 + self.class.content_tag 'div', label_html + control_html,
  100 + :class => 'formfieldline'
  101 + end
  102 +
  103 + end
  104 +
  105 +
3 def generate_form( name, obj, fields={} ) 106 def generate_form( name, obj, fields={} )
4 labelled_form_for name, obj do |f| 107 labelled_form_for name, obj do |f|
5 f.text_field(:name) 108 f.text_field(:name)
@@ -30,6 +133,29 @@ module FormsHelper @@ -30,6 +133,29 @@ module FormsHelper
30 select_tag( name, options_from_collection_for_select(collection, value_method, text_method, selected), options) 133 select_tag( name, options_from_collection_for_select(collection, value_method, text_method, selected), options)
31 end 134 end
32 135
  136 + def submit_button(type, label, html_options = {})
  137 + bt_cancel = html_options[:cancel] ? button(:cancel, _('Cancel'), html_options[:cancel]) : ''
  138 +
  139 + html_options[:class] = [html_options[:class], 'submit'].compact.join(' ')
  140 +
  141 + the_class = "button with-text icon-#{type}"
  142 + if html_options.has_key?(:class)
  143 + the_class << ' ' << html_options[:class]
  144 + end
  145 +
  146 + bt_submit = submit_tag(label, html_options.merge(:class => the_class))
  147 +
  148 + bt_submit + bt_cancel
  149 + end
  150 +
  151 + def text_field_with_local_autocomplete(name, choices, html_options = {})
  152 + id = html_options[:id] || name
  153 +
  154 + text_field_tag(name, '', html_options) +
  155 + content_tag('div', '', :id => "autocomplete-for-#{id}", :class => 'auto-complete', :style => 'display: none;') +
  156 + javascript_tag('new Autocompleter.Local(%s, %s, %s)' % [ id.to_json, "autocomplete-for-#{id}".to_json, choices.to_json ] )
  157 + end
  158 +
33 protected 159 protected
34 def self.next_id_number 160 def self.next_id_number
35 if defined? @@id_num 161 if defined? @@id_num