Commit 9e654d48133c2fcaf1dc41a7b3c17b8693850a65

Authored by Fabio Teixeira
1 parent 18b4327e

correcoes_aderencia: Make Full Name validation ignore "áàâãéèêíïóôõöú"

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Signed-off-by: Rodrigo Medeiros <rodrigo.mss01@gmail.com>
lib/ext/person.rb
  1 +# encoding: utf-8
  2 +
1 require_dependency 'person' 3 require_dependency 'person'
2 4
3 class Person 5 class Person
@@ -58,17 +60,19 @@ class Person @@ -58,17 +60,19 @@ class Person
58 end 60 end
59 61
60 def validate_full_name 62 def validate_full_name
61 - reg_firsts_char = /(^|\s)([a-z]|[0-9])/  
62 - reg_special_char = /[^\w\*\s]/ 63 + full_validation = /([^\w\*\s*])|(^|\s)([a-z]|[0-9])/
  64 + partial_validation = /[^\w\*\s]/
63 invalid = false 65 invalid = false
64 66
65 return false if self.name.blank? 67 return false if self.name.blank?
66 68
67 - self.name.split(" ").each do |value| 69 + validation_name = replace_some_special_chars(self.name)
  70 +
  71 + validation_name.split(" ").each do |value|
68 invalid = if value.length > 3 72 invalid = if value.length > 3
69 - reg_firsts_char.match(value) || reg_special_char.match(value) 73 + full_validation.match(value)
70 else 74 else
71 - reg_special_char.match(value) 75 + partial_validation.match(value)
72 end 76 end
73 end 77 end
74 78
@@ -82,4 +86,24 @@ class Person @@ -82,4 +86,24 @@ class Person
82 def software? 86 def software?
83 false 87 false
84 end 88 end
  89 +
  90 + private
  91 +
  92 + def replace_some_special_chars text
  93 + text.gsub(/([áàâãéèêíïóôõöú])/) do |value|
  94 + if( ["á","à","â","ã"].include?(value) )
  95 + "a"
  96 + elsif( ["é","è","ê"].include?(value) )
  97 + "e"
  98 + elsif( ["í","ï"].include?(value) )
  99 + "i"
  100 + elsif ( ["ó","ô","õ","ö"].include?(value) )
  101 + "o"
  102 + elsif( ["ú"].indexOf(value) )
  103 + "u"
  104 + else
  105 + value
  106 + end
  107 + end
  108 + end
85 end 109 end
public/mpog-user-validations.js
@@ -148,17 +148,37 @@ @@ -148,17 +148,37 @@
148 } 148 }
149 149
150 function set_full_name_validation() { 150 function set_full_name_validation() {
  151 + // Sorry, I know its ugly. But I cant get ([^\w\*\s*])|(^|\s)([a-z]|[0-9])
  152 + // to ignore Brazilian not so much special chars in names
  153 + function replace_some_special_chars(text) {
  154 + return text.replace(/([áàâãéèêíïóôõöú])/g, function(value){
  155 + if( ["á","à","â","ã"].indexOf(value) != -1 )
  156 + return "a";
  157 + else if( ["é","è","ê"].indexOf(value) != -1 )
  158 + return "e";
  159 + else if( ["í","ï"].indexOf(value) != -1 )
  160 + return "i";
  161 + else if ( ["ó","ô","õ","ö"].indexOf(value) != -1 )
  162 + return "o";
  163 + else if( ["ú"].indexOf(value) != -1 )
  164 + return "u";
  165 + else
  166 + return value;
  167 + });
  168 + }
  169 +
151 function is_invalid_formated(text) { 170 function is_invalid_formated(text) {
152 - var reg_firsts_char = /(^|\s)([a-z]|[0-9])/g;  
153 - var reg_special_char = /[^\w\*\s*]/g;  
154 - var invalid = false; 171 + var full_validation = /([^\w\*\s*])|(^|\s)([a-z]|[0-9])/; // no special chars and do not initialize with no capital latter
  172 + var partial_validation = /[^\w\*\s*]/; // no special chars
  173 + text = replace_some_special_chars(text);
155 var slices = text.split(" "); 174 var slices = text.split(" ");
  175 + var invalid = false;
156 176
157 for(var i = 0; i < slices.length; i++) { 177 for(var i = 0; i < slices.length; i++) {
158 if( slices[i].length > 3 ) { 178 if( slices[i].length > 3 ) {
159 - invalid = reg_firsts_char.test(slices[i]) || reg_special_char.test(slices[i]); 179 + invalid = full_validation.test(slices[i]);
160 } else { 180 } else {
161 - invalid = reg_special_char.test(slices[i]); 181 + invalid = partial_validation.test(slices[i]);
162 } 182 }
163 183
164 if(invalid) break; 184 if(invalid) break;
test/unit/mpog_person_test.rb
  1 +# encoding: utf-8
  2 +
1 require File.dirname(__FILE__) + '/../../../../test/test_helper' 3 require File.dirname(__FILE__) + '/../../../../test/test_helper'
2 4
3 class MpogSoftwarePluginPersonTest < ActiveSupport::TestCase 5 class MpogSoftwarePluginPersonTest < ActiveSupport::TestCase
@@ -8,6 +10,13 @@ class MpogSoftwarePluginPersonTest &lt; ActiveSupport::TestCase @@ -8,6 +10,13 @@ class MpogSoftwarePluginPersonTest &lt; ActiveSupport::TestCase
8 assert_equal true, p.save 10 assert_equal true, p.save
9 end 11 end
10 12
  13 + should 'save person with a valid full name with accents' do
  14 + p = Person::new :name=>'Jônatàs dâ Sîlvã Jösé', :identifier=>"jonatas-jose-da-silva"
  15 + p.user = fast_create(:user)
  16 +
  17 + assert_equal true, p.save
  18 + end
  19 +
11 should 'not save person whose name has not capital letter' do 20 should 'not save person whose name has not capital letter' do
12 p = Person::new :name=>"simple name" 21 p = Person::new :name=>"simple name"
13 assert !p.save, _("Name Should begin with a capital letter and no special characters") 22 assert !p.save, _("Name Should begin with a capital letter and no special characters")