Commit 81340e011ae72e8331e4f955ab7e5b2573926a31

Authored by AntonioTerceiro
1 parent 4ee513ea

ActionItem12: making use of ActiveRecord validation to check acceptance of terms



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@485 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/account_controller.rb
@@ -26,20 +26,18 @@ class AccountController < ApplicationController @@ -26,20 +26,18 @@ class AccountController < ApplicationController
26 # action to register an user to the application 26 # action to register an user to the application
27 def signup 27 def signup
28 begin 28 begin
29 - @terms_of_use = virtual_community.terms_of_use  
30 - terms_accepted = params[:terms_accepted]  
31 @user = User.new(params[:user]) 29 @user = User.new(params[:user])
32 - return unless request.post?  
33 - if @terms_of_use and !terms_accepted  
34 - flash[:notice] = _("You have to accept the terms of service to signup")  
35 - return 30 + @user.terms_of_use = virtual_community.terms_of_use
  31 + @terms_of_use = virtual_community.terms_of_use
  32 +
  33 + if request.post?
  34 + @user.save!
  35 + @user.person.virtual_community = virtual_community
  36 + @user.person.save!
  37 + self.current_user = @user
  38 + redirect_back_or_default(:controller => 'account', :action => 'index')
  39 + flash[:notice] = _("Thanks for signing up!")
36 end 40 end
37 - @user.save!  
38 - @user.person.virtual_community = virtual_community  
39 - @user.person.save!  
40 - self.current_user = @user  
41 - redirect_back_or_default(:controller => 'account', :action => 'index')  
42 - flash[:notice] = _("Thanks for signing up!")  
43 rescue ActiveRecord::RecordInvalid 41 rescue ActiveRecord::RecordInvalid
44 render :action => 'signup' 42 render :action => 'signup'
45 end 43 end
app/models/user.rb
@@ -27,6 +27,8 @@ class User < ActiveRecord::Base @@ -27,6 +27,8 @@ class User < ActiveRecord::Base
27 validates_uniqueness_of :login, :email, :case_sensitive => false 27 validates_uniqueness_of :login, :email, :case_sensitive => false
28 before_save :encrypt_password 28 before_save :encrypt_password
29 29
  30 + validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('%{fn} must be checked in order to signup.')
  31 +
30 # Authenticates a user by their login name and unencrypted password. Returns the user or nil. 32 # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
31 def self.authenticate(login, password) 33 def self.authenticate(login, password)
32 u = find_by_login(login) # need to get the salt 34 u = find_by_login(login) # need to get the salt
app/views/account/signup.rhtml
@@ -9,9 +9,13 @@ @@ -9,9 +9,13 @@
9 <%= f.password_field :password_confirmation %> 9 <%= f.password_field :password_confirmation %>
10 10
11 <% if @terms_of_use %> 11 <% if @terms_of_use %>
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> 12 + <p>
  13 + <%= @terms_of_use %>
  14 + </p>
  15 + <p>
  16 + <%= check_box 'user', 'terms_accepted' %>
  17 + <%= _('I accept the terms of use') %>
  18 + </p>
15 <% end %> 19 <% end %>
16 20
17 <p><%= submit_tag 'Sign up' %></p> 21 <p><%= submit_tag 'Sign up' %></p>
db/migrate/006_create_users.rb
@@ -9,6 +9,9 @@ class CreateUsers &lt; ActiveRecord::Migration @@ -9,6 +9,9 @@ class CreateUsers &lt; ActiveRecord::Migration
9 t.column :updated_at, :datetime 9 t.column :updated_at, :datetime
10 t.column :remember_token, :string 10 t.column :remember_token, :string
11 t.column :remember_token_expires_at, :datetime 11 t.column :remember_token_expires_at, :datetime
  12 +
  13 + t.column :terms_of_use, :text
  14 + t.column :terms_accepted, :string, :limit => 1
12 end 15 end
13 end 16 end
14 17
test/functional/account_controller_test.rb
@@ -79,7 +79,7 @@ class AccountControllerTest &lt; Test::Unit::TestCase @@ -79,7 +79,7 @@ class AccountControllerTest &lt; Test::Unit::TestCase
79 def test_shoud_save_with_acceptance_of_terms_of_use_on_signup 79 def test_shoud_save_with_acceptance_of_terms_of_use_on_signup
80 assert_difference User, :count do 80 assert_difference User, :count do
81 VirtualCommunity.default.update_attributes(:terms_of_use => 'some terms ...') 81 VirtualCommunity.default.update_attributes(:terms_of_use => 'some terms ...')
82 - create_user(:terms_accepted => true) 82 + create_user(:terms_accepted => '1')
83 assert_response :redirect 83 assert_response :redirect
84 end 84 end
85 end 85 end