Commit d24e09beae33a65c5d8ad16b3eba1b60d338b0c8

Authored by Gabriela Navarro
1 parent 70d4c608

Show or hide some institution fields depending on user country

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
controllers/mpog_software_plugin_controller.rb
... ... @@ -198,11 +198,6 @@ class MpogSoftwarePluginController &lt; ApplicationController
198 198 end
199 199 end
200 200  
201   - if institution.cnpj.nil? or institution.cnpj.blank?
202   - institution.errors.add(:cnpj, _("can't be blank"))
203   - return institution
204   - end
205   -
206 201 InstitutionHelper.register_institution_modification institution
207 202  
208 203 institution
... ...
lib/institution.rb
... ... @@ -22,7 +22,8 @@ class Institution &lt; ActiveRecord::Base
22 22 where("name ilike ? OR acronym ilike ?", "%#{value}%", "%#{value}%" )
23 23 }
24 24  
25   - validate :validate_country, :validate_state, :validate_city, :verify_institution_type
  25 + validate :validate_country, :validate_state, :validate_city, :verify_institution_type, :validate_cnpj, :validate_format_cnpj
  26 +
26 27  
27 28 protected
28 29  
... ... @@ -47,17 +48,51 @@ class Institution &lt; ActiveRecord::Base
47 48  
48 49 def validate_state
49 50 if (self.community.blank?) || self.errors[:state].blank? && self.community.state.blank?
50   - self.errors.add(:state, _("can't be blank"))
51   - return false
  51 + if self.community.country == "BR"
  52 + self.errors.add(:state, _("can't be blank"))
  53 + return false
  54 + else
  55 + return true
  56 + end
52 57 end
53 58 return true
54 59 end
55 60  
56 61 def validate_city
57 62 if (self.community.blank?) || self.errors[:city].blank? && self.community.city.blank?
58   - self.errors.add(:city, _("can't be blank"))
59   - return false
  63 + if self.community.country == "BR"
  64 + self.errors.add(:city, _("can't be blank"))
  65 + return false
  66 + else
  67 + return true
  68 + end
60 69 end
61 70 return true
62 71 end
  72 +
  73 + def validate_format_cnpj
  74 + return true if !self.community.blank? && self.community.country != "BR"
  75 +
  76 + format = /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/
  77 +
  78 + if !self.cnpj.blank? && format.match(self.cnpj)
  79 + return true
  80 + else
  81 + self.errors.add(:cnpj, _("invalid format"))
  82 + return false
  83 + end
  84 + end
  85 +
  86 + def validate_cnpj
  87 + if !self.community.blank? && self.community.country == "BR"
  88 + if self.errors[:cnpj].blank? && self.cnpj.blank?
  89 + self.errors.add(:cnpj, _("can't be blank"))
  90 + return false
  91 + else
  92 + return true
  93 + end
  94 + else
  95 + return true
  96 + end
  97 + end
63 98 end
... ...
lib/private_institution.rb
1 1 class PrivateInstitution < Institution
2   - validates :cnpj, :presence=>true, :uniqueness=>true
3   - validates_format_of :cnpj, :with => /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/
4   -end
5 2 \ No newline at end of file
  3 + validates :cnpj, :uniqueness=>true, :allow_nil=>true, :allow_blank=>true
  4 +end
... ...
public/mpog-institution-validations.js
... ... @@ -21,12 +21,10 @@
21 21  
22 22 function show_public_institutions_fields() {
23 23 jQuery(".public-institutions-fields").show();
24   - jQuery("label[for='institutions_acronym']").html(jQuery("#acronym_translate").val());
25 24 }
26 25  
27 26 function show_private_institutions_fields() {
28 27 jQuery(".public-institutions-fields").hide();
29   - jQuery("label[for='institutions_acronym']").html(jQuery("#fantasy_name_translate").val());
30 28  
31 29 jQuery("#institutions_governmental_power option").selected(0);
32 30 jQuery("#institutions_governmental_sphere option").selected(0);
... ... @@ -183,7 +181,25 @@
183 181 jQuery(".intitution_cnpj_field").mask("99.999.999/9999-99");
184 182 }
185 183  
  184 + function show_hide_cnpj_city() {
  185 + var cnpj = jQuery("#institutions_cnpj").parent().parent();
  186 + var city = jQuery("#community_city").parent().parent();
  187 + var state = jQuery("#community_state").parent().parent();
  188 +
  189 + if( this.value != "BR" ) {
  190 + cnpj.hide();
  191 + city.hide();
  192 + state.hide();
  193 + } else {
  194 + cnpj.show();
  195 + city.show();
  196 + state.show();
  197 + }
  198 + }
  199 +
186 200 function set_events() {
  201 + show_private_institutions_fields();
  202 +
187 203 jQuery('#community_country').val("BR");
188 204  
189 205 jQuery("#create_institution_link").click(open_create_institution_modal);
... ... @@ -203,6 +219,8 @@
203 219  
204 220 jQuery(".remove-institution").click(remove_institution);
205 221  
  222 + jQuery("#community_country").change(show_hide_cnpj_city);
  223 +
206 224 add_mask_to_form_items();
207 225  
208 226 institution_autocomplete();
... ...
test/functional/mpog_software_plugin_controller_test.rb
... ... @@ -148,6 +148,29 @@ class MpogSoftwarePluginControllerTest &lt; ActionController::TestCase
148 148 assert_redirected_to(controller: "mpog_software_plugin", action: "create_institution_admin")
149 149 end
150 150  
  151 + should "Create foreign institution without city, state and cnpj by post" do
  152 + @controller.stubs(:verify_recaptcha).returns(true)
  153 +
  154 + fields = generate_form_fields "Foreign institution", "AZ", "", "", "", "PrivateInstitution"
  155 + fields[:institutions][:acronym] = "FI"
  156 +
  157 + post :new_institution, fields
  158 +
  159 + assert_redirected_to(controller: "admin_panel", action: "index")
  160 + end
  161 +
  162 + should "Create foreign institution without city, state and cnpj by ajax" do
  163 + @controller.stubs(:verify_recaptcha).returns(true)
  164 +
  165 + fields = generate_form_fields "Foreign institution", "AZ", "", "", "", "PrivateInstitution"
  166 + fields[:institutions][:acronym] = "FI"
  167 +
  168 + xhr :post, :new_institution, fields
  169 +
  170 + json_response = ActiveSupport::JSON.decode(@response.body)
  171 + assert json_response["success"]
  172 + end
  173 +
151 174 private
152 175  
153 176 def generate_form_fields name, country, state, city, cnpj, type
... ...
views/mpog_software_plugin/_institution.html.erb
... ... @@ -30,12 +30,12 @@
30 30 <span class=''>
31 31 <div class='formfield type-radio'>
32 32 <label> <%= _("Public Institution") %>
33   - <%= radio_button_tag("institutions[type]", "PublicInstitution", true) %>
  33 + <%= radio_button_tag("institutions[type]", "PublicInstitution") %>
34 34 </label>
35 35  
36 36 <label>
37 37 <%= _("Private Institution") %>
38   - <%= radio_button_tag("institutions[type]" ,"PrivateInstitution")%>
  38 + <%= radio_button_tag("institutions[type]" ,"PrivateInstitution", true)%>
39 39 </label>
40 40 </div>
41 41 </span>
... ...