Commit 9e45dbee805cabf40342f3dcc6968770902ee5ac
1 parent
31719255
Exists in
master
and in
23 other branches
ActionItem501: moved some forms helper methods from forms_helper.rb to applicati…
…on_helper.rb because of issues with tranlation of labels git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2168 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
104 additions
and
104 deletions
Show diff stats
app/helpers/application_helper.rb
| ... | ... | @@ -538,4 +538,107 @@ module ApplicationHelper |
| 538 | 538 | '\n\n\'+this.href)">\2</a>' ) |
| 539 | 539 | end |
| 540 | 540 | |
| 541 | + # Should be on the forms_helper file but when its there the translation of labels doesn't work | |
| 542 | + class NoosferoFormBuilder < ActionView::Helpers::FormBuilder | |
| 543 | + include GetText | |
| 544 | + extend ActionView::Helpers::TagHelper | |
| 545 | + | |
| 546 | + def self.output_field(text, field_html, field_id = nil) | |
| 547 | + # try to guess an id if none given | |
| 548 | + if field_id.nil? | |
| 549 | + field_html =~ /id=['"]([^'"]*)['"]/ | |
| 550 | + field_id = $1 | |
| 551 | + end | |
| 552 | + field_html =~ /type=['"]([^'"]*)['"]/ | |
| 553 | + field_html =~ /<(\w*)/ unless $1 | |
| 554 | + field_type = $1 | |
| 555 | + field_class = 'formfield type-' + field_type if field_type | |
| 556 | + | |
| 557 | + label_html = content_tag('label', gettext(text), :class => 'formlabel', :for => field_id) | |
| 558 | + control_html = content_tag('div', field_html, :class => field_class ) | |
| 559 | + | |
| 560 | + content_tag('div', label_html + control_html, :class => 'formfieldline' ) | |
| 561 | + end | |
| 562 | + | |
| 563 | + (field_helpers - %w(hidden_field)).each do |selector| | |
| 564 | + src = <<-END_SRC | |
| 565 | + def #{selector}(field, *args, &proc) | |
| 566 | + column = object.class.columns_hash[field.to_s] | |
| 567 | + text = | |
| 568 | + ( column ? | |
| 569 | + column.human_name : | |
| 570 | + field.to_s.humanize | |
| 571 | + ) | |
| 572 | + | |
| 573 | + NoosferoFormBuilder::output_field(text, super) | |
| 574 | + end | |
| 575 | + END_SRC | |
| 576 | + class_eval src, __FILE__, __LINE__ | |
| 577 | + end | |
| 578 | + | |
| 579 | + # Create a formatable radio collection | |
| 580 | + # Tha values parameter is a array of [value, label] arrays like this: | |
| 581 | + # [ ['en',_('English')], ['pt',_('Portuguese')], ['es',_('Spanish')] ] | |
| 582 | + # The option :size will set how many radios will be showed in each line | |
| 583 | + # Example: use :size => 3 as option if you want 3 radios by line | |
| 584 | + def radio_group( object_name, method, values, options = {} ) | |
| 585 | + line_size = options[:size] || 0 | |
| 586 | + line_item = 0 | |
| 587 | + html = "\n" | |
| 588 | + values.each { |val, h_val| | |
| 589 | + id = object_name.to_s() +'_'+ method.to_s() +'_'+ val.to_s() | |
| 590 | + # Não está apresentando o sexo selecionado ao revisitar | |
| 591 | + # http://localhost:3000/myprofile/manuel/profile_editor/edit :-( | |
| 592 | + html += self.class.content_tag( 'span', | |
| 593 | + @template.radio_button( object_name, method, val, | |
| 594 | + :id => id, :object => @object ) + | |
| 595 | + self.class.content_tag( 'label', h_val, :for => id ), | |
| 596 | + :class => 'lineitem' + (line_item+=1).to_s() ) +"\n" | |
| 597 | + if line_item == line_size | |
| 598 | + line_item = 0 | |
| 599 | + html += "<br />\n" | |
| 600 | + end | |
| 601 | + } | |
| 602 | + html += "<br />\n" if line_size == 0 || ( values.size % line_size ) > 0 | |
| 603 | + column = object.class.columns_hash[method.to_s] | |
| 604 | + text = | |
| 605 | + ( column ? | |
| 606 | + column.human_name : | |
| 607 | + _(method.to_s.humanize) | |
| 608 | + ) | |
| 609 | + label_html = self.class.content_tag 'label', text, | |
| 610 | + :class => 'formlabel' | |
| 611 | + control_html = self.class.content_tag 'div', html, | |
| 612 | + :class => 'formfield type-radio '+ | |
| 613 | + 'fieldgroup linesize'+line_size.to_s() | |
| 614 | + | |
| 615 | + self.class.content_tag 'div', label_html + control_html, | |
| 616 | + :class => 'formfieldline' | |
| 617 | + end | |
| 618 | + | |
| 619 | + end | |
| 620 | + | |
| 621 | + # create a form field structure (as if it were generated with | |
| 622 | + # labelled_form_for), but with a customized control and label. | |
| 623 | + # | |
| 624 | + # If +field_id+ is not given, the underlying implementation will try to guess | |
| 625 | + # it from +field_html+ using a regular expression. In this case, make sure | |
| 626 | + # that the first ocurrance of id=['"]([^'"]*)['"] in +field_html+ if the one | |
| 627 | + # you want (i.e. the correct id for the control ) | |
| 628 | + def labelled_form_field(label, field_html, field_id = nil) | |
| 629 | + NoosferoFormBuilder::output_field(label, field_html, field_id) | |
| 630 | + end | |
| 631 | + | |
| 632 | + alias_method :display_form_field, :labelled_form_field | |
| 633 | + | |
| 634 | + def labelled_fields_for(name, object = nil, options = {}, &proc) | |
| 635 | + object ||= instance_variable_get("@#{name}") | |
| 636 | + fields_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc) | |
| 637 | + end | |
| 638 | + | |
| 639 | + def labelled_form_for(name, object = nil, options = {}, &proc) | |
| 640 | + object ||= instance_variable_get("@#{name}") | |
| 641 | + form_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc) | |
| 642 | + end | |
| 643 | + | |
| 541 | 644 | end | ... | ... |
app/helpers/forms_helper.rb
| 1 | 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 | - | |
| 106 | 3 | def generate_form( name, obj, fields={} ) |
| 107 | 4 | labelled_form_for name, obj do |f| |
| 108 | 5 | f.text_field(:name) |
| ... | ... | @@ -137,7 +34,7 @@ module FormsHelper |
| 137 | 34 | bt_cancel = html_options[:cancel] ? button(:cancel, _('Cancel'), html_options[:cancel]) : '' |
| 138 | 35 | |
| 139 | 36 | html_options[:class] = [html_options[:class], 'submit'].compact.join(' ') |
| 140 | - | |
| 37 | + | |
| 141 | 38 | the_class = "button with-text icon-#{type}" |
| 142 | 39 | if html_options.has_key?(:class) |
| 143 | 40 | the_class << ' ' << html_options[:class] | ... | ... |