From 0b4c072dea87683a324665144be219848dabaa00 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 7 May 2012 09:29:11 -0300 Subject: [PATCH] [stoa] Signup Complement --- plugins/stoa/controllers/stoa_plugin_controller.rb | 8 ++++++++ plugins/stoa/lib/stoa_plugin.rb | 29 ++++------------------------- plugins/stoa/public/images/grey-bg.png | Bin 0 -> 187 bytes plugins/stoa/public/javascripts/jquery.observe_field.js | 39 +++++++++++++++++++++++++++++++++++++++ plugins/stoa/public/javascripts/signup_complement.js | 41 +++++++++++++++++++++++++++++++++++++++++ plugins/stoa/public/style.css | 9 ++++++++- plugins/stoa/test/functional/account_controller_test.rb | 23 ++++++++++++++++------- plugins/stoa/test/functional/stoa_plugin_controller_test.rb | 36 +++++++++++++++++++++++++++--------- 8 files changed, 143 insertions(+), 42 deletions(-) create mode 100644 plugins/stoa/public/images/grey-bg.png create mode 100644 plugins/stoa/public/javascripts/jquery.observe_field.js create mode 100644 plugins/stoa/public/javascripts/signup_complement.js diff --git a/plugins/stoa/controllers/stoa_plugin_controller.rb b/plugins/stoa/controllers/stoa_plugin_controller.rb index 300e38d..ffb1d8a 100644 --- a/plugins/stoa/controllers/stoa_plugin_controller.rb +++ b/plugins/stoa/controllers/stoa_plugin_controller.rb @@ -32,4 +32,12 @@ class StoaPluginController < PublicController end end + def check_cpf + begin + render :text => { :exists => !StoaPlugin::UspUser.find_by_codpes(params[:usp_id]).cpf.blank? }.to_json + rescue Exception => exception + render :text => { :exists => false, :error => {:message => exception.to_s, :backtrace => exception.backtrace} }.to_json + end + end + end diff --git a/plugins/stoa/lib/stoa_plugin.rb b/plugins/stoa/lib/stoa_plugin.rb index 651a47f..376a960 100644 --- a/plugins/stoa/lib/stoa_plugin.rb +++ b/plugins/stoa/lib/stoa_plugin.rb @@ -20,37 +20,16 @@ class StoaPlugin < Noosfero::Plugin def signup_extra_contents lambda { required(labelled_form_field(_('USP number'), text_field_tag('profile_data[usp_id]', '', :id => 'usp_id_field'))) + - labelled_form_field(_('Select a confirmation data'), select_tag('confirmation_field', - options_for_select([['CPF','cpf'], [_('Birth date (yyyy-mm-dd)'), 'birth_date']]) - )) + - required(labelled_form_field(_('Confirmation value'), text_field_tag('confirmation_value', '', :placeholder=>_('Confirmation value')))) + - javascript_tag(<<-EOF - jQuery("#usp_id_field").change(function(){ - var me=this; - jQuery(this).addClass('checking').removeClass('validated'); - jQuery.getJSON('#{url_for(:controller => 'stoa_plugin', :action => 'check_usp_id')}?usp_id='+this.value, - function(data){ - if(data.exists) jQuery(me).removeClass('checking').addClass('validated'); - else jQuery(me).removeClass('checking').addClass('invalid'); - if(data.error) displayValidationUspIdError(data.error); - } - ); - }); - - function displayValidationUspIdError(error){ - jQuery.colorbox({html: '

'+error.message+'

'+error.backtrace.join("
"), - height: "80%", - width: "70%" }); - } - EOF - ) + content_tag('div', required(labelled_form_field(_('Birth date (yyyy-mm-dd)'), text_field_tag('birth_date', ''))), :id => 'signup-birth-date', :style => 'display: none') + + content_tag('div', required(labelled_form_field(_('CPF'), text_field_tag('cpf', ''))), :id => 'signup-cpf', :style => 'display:none') + + javascript_include_tag('../plugins/stoa/javascripts/jquery.observe_field', '../plugins/stoa/javascripts/signup_complement') } end def account_controller_filters block = lambda do if request.post? - if !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[:confirmation_value]) + if !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]]) @person = Person.new @person.errors.add(:usp_id, _(' validation failed')) render :action => :signup diff --git a/plugins/stoa/public/images/grey-bg.png b/plugins/stoa/public/images/grey-bg.png new file mode 100644 index 0000000..41b42ae Binary files /dev/null and b/plugins/stoa/public/images/grey-bg.png differ diff --git a/plugins/stoa/public/javascripts/jquery.observe_field.js b/plugins/stoa/public/javascripts/jquery.observe_field.js new file mode 100644 index 0000000..b2d72e5 --- /dev/null +++ b/plugins/stoa/public/javascripts/jquery.observe_field.js @@ -0,0 +1,39 @@ +// jquery.observe_field.js + + +(function( $ ){ + + jQuery.fn.observe_field = function(frequency, callback) { + + frequency = frequency * 1000; // translate to milliseconds + + return this.each(function(){ + var $this = $(this); + var prev = $this.val(); + + var check = function() { + var val = $this.val(); + if(prev != val){ + prev = val; + $this.map(callback); // invokes the callback on $this + } + }; + + var reset = function() { + if(ti){ + clearInterval(ti); + ti = setInterval(check, frequency); + } + }; + + check(); + var ti = setInterval(check, frequency); // invoke check periodically + + // reset counter after user interaction + $this.bind('keyup click mousemove', reset); //mousemove is for selects + }); + + }; + +})( jQuery ); + diff --git a/plugins/stoa/public/javascripts/signup_complement.js b/plugins/stoa/public/javascripts/signup_complement.js new file mode 100644 index 0000000..3f89ba1 --- /dev/null +++ b/plugins/stoa/public/javascripts/signup_complement.js @@ -0,0 +1,41 @@ +jQuery("#usp_id_field").observe_field(1, function(){ + var me=this; + jQuery(this).addClass('checking').removeClass('validated'); + jQuery.getJSON('/plugin/stoa/check_usp_id?usp_id='+me.value, + function(data){ + if(data.exists) { + jQuery.getJSON('/plugin/stoa/check_cpf?usp_id='+me.value, + function(data){ + if(data.exists){ + jQuery('#signup-birth-date').hide(); + jQuery('#signup-cpf').show(); + jQuery('#confirmation_field').remove(); + jQuery('#signup-form').append('') + } + else { + jQuery('#signup-cpf').hide(); + jQuery('#signup-birth-date').show(); + jQuery('#confirmation_field').remove(); + jQuery('#signup-form').append('') + } + jQuery('#signup-form .submit').attr('disabled', false); + jQuery(me).removeClass('checking').addClass('validated'); + }); + } + else { + jQuery('#signup-form .submit').attr('disabled', true); + jQuery('#signup-birth-date').hide(); + jQuery('#signup-cpf').hide(); + jQuery(me).removeClass('checking').addClass('invalid'); + } + if(data.error) displayValidationUspIdError(data.error); + } +); +}); + +function displayValidationUspIdError(error){ + jQuery.colorbox({html: '

'+error.message+'

'+error.backtrace.join("
"), + height: "80%", + width: "70%" }); +} + diff --git a/plugins/stoa/public/style.css b/plugins/stoa/public/style.css index 18346f8..b651891 100644 --- a/plugins/stoa/public/style.css +++ b/plugins/stoa/public/style.css @@ -1,5 +1,12 @@ #signup-form label[for="usp_id_field"], -#signup-form label[for="confirmation_field"] { +#signup-form label[for="cpf"], +#signup-form label[for="birth_date"] { display: block; } +#content #signup-form input.submit[disabled=""] { + background-image: url(/plugins/stoa/images/grey-bg.png); + text-shadow: 0 -1px 0 #686156; + cursor: default; +} + diff --git a/plugins/stoa/test/functional/account_controller_test.rb b/plugins/stoa/test/functional/account_controller_test.rb index 75a113c..f2c0341 100644 --- a/plugins/stoa/test/functional/account_controller_test.rb +++ b/plugins/stoa/test/functional/account_controller_test.rb @@ -6,26 +6,35 @@ class AccountController; def rescue_action(e) raise e end; end class AccountControllerTest < ActionController::TestCase + SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] + def setup @controller = AccountController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new - environment = Environment.default - environment.enabled_plugins = ['StoaPlugin'] - environment.save! @db = Tempfile.new('stoa-test') configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path} + ActiveRecord::Base.establish_connection(:stoa) + ActiveRecord::Schema.verbose = false + ActiveRecord::Schema.create_table "pessoa" do |t| + t.integer "codpes" + t.text "numcpf" + t.date "dtanas" + end + ActiveRecord::Base.establish_connection(:test) + StoaPlugin::UspUser.create!(:codpes => 12345678, :cpf => Digest::MD5.hexdigest(SALT+'12345678'), :birth_date => '1970-01-30') + Environment.default.enable_plugin(StoaPlugin.name) end should 'fail if confirmation value doesn\'t match' do - StoaPlugin::UspUser.stubs(:matches?).returns(false) - post :signup, :profile_data => {:usp_id => '87654321'}, :confirmation_field => 'cpf', :confirmation_value => '00000000' + #StoaPlugin::UspUser.stubs(:matches?).returns(false) + post :signup, :profile_data => {:usp_id => '12345678'}, :confirmation_field => 'cpf', :cpf => '00000000' assert_not_nil assigns(:person).errors[:usp_id] end should 'pass if confirmation value matches' do - StoaPlugin::UspUser.stubs(:matches?).returns(true) - post :signup, :profile_data => {:usp_id => '87654321'}, :confirmation_field => 'cpf', :confirmation_value => '12345678' + #StoaPlugin::UspUser.stubs(:matches?).returns(true) + post :signup, :profile_data => {:usp_id => '12345678'}, :confirmation_field => 'cpf', :cpf => '12345678' assert_nil assigns(:person).errors[:usp_id] end diff --git a/plugins/stoa/test/functional/stoa_plugin_controller_test.rb b/plugins/stoa/test/functional/stoa_plugin_controller_test.rb index 68a3091..7d0ca82 100644 --- a/plugins/stoa/test/functional/stoa_plugin_controller_test.rb +++ b/plugins/stoa/test/functional/stoa_plugin_controller_test.rb @@ -6,16 +6,25 @@ class StoaPluginController; def rescue_action(e) raise e end; end class StoaPluginControllerTest < ActionController::TestCase + SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] + def setup @controller = StoaPluginController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new - @user = create_user('real_user', :password => '123456', :password_confirmation => '123456') - environment = Environment.default - environment.enabled_plugins = ['StoaPlugin'] - environment.save! @db = Tempfile.new('stoa-test') configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path} + ActiveRecord::Base.establish_connection(:stoa) + ActiveRecord::Schema.verbose = false + ActiveRecord::Schema.create_table "pessoa" do |t| + t.integer "codpes" + t.text "numcpf" + t.date "dtanas" + end + ActiveRecord::Base.establish_connection(:test) + @user = User.find_by_login('real_user') || create_user('real_user', :password => '123456', :password_confirmation => '123456') + StoaPlugin::UspUser.create!(:codpes => 12345678, :cpf => Digest::MD5.hexdigest(SALT+'12345678'), :birth_date => '1970-01-30') + Environment.default.enable_plugin(StoaPlugin.name) end attr_accessor :user @@ -52,18 +61,27 @@ class StoaPluginControllerTest < ActionController::TestCase assert_equal user.login, json_response['username'] end + should 'check valid usp id' do + get :check_usp_id, :usp_id => '12345678' + assert json_response['exists'] + end + should 'check invalid usp id' do - StoaPlugin::UspUser.stubs(:exists?).returns(false) - get :check_usp_id, :usp_id => '987654321' + get :check_usp_id, :usp_id => '87654321' assert !json_response['exists'] end - should 'check valid usp id' do - StoaPlugin::UspUser.stubs(:exists?).returns(true) - get :check_usp_id, :usp_id => '987654321' + should 'check existent cpf' do + get :check_cpf, :usp_id => '12345678' assert json_response['exists'] end + should 'check not existent cpf' do + StoaPlugin::UspUser.create(:codpes => 87654321, :birth_date => '1970-01-30') + get :check_cpf, :usp_id => '87654321' + assert !json_response['exists'] + end + private def json_response -- libgit2 0.21.2