From 81340e011ae72e8331e4f955ab7e5b2573926a31 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Sat, 15 Sep 2007 18:11:54 +0000 Subject: [PATCH] ActionItem12: making use of ActiveRecord validation to check acceptance of terms --- app/controllers/account_controller.rb | 22 ++++++++++------------ app/models/user.rb | 2 ++ app/views/account/signup.rhtml | 10 +++++++--- db/migrate/006_create_users.rb | 3 +++ test/functional/account_controller_test.rb | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index f648a33..a20ea3a 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -26,20 +26,18 @@ class AccountController < ApplicationController # action to register an user to the application def signup begin - @terms_of_use = virtual_community.terms_of_use - terms_accepted = params[:terms_accepted] @user = User.new(params[:user]) - return unless request.post? - if @terms_of_use and !terms_accepted - flash[:notice] = _("You have to accept the terms of service to signup") - return + @user.terms_of_use = virtual_community.terms_of_use + @terms_of_use = virtual_community.terms_of_use + + if request.post? + @user.save! + @user.person.virtual_community = virtual_community + @user.person.save! + self.current_user = @user + redirect_back_or_default(:controller => 'account', :action => 'index') + flash[:notice] = _("Thanks for signing up!") end - @user.save! - @user.person.virtual_community = virtual_community - @user.person.save! - self.current_user = @user - redirect_back_or_default(:controller => 'account', :action => 'index') - flash[:notice] = _("Thanks for signing up!") rescue ActiveRecord::RecordInvalid render :action => 'signup' end diff --git a/app/models/user.rb b/app/models/user.rb index 6de91e0..bf94c34 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,6 +27,8 @@ class User < ActiveRecord::Base validates_uniqueness_of :login, :email, :case_sensitive => false before_save :encrypt_password + 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.') + # Authenticates a user by their login name and unencrypted password. Returns the user or nil. def self.authenticate(login, password) u = find_by_login(login) # need to get the salt diff --git a/app/views/account/signup.rhtml b/app/views/account/signup.rhtml index 8e48b3f..b0d6fcc 100644 --- a/app/views/account/signup.rhtml +++ b/app/views/account/signup.rhtml @@ -9,9 +9,13 @@ <%= f.password_field :password_confirmation %> <% if @terms_of_use %> -

<%= @terms_of_use %>

-

<%= check_box_tag 'terms_accepted' %> -

+

+ <%= @terms_of_use %> +

+

+ <%= check_box 'user', 'terms_accepted' %> + <%= _('I accept the terms of use') %> +

<% end %>

<%= submit_tag 'Sign up' %>

diff --git a/db/migrate/006_create_users.rb b/db/migrate/006_create_users.rb index 9c78b5f..aeefe83 100644 --- a/db/migrate/006_create_users.rb +++ b/db/migrate/006_create_users.rb @@ -9,6 +9,9 @@ class CreateUsers < ActiveRecord::Migration t.column :updated_at, :datetime t.column :remember_token, :string t.column :remember_token_expires_at, :datetime + + t.column :terms_of_use, :text + t.column :terms_accepted, :string, :limit => 1 end end diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 7d96c26..897f698 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -79,7 +79,7 @@ class AccountControllerTest < Test::Unit::TestCase def test_shoud_save_with_acceptance_of_terms_of_use_on_signup assert_difference User, :count do VirtualCommunity.default.update_attributes(:terms_of_use => 'some terms ...') - create_user(:terms_accepted => true) + create_user(:terms_accepted => '1') assert_response :redirect end end -- libgit2 0.21.2