Commit ca50f7df31fee3e800d1f4c61d36469d5677da30

Authored by AntonioTerceiro
1 parent 2d2b8519

ActionItem65: DRY approach to translation of forms



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@475 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/account_controller.rb
... ... @@ -27,7 +27,7 @@ class AccountController < ApplicationController
27 27 def signup
28 28 begin
29 29 @terms_of_use = virtual_community.terms_of_use
30   - terms_accepted = params[:user] ? params[:user].delete(:terms_accepted) : false
  30 + terms_accepted = params[:terms_accepted]
31 31 @user = User.new(params[:user])
32 32 return unless request.post?
33 33 if @terms_of_use and !terms_accepted
... ...
app/helpers/application_helper.rb
... ... @@ -153,7 +153,7 @@ module ApplicationHelper
153 153 # returns the current profile beign viewed.
154 154 #
155 155 # Make sure that you use this helper method only in contexts where there
156   - # should be a currnt profile (i.e. while viewing some profile's pages, or the
  156 + # should be a current profile (i.e. while viewing some profile's pages, or the
157 157 # profile info, etc), because if there is no profile an exception is thrown.
158 158 def profile
159 159 @profile || raise("There is no current profile")
... ... @@ -167,4 +167,23 @@ module ApplicationHelper
167 167 content_tag('div', content_tag('div', content_tag('label', label)) + html_for_field, :class => 'formfield')
168 168 end
169 169  
  170 + def labelled_form_for(name, object = nil, options = {}, &proc)
  171 + object ||= instance_variable_get("@#{name}")
  172 + form_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc)
  173 + end
  174 +
  175 + class NoosferoFormBuilder < ActionView::Helpers::FormBuilder
  176 + include GetText
  177 +
  178 + (field_helpers - %w(hidden_field)).each do |selector|
  179 + src = <<-END_SRC
  180 + def #{selector}(field, *args, &proc)
  181 + column = object.class.columns_hash[field.to_s]
  182 + "<div class='formfield'>" + "<div><label for='\#{field}'>" + (column ? column.human_name : _(object.class.name + "|" + field.to_s.humanize)) + "</label></div>" + super + "</div>"
  183 + end
  184 + END_SRC
  185 + class_eval src, __FILE__, __LINE__
  186 + end
  187 + end
  188 +
170 189 end
... ...
app/models/person_info.rb
... ... @@ -6,9 +6,9 @@ class PersonInfo &lt; ActiveRecord::Base
6 6  
7 7 def summary
8 8 [
9   - [ _('Name'), self.name ],
10   - [ _('Address'), self.address ],
11   - [ _('Contact Information'), self.contact_information ],
  9 + [ PersonInfo.columns_hash['name'].human_name, self.name ],
  10 + [ PersonInfo.columns_hash['address'].hunam_name, self.address ],
  11 + [ PersonInfo.columns_hash['contact_information'], self.contact_information ],
12 12 ]
13 13 end
14 14  
... ...
app/models/user.rb
... ... @@ -4,6 +4,9 @@ require &#39;digest/sha1&#39;
4 4 # Rails generator.
5 5 class User < ActiveRecord::Base
6 6  
  7 + N_('User|Password')
  8 + N_('User|Password confirmation')
  9 +
7 10 after_create do |user|
8 11 Person.create!(:identifier => user.login, :name => user.login, :user_id => user.id)
9 12 end
... ...
app/views/account/login.rhtml
1 1 <h1><%= _('Login') %></h1>
2 2  
3 3 <% form_tag do -%>
4   -<p><label for="login"> <%= _('Login') %> </label><br/>
  4 +<p><label for="login"> <%= _('Username') %> </label><br/>
5 5 <%= text_field_tag 'login' %></p>
6 6  
7 7 <p><label for="password"> <%= _('Password') %></label><br/>
... ...
app/views/account/signup.rhtml
1 1 <h1><%= _('Register') %></h1>
2 2  
3 3 <%= error_messages_for :user %>
4   -<% form_for :user do |f| -%>
5   -<p><label for="login">Login</label><br/>
6   -<%= f.text_field :login %></p>
7   -
8   -<p><label for="email">Email</label><br/>
9   -<%= f.text_field :email %></p>
10   -
11   -<p><label for="password">Password</label><br/>
12   -<%= f.password_field :password %></p>
13   -
14   -<p><label for="password_confirmation">Confirm Password</label><br/>
15   -<%= f.password_field :password_confirmation %></p>
  4 +<% labelled_form_for :user, @user do |f| -%>
  5 +
  6 +<%= f.text_field :login %>
  7 +<%= f.text_field :email %>
  8 +<%= f.password_field :password %>
  9 +<%= f.password_field :password_confirmation %>
16 10  
17 11 <% if @terms_of_use %>
18   - <p> <%= @terms_of_use %> </p>
19   - <p> <%= check_box_tag 'user[terms_accepted]' %>
20   - <label for="terms_accepted">I accept the terms of use</label> </p>
  12 + <p> <%= @terms_of_use %> </p>
  13 + <p> <%= check_box_tag 'terms_accepted' %>
  14 + <label for="terms_accepted"><%= _('I accept the terms of use') %></label> </p>
21 15 <% end %>
22 16  
23 17 <p><%= submit_tag 'Sign up' %></p>
... ...