Commit 9596b3aed68ce86eca661acd75196ff04cc6cd3c

Authored by Thiago Ribeiro
2 parents 979d6052 f1cad301

Merge branch 'modal_fine_tune' into 'master'

Modal fine tune

Squashed branch, it adds:
* Some fine tunes on institution modal styles
* Fix public and private modal cnpj validations(with its tests)
* Remove unnecessary code

See merge request !95
src/noosfero-spb/gov_user/lib/gov_user_plugin.rb
@@ -148,7 +148,6 @@ class GovUserPlugin < Noosfero::Plugin @@ -148,7 +148,6 @@ class GovUserPlugin < Noosfero::Plugin
148 lib/noosfero-root.js 148 lib/noosfero-root.js
149 lib/select-element.js 149 lib/select-element.js
150 lib/select-field-choices.js 150 lib/select-field-choices.js
151 - lib/modal-observer.js  
152 views/complete-registration.js 151 views/complete-registration.js
153 views/control-panel.js 152 views/control-panel.js
154 views/create-institution.js 153 views/create-institution.js
src/noosfero-spb/gov_user/lib/institution.rb
@@ -6,6 +6,8 @@ class Institution < ActiveRecord::Base @@ -6,6 +6,8 @@ class Institution < ActiveRecord::Base
6 :display => %w[compact] 6 :display => %w[compact]
7 } 7 }
8 8
  9 + CNPJ_FORMAT = /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/
  10 +
9 def self.default_search_display 11 def self.default_search_display
10 'compact' 12 'compact'
11 end 13 end
@@ -23,7 +25,7 @@ class Institution < ActiveRecord::Base @@ -23,7 +25,7 @@ class Institution < ActiveRecord::Base
23 :corporate_name, :siorg_code, :community 25 :corporate_name, :siorg_code, :community
24 26
25 validates :name, :presence=>true, :uniqueness=>true 27 validates :name, :presence=>true, :uniqueness=>true
26 - validates :cnpj, :presence=>true, :uniqueness=>true 28 + validates :cnpj, :length=>{maximum: 18}
27 29
28 before_save :verify_institution_type 30 before_save :verify_institution_type
29 31
@@ -34,7 +36,7 @@ class Institution < ActiveRecord::Base @@ -34,7 +36,7 @@ class Institution < ActiveRecord::Base
34 } 36 }
35 37
36 validate :validate_country, :validate_state, :validate_city, 38 validate :validate_country, :validate_state, :validate_city,
37 - :verify_institution_type, :validate_format_cnpj 39 + :verify_institution_type
38 40
39 41
40 def has_accepted_rating? user_rating 42 def has_accepted_rating? user_rating
@@ -99,18 +101,4 @@ class Institution < ActiveRecord::Base @@ -99,18 +101,4 @@ class Institution < ActiveRecord::Base
99 101
100 return true 102 return true
101 end 103 end
102 -  
103 - def validate_format_cnpj  
104 - return true if self.community.blank? && self.community.country != "BR"  
105 - return true if self.cnpj.blank?  
106 -  
107 - format = /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/  
108 -  
109 - if !self.cnpj.blank? && format.match(self.cnpj)  
110 - return true  
111 - else  
112 - self.errors.add(:cnpj, _("invalid format"))  
113 - return false  
114 - end  
115 - end  
116 end 104 end
src/noosfero-spb/gov_user/lib/private_institution.rb
1 class PrivateInstitution < Institution 1 class PrivateInstitution < Institution
  2 + validates :cnpj,
  3 + :presence=>true,
  4 + :format => {with: CNPJ_FORMAT},
  5 + :if => :is_a_brazilian_institution?
  6 +
  7 + validates :cnpj,
  8 + :uniqueness=>true, :unless => 'cnpj.blank?'
  9 +
  10 + private
  11 + def is_a_brazilian_institution?
  12 + self.community.country == "BR"
  13 + end
2 end 14 end
src/noosfero-spb/gov_user/lib/public_institution.rb
@@ -5,9 +5,7 @@ class PublicInstitution &lt; Institution @@ -5,9 +5,7 @@ class PublicInstitution &lt; Institution
5 validates :acronym, :allow_blank => true, :allow_nil => true, 5 validates :acronym, :allow_blank => true, :allow_nil => true,
6 :uniqueness=>true 6 :uniqueness=>true
7 7
8 - validates_format_of(  
9 - :cnpj,  
10 - :with => /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/,  
11 - :allow_nil => true, :allow_blank => true  
12 - ) 8 + validates :cnpj,
  9 + :format => {with: CNPJ_FORMAT},
  10 + :unless => 'cnpj.blank?'
13 end 11 end
src/noosfero-spb/gov_user/po/pt/gov_user.po
@@ -172,7 +172,7 @@ msgstr &quot;Nome de Instituição já existe&quot; @@ -172,7 +172,7 @@ msgstr &quot;Nome de Instituição já existe&quot;
172 #: plugins/gov_user/views/gov_user_plugin_myprofile/edit_institution.html.erb:44 172 #: plugins/gov_user/views/gov_user_plugin_myprofile/edit_institution.html.erb:44
173 #: plugins/gov_user/views/gov_user_plugin/_institution.html.erb:62 173 #: plugins/gov_user/views/gov_user_plugin/_institution.html.erb:62
174 msgid "Corporate Name" 174 msgid "Corporate Name"
175 -msgstr "Razão social" 175 +msgstr "Razão Social"
176 176
177 #: plugins/gov_user/views/gov_user_plugin_myprofile/edit_institution.html.erb:49 177 #: plugins/gov_user/views/gov_user_plugin_myprofile/edit_institution.html.erb:49
178 #: plugins/gov_user/views/gov_user_plugin/_institution.html.erb:79 178 #: plugins/gov_user/views/gov_user_plugin/_institution.html.erb:79
src/noosfero-spb/gov_user/public/lib/modal-observer.js
@@ -1,36 +0,0 @@ @@ -1,36 +0,0 @@
1 -/* globals modulejs */  
2 -  
3 -// Works on: IE 11, Edge 12+, Firefox 40+, Chrome 43+, Opera 32+, Safari 32+  
4 -modulejs.define("ModalObserver", function() {  
5 - "use strict";  
6 -  
7 - function ModalObserver(target, callback) {  
8 - this.action_callback = callback;  
9 - this.observer = new MutationObserver(this.mutationVerifier.bind(this));  
10 -  
11 - this.observer.observe(target, {attributes: true});  
12 - }  
13 -  
14 -  
15 - ModalObserver.prototype.mutationVerifier = function(mutations) {  
16 - var callback = this.action_callback;  
17 - var observer = this.observer;  
18 -  
19 - mutations.forEach(function(mutation) {  
20 - if (mutation.attributeName === "style" &&  
21 - mutation.target.style.display === "none")  
22 - {  
23 - callback();  
24 - // stop the observer, once its action is done  
25 - observer.disconnect();  
26 - }  
27 - });  
28 - };  
29 -  
30 -  
31 - return {  
32 - init: function(target, callback) {  
33 - new ModalObserver(target, callback);  
34 - }  
35 - };  
36 -});  
src/noosfero-spb/gov_user/public/views/create-institution.js
@@ -21,6 +21,7 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;] @@ -21,6 +21,7 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
21 21
22 function show_public_institutions_fields() { 22 function show_public_institutions_fields() {
23 $(".public-institutions-fields").show(); 23 $(".public-institutions-fields").show();
  24 + $("#cnpj_required_field_mark").html("");
24 } 25 }
25 26
26 27
@@ -28,6 +29,7 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;] @@ -28,6 +29,7 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
28 $(".public-institutions-fields").hide(); 29 $(".public-institutions-fields").hide();
29 $("#institutions_governmental_power option").selected(0); 30 $("#institutions_governmental_power option").selected(0);
30 $("#institutions_governmental_sphere option").selected(0); 31 $("#institutions_governmental_sphere option").selected(0);
  32 + $("#cnpj_required_field_mark").html("(*)");
31 } 33 }
32 34
33 35
@@ -64,6 +66,17 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;] @@ -64,6 +66,17 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
64 } 66 }
65 67
66 68
  69 + // If the page has a user institution list, update it without repeating the institution
  70 + function update_user_institutions_list() {
  71 + $(".institution_container").append(get_clone_institution_data(institution_id));
  72 + add_selected_institution_to_list(institution_id, institution_name);
  73 +
  74 + $(".remove-institution").click(remove_institution);
  75 + //$('#institution_dialog').dialog('close');
  76 + $('#institution_modal').modal('toggle');
  77 + }
  78 +
  79 +
67 function success_ajax_response(response) { 80 function success_ajax_response(response) {
68 close_loading(); 81 close_loading();
69 82
@@ -75,24 +88,19 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;] @@ -75,24 +88,19 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
75 window.display_notice(response.message); 88 window.display_notice(response.message);
76 89
77 set_institution_field_name($("#community_name").val()); 90 set_institution_field_name($("#community_name").val());
78 - //settup_created_institution();  
79 91
80 // Close modal 92 // Close modal
81 - //$(".modal-header .close").trigger("click"); 93 + $(".modal-header .close").trigger("click");
82 94
83 // Clear modal fields 95 // Clear modal fields
84 $("#institution_modal_body").html(window.sessionStorage.getItem("InstitutionModalBody")); 96 $("#institution_modal_body").html(window.sessionStorage.getItem("InstitutionModalBody"));
  97 + $(".modal .modal-body div").show();
85 98
86 // Reset modal events 99 // Reset modal events
87 init_module(); 100 init_module();
88 101
89 -  
90 - // If the page has a user institution list, update it without repeating the institution  
91 - $(".institution_container").append(get_clone_institution_data(institution_id));  
92 - add_selected_institution_to_list(institution_id, institution_name);  
93 - $(".remove-institution").click(remove_institution);  
94 - //$('#institution_dialog').dialog('close');  
95 - $('#institution_modal').modal('toggle'); 102 + // If the user is is his profile edition page
  103 + update_user_institutions_list();
96 } else { 104 } else {
97 var errors = create_error_list(response); 105 var errors = create_error_list(response);
98 $("#create_institution_errors").switchClass("hide-field", "show-field").html("<h2>"+response.message+"</h2>"+errors); 106 $("#create_institution_errors").switchClass("hide-field", "show-field").html("<h2>"+response.message+"</h2>"+errors);
@@ -271,9 +279,7 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;] @@ -271,9 +279,7 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
271 279
272 280
273 function add_mask_to_form_items() { 281 function add_mask_to_form_items() {
274 - if ($.mask) {  
275 - $("#institutions_cnpj").mask("99.999.999/9999-99");  
276 - } 282 + $("#institutions_cnpj").mask("99.999.999/9999-99");
277 } 283 }
278 284
279 285
src/noosfero-spb/gov_user/public/views/institution-modal.js
1 /* globals modulejs */ 1 /* globals modulejs */
2 2
3 modulejs.define('InstitutionModal', 3 modulejs.define('InstitutionModal',
4 - ['jquery', 'NoosferoRoot', 'CreateInstitution', 'ModalObserver'],  
5 - function($, NoosferoRoot, CreateInstitution, ModalObserver) 4 + ['jquery', 'NoosferoRoot', 'CreateInstitution'],
  5 + function($, NoosferoRoot, CreateInstitution)
6 { 6 {
7 'use strict'; 7 'use strict';
8 8
@@ -11,32 +11,15 @@ modulejs.define(&#39;InstitutionModal&#39;, @@ -11,32 +11,15 @@ modulejs.define(&#39;InstitutionModal&#39;,
11 NoosferoRoot.urlWithSubDirectory("/plugin/gov_user/create_institution"), 11 NoosferoRoot.urlWithSubDirectory("/plugin/gov_user/create_institution"),
12 }; 12 };
13 13
14 - // When the modal is closed, put the community name into the autocomplete  
15 - function observer_action() {  
16 - var community_name = $("#community_name").val();  
17 - $("#input_institution").attr("value", community_name).autocomplete("search");  
18 -  
19 - // Clear error messages  
20 - $("#create_institution_errors").html("");  
21 - }  
22 -  
23 - // Observe when modal is closed  
24 - function observe_modal() {  
25 - var institution_modal = document.querySelector("#institution_modal");  
26 - ModalObserver.init(institution_modal, observer_action);  
27 - }  
28 -  
29 - 14 + // Get the modal html code and prepare it
30 function prepare_institution_modal() { 15 function prepare_institution_modal() {
31 - $.get(AJAX_URL.create_institution_modal, function(response){ 16 + $.get(AJAX_URL.create_institution_modal, function(response) {
32 window.sessionStorage.setItem("InstitutionModalBody", response); 17 window.sessionStorage.setItem("InstitutionModalBody", response);
33 $("#institution_modal_body").html(response); 18 $("#institution_modal_body").html(response);
34 19
35 // Set all events on modal 20 // Set all events on modal
36 CreateInstitution.init(); 21 CreateInstitution.init();
37 }); 22 });
38 -  
39 - $("#create_institution_link").click(observe_modal);  
40 } 23 }
41 24
42 25
src/noosfero-spb/gov_user/test/unit/private_institution_test.rb
@@ -19,10 +19,13 @@ class PrivateInstitutionTest &lt; ActiveSupport::TestCase @@ -19,10 +19,13 @@ class PrivateInstitutionTest &lt; ActiveSupport::TestCase
19 Institution.destroy_all 19 Institution.destroy_all
20 end 20 end
21 21
22 - should "save without a cnpj" do 22 + should "not save without a cnpj" do
23 @institution.cnpj = nil 23 @institution.cnpj = nil
24 24
25 - assert @institution.save 25 + assert_equal false, @institution.save
  26 +
  27 + @institution.cnpj = "11.111.111/1111-11"
  28 + assert_equal true, @institution.save
26 end 29 end
27 30
28 should "save without fantasy name" do 31 should "save without fantasy name" do
@@ -31,4 +34,10 @@ class PrivateInstitutionTest &lt; ActiveSupport::TestCase @@ -31,4 +34,10 @@ class PrivateInstitutionTest &lt; ActiveSupport::TestCase
31 34
32 assert @institution.save 35 assert @institution.save
33 end 36 end
  37 +
  38 + should "not verify cnpj if it isnt a brazilian institution" do
  39 + @institution.cnpj = nil
  40 + @institution.community.country = "AR"
  41 + assert_equal true, @institution.save
  42 + end
34 end 43 end
src/noosfero-spb/gov_user/test/unit/public_institution_test.rb
@@ -65,4 +65,15 @@ class PublicInstitutionTest &lt; ActiveSupport::TestCase @@ -65,4 +65,15 @@ class PublicInstitutionTest &lt; ActiveSupport::TestCase
65 assert !@institution.save 65 assert !@institution.save
66 assert @institution.errors.full_messages.include? invalid_msg 66 assert @institution.errors.full_messages.include? invalid_msg
67 end 67 end
  68 +
  69 +
  70 + should "verify cnpj format if it is filled" do
  71 + @institution.cnpj = "123456789"
  72 +
  73 + assert_equal false, @institution.save
  74 +
  75 + @institution.cnpj = "11.222.333/4444-55"
  76 +
  77 + assert_equal true, @institution.save
  78 + end
68 end 79 end
src/noosfero-spb/gov_user/views/gov_user_plugin/_institution.html.erb
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 <div class="spb-row no-margin-top"> 2 <div class="spb-row no-margin-top">
3 <% if environment.enabled?('admin_must_approve_new_communities') %> 3 <% if environment.enabled?('admin_must_approve_new_communities') %>
4 <div class='spb-col spb-col-12 explanation'> 4 <div class='spb-col spb-col-12 explanation'>
5 - <%= _("Note that the creation of communities in this environment is restricted. Your request to create this new community will be sent to %{environment} administrators and will be approved or rejected according to their methods and criteria.") % { :environment => environment.name }%> 5 + <%= _("Note that the creation of institutions in this environment is restricted. Your request to create this new community will be sent to %{environment} administrators and will be approved or rejected according to their methods and criteria.") % { :environment => environment.name }%>
6 </div> 6 </div>
7 <% end %> 7 <% end %>
8 </div> 8 </div>
@@ -107,7 +107,7 @@ @@ -107,7 +107,7 @@
107 <div class="spb-col spb-col-12"> 107 <div class="spb-col spb-col-12">
108 <label class="formlabel" for="institutions_cnpj"> 108 <label class="formlabel" for="institutions_cnpj">
109 <%= _("CNPJ") %> 109 <%= _("CNPJ") %>
110 - <span class="required-field">(*)</span> 110 + <span id="cnpj_required_field_mark" class="required-field"></span>
111 </label> 111 </label>
112 <%= inst.text_field(:cnpj, :placeholder=>"99.999.999/9999-99", :class=>"intitution_cnpj_field", :value => params[:institutions][:cnpj]) %> 112 <%= inst.text_field(:cnpj, :placeholder=>"99.999.999/9999-99", :class=>"intitution_cnpj_field", :value => params[:institutions][:cnpj]) %>
113 </div> 113 </div>
@@ -154,7 +154,7 @@ @@ -154,7 +154,7 @@
154 <%= inst.select(:juridical_nature, @juridical_nature, :selected=>params[:institutions][:juridical_nature], :class => flash[:error_institution_juridical_nature])%> 154 <%= inst.select(:juridical_nature, @juridical_nature, :selected=>params[:institutions][:juridical_nature], :class => flash[:error_institution_juridical_nature])%>
155 </div> 155 </div>
156 </div> 156 </div>
157 - 157 +
158 <div class="spb-row public-institutions-fields"> 158 <div class="spb-row public-institutions-fields">
159 <div class="spb-col spb-col-12"> 159 <div class="spb-col spb-col-12">
160 <%= _("SISP ?") %> 160 <%= _("SISP ?") %>
src/noosfero-spb/noosfero-spb-theme/css/modal.css
@@ -161,11 +161,18 @@ @@ -161,11 +161,18 @@
161 .modal .explanation { 161 .modal .explanation {
162 color: #3F60C6; 162 color: #3F60C6;
163 font-size: 12px; 163 font-size: 12px;
  164 + font-style: normal;
  165 + font-family: 'open_sansitalic';
164 } 166 }
165 167
166 .modal .required-field, .modal .errorExplanation { 168 .modal .required-field, .modal .errorExplanation {
167 color: #EA1C00; 169 color: #EA1C00;
168 font-size:12px; 170 font-size:12px;
  171 + margin-bottom: 20px;
  172 +}
  173 +
  174 +.modal .required-field, .modal .errorExplanation:empty {
  175 + margin-bottom: 0px;
169 } 176 }
170 177
171 .modal .spb-row { 178 .modal .spb-row {