Commit f1cad301ca13c8876034b68a865ff05a48f1f04e

Authored by Fabio Teixeira
1 parent 147297e0

Fix institutions cnpj validations and do some fine tunes on modal style

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
src/noosfero-spb/gov_user/lib/gov_user_plugin.rb
... ... @@ -148,7 +148,6 @@ class GovUserPlugin &lt; Noosfero::Plugin
148 148 lib/noosfero-root.js
149 149 lib/select-element.js
150 150 lib/select-field-choices.js
151   - lib/modal-observer.js
152 151 views/complete-registration.js
153 152 views/control-panel.js
154 153 views/create-institution.js
... ...
src/noosfero-spb/gov_user/lib/institution.rb
... ... @@ -6,6 +6,8 @@ class Institution &lt; ActiveRecord::Base
6 6 :display => %w[compact]
7 7 }
8 8  
  9 + CNPJ_FORMAT = /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/
  10 +
9 11 def self.default_search_display
10 12 'compact'
11 13 end
... ... @@ -23,7 +25,7 @@ class Institution &lt; ActiveRecord::Base
23 25 :corporate_name, :siorg_code, :community
24 26  
25 27 validates :name, :presence=>true, :uniqueness=>true
26   - validates :cnpj, :presence=>true, :uniqueness=>true
  28 + validates :cnpj, :length=>{maximum: 18}
27 29  
28 30 before_save :verify_institution_type
29 31  
... ... @@ -34,7 +36,7 @@ class Institution &lt; ActiveRecord::Base
34 36 }
35 37  
36 38 validate :validate_country, :validate_state, :validate_city,
37   - :verify_institution_type, :validate_format_cnpj
  39 + :verify_institution_type
38 40  
39 41  
40 42 def has_accepted_rating? user_rating
... ... @@ -99,18 +101,4 @@ class Institution &lt; ActiveRecord::Base
99 101  
100 102 return true
101 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 104 end
... ...
src/noosfero-spb/gov_user/lib/private_institution.rb
1 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 14 end
... ...
src/noosfero-spb/gov_user/lib/public_institution.rb
... ... @@ -5,9 +5,7 @@ class PublicInstitution &lt; Institution
5 5 validates :acronym, :allow_blank => true, :allow_nil => true,
6 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 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 172 #: plugins/gov_user/views/gov_user_plugin_myprofile/edit_institution.html.erb:44
173 173 #: plugins/gov_user/views/gov_user_plugin/_institution.html.erb:62
174 174 msgid "Corporate Name"
175   -msgstr "Razão social"
  175 +msgstr "Razão Social"
176 176  
177 177 #: plugins/gov_user/views/gov_user_plugin_myprofile/edit_institution.html.erb:49
178 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   -/* 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 21  
22 22 function show_public_institutions_fields() {
23 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 29 $(".public-institutions-fields").hide();
29 30 $("#institutions_governmental_power option").selected(0);
30 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 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 80 function success_ajax_response(response) {
68 81 close_loading();
69 82  
... ... @@ -75,24 +88,19 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
75 88 window.display_notice(response.message);
76 89  
77 90 set_institution_field_name($("#community_name").val());
78   - //settup_created_institution();
79 91  
80 92 // Close modal
81   - //$(".modal-header .close").trigger("click");
  93 + $(".modal-header .close").trigger("click");
82 94  
83 95 // Clear modal fields
84 96 $("#institution_modal_body").html(window.sessionStorage.getItem("InstitutionModalBody"));
  97 + $(".modal .modal-body div").show();
85 98  
86 99 // Reset modal events
87 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 104 } else {
97 105 var errors = create_error_list(response);
98 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 279  
272 280  
273 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 1 /* globals modulejs */
2 2  
3 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 7 'use strict';
8 8  
... ... @@ -11,32 +11,15 @@ modulejs.define(&#39;InstitutionModal&#39;,
11 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 15 function prepare_institution_modal() {
31   - $.get(AJAX_URL.create_institution_modal, function(response){
  16 + $.get(AJAX_URL.create_institution_modal, function(response) {
32 17 window.sessionStorage.setItem("InstitutionModalBody", response);
33 18 $("#institution_modal_body").html(response);
34 19  
35 20 // Set all events on modal
36 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 19 Institution.destroy_all
20 20 end
21 21  
22   - should "save without a cnpj" do
  22 + should "not save without a cnpj" do
23 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 29 end
27 30  
28 31 should "save without fantasy name" do
... ... @@ -31,4 +34,10 @@ class PrivateInstitutionTest &lt; ActiveSupport::TestCase
31 34  
32 35 assert @institution.save
33 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 43 end
... ...
src/noosfero-spb/gov_user/test/unit/public_institution_test.rb
... ... @@ -65,4 +65,15 @@ class PublicInstitutionTest &lt; ActiveSupport::TestCase
65 65 assert !@institution.save
66 66 assert @institution.errors.full_messages.include? invalid_msg
67 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 79 end
... ...
src/noosfero-spb/gov_user/views/gov_user_plugin/_institution.html.erb
... ... @@ -2,7 +2,7 @@
2 2 <div class="spb-row no-margin-top">
3 3 <% if environment.enabled?('admin_must_approve_new_communities') %>
4 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 6 </div>
7 7 <% end %>
8 8 </div>
... ... @@ -107,7 +107,7 @@
107 107 <div class="spb-col spb-col-12">
108 108 <label class="formlabel" for="institutions_cnpj">
109 109 <%= _("CNPJ") %>
110   - <span class="required-field">(*)</span>
  110 + <span id="cnpj_required_field_mark" class="required-field"></span>
111 111 </label>
112 112 <%= inst.text_field(:cnpj, :placeholder=>"99.999.999/9999-99", :class=>"intitution_cnpj_field", :value => params[:institutions][:cnpj]) %>
113 113 </div>
... ... @@ -154,7 +154,7 @@
154 154 <%= inst.select(:juridical_nature, @juridical_nature, :selected=>params[:institutions][:juridical_nature], :class => flash[:error_institution_juridical_nature])%>
155 155 </div>
156 156 </div>
157   -
  157 +
158 158 <div class="spb-row public-institutions-fields">
159 159 <div class="spb-col spb-col-12">
160 160 <%= _("SISP ?") %>
... ...
src/noosfero-spb/noosfero-spb-theme/css/modal.css
... ... @@ -161,11 +161,18 @@
161 161 .modal .explanation {
162 162 color: #3F60C6;
163 163 font-size: 12px;
  164 + font-style: normal;
  165 + font-family: 'open_sansitalic';
164 166 }
165 167  
166 168 .modal .required-field, .modal .errorExplanation {
167 169 color: #EA1C00;
168 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 178 .modal .spb-row {
... ...