Commit e357d9c7efdba78fdf7dfb37753346fdd65c986d

Authored by Gabriela Navarro
Committed by Fabio Teixeira
1 parent f6b472cd

Fix highlight error for fields on modal and admin view and add autocomplete for city

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
controllers/gov_user_plugin_controller.rb
... ... @@ -15,6 +15,9 @@ class GovUserPluginController &lt; ApplicationController
15 15 def create_institution
16 16 @show_sisp_field = environment.admins.include?(current_user.person)
17 17 @state_list = get_state_list()
  18 + @governmental_sphere = [[_("Select a Governmental Sphere"), 0]]|GovernmentalSphere.all.map {|s| [s.name, s.id]}
  19 + @governmental_power = [[_("Select a Governmental Power"), 0]]|GovernmentalPower.all.map {|g| [g.name, g.id]}
  20 + @juridical_nature = [[_("Select a Juridical Nature"), 0]]|JuridicalNature.all.map {|j| [j.name, j.id]}
18 21  
19 22 params[:community] ||= {}
20 23 params[:institutions] ||= {}
... ... @@ -40,6 +43,9 @@ class GovUserPluginController &lt; ApplicationController
40 43 def create_institution_admin
41 44 @show_sisp_field = environment.admins.include?(current_user.person)
42 45 @state_list = get_state_list()
  46 + @governmental_sphere = [[_("Select a Governmental Sphere"), 0]]|GovernmentalSphere.all.map {|s| [s.name, s.id]}
  47 + @governmental_power = [[_("Select a Governmental Power"), 0]]|GovernmentalPower.all.map {|g| [g.name, g.id]}
  48 + @juridical_nature = [[_("Select a Juridical Nature"), 0]]|JuridicalNature.all.map {|j| [j.name, j.id]}
43 49  
44 50 @url_token = split_http_referer request.original_url()
45 51  
... ... @@ -205,9 +211,10 @@ class GovUserPluginController &lt; ApplicationController
205 211 end
206 212  
207 213 def save_institution institution
  214 + inst_errors = institution.errors.messages
  215 + com_errors = institution.community.errors.messages
208 216  
209   - inst_errors = institution.errors.full_messages
210   - com_errors = institution.community.errors.full_messages
  217 + set_errors institution
211 218  
212 219 set_error_css institution
213 220  
... ... @@ -219,7 +226,7 @@ class GovUserPluginController &lt; ApplicationController
219 226 else
220 227 { :success => false,
221 228 :message => _("Institution could not be created!"),
222   - :errors => inst_errors << com_errors
  229 + :errors => inst_errors.merge(com_errors)
223 230 }
224 231 end
225 232 end
... ... @@ -229,23 +236,24 @@ class GovUserPluginController &lt; ApplicationController
229 236 redirect_to :controller => "/admin_panel", :action => "index"
230 237 else
231 238 flash[:errors] = response_message[:errors]
232   - redirect_to :controller => "gov_user_plugin", :action => "create_institution_admin", :params => params
  239 +
  240 + redirect_to :controller => "gov_user_plugin", :action => "create_institution_admin"
233 241 end
234 242 end
235 243  
236   - def set_error_css institution
237   - institution.valid?
238   - institution.community.valid?
239   - params[:error_community_name] = institution.community.errors.include?(:name) ? "highlight-error" : ""
240   - params[:error_community_country] = institution.community.errors.include?(:country) ? "highlight-error" : ""
241   - params[:error_community_city] = institution.errors.include?(:city) ? "highlight-error" : ""
242   - params[:error_community_state] = institution.errors.include?(:state) ? "highlight-error" : ""
243   - params[:error_institution_corporate_name] = institution.errors.include?(:corporate_name) ? "highlight-error" : ""
244   - params[:error_institution_acronym] = institution.errors.include?(:acronym) ? "highlight-error" : ""
245   - params[:error_institution_cnpj] = institution.errors.include?(:cnpj) ? "highlight-error" : ""
246   - params[:error_institution_governmental_sphere] = institution.errors.include?(:governmental_sphere) ? "highlight-error" : ""
247   - params[:error_institution_governmental_power] = institution.errors.include?(:governmental_power) ? "highlight-error" : ""
248   - params[:error_institution_juridical_nature] = institution.errors.include?(:juridical_nature) ? "highlight-error" : ""
249   - params[:error_institution_sisp] = institution.errors.include?(:sisp) ? "highlight-error" : ""
  244 + def set_errors institution
  245 + institution.valid? if institution
  246 + institution.community.valid? if institution.community
  247 +
  248 + flash[:error_community_name] = institution.community.errors.include?(:name) ? "highlight-error" : ""
  249 + flash[:error_community_country] = institution.errors.include?(:country) ? "highlight-error" : ""
  250 + flash[:error_community_state] = institution.errors.include?(:state) ? "highlight-error" : ""
  251 + flash[:error_community_city] = institution.errors.include?(:city) ? "highlight-error" : ""
  252 + flash[:error_institution_corporate_name] = institution.errors.include?(:corporate_name) ? "highlight-error" : ""
  253 + flash[:error_institution_cnpj] = institution.errors.include?(:cnpj) ? "highlight-error" : ""
  254 + flash[:error_institution_governmental_sphere] = institution.errors.include?(:governmental_sphere) ? "highlight-error" : ""
  255 + flash[:error_institution_governmental_power] = institution.errors.include?(:governmental_power) ? "highlight-error" : ""
  256 + flash[:error_institution_juridical_nature] = institution.errors.include?(:juridical_nature) ? "highlight-error" : ""
250 257 end
  258 +
251 259 end
... ...
lib/gov_user_plugin.rb
... ... @@ -124,6 +124,23 @@ class GovUserPlugin &lt; Noosfero::Plugin
124 124 percentege
125 125 end
126 126  
  127 + def stylesheet?
  128 + true
  129 + end
  130 +
  131 + def admin_panel_links
  132 + [
  133 + {
  134 + :title => _('Create Institution'),
  135 + :url => {
  136 + :controller => 'gov_user_plugin',
  137 + :action => 'create_institution_admin'
  138 + }
  139 + }
  140 + ]
  141 + end
  142 +
  143 +
127 144 def js_files
128 145 %w(
129 146 vendor/modulejs-1.5.0.min.js
... ...
public/views/create-institution.js
... ... @@ -9,7 +9,9 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
9 9 institution_already_exists:
10 10 NoosferoRoot.urlWithSubDirectory("/plugin/gov_user/institution_already_exists"),
11 11 get_institutions:
12   - NoosferoRoot.urlWithSubDirectory("/plugin/gov_user/get_institutions")
  12 + NoosferoRoot.urlWithSubDirectory("/plugin/gov_user/get_institutions"),
  13 + auto_complete_city:
  14 + NoosferoRoot.urlWithSubDirectory("/account/search_cities")
13 15 };
14 16  
15 17  
... ... @@ -96,19 +98,49 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
96 98  
97 99 $(".remove-institution").click(remove_institution);
98 100 } else {
99   - var errors = "<ul>";
  101 + var errors = create_error_list(response);
  102 + $("#create_institution_errors").switchClass("hide-field", "show-field").html("<h2>"+response.message+"</h2>"+errors);
100 103  
101   - for(var i = 0; i < response.errors.length; i++) {
102   - errors += "<li>"+response.errors[i]+"</li>";
103   - }
104   - errors += "</ul>";
105   - if($('#institution_dialog') == 0){
106   - $('#create_institution_errors').switchClass("show-field", "hide-field");
107   - $('#errorExplanation').html("<h2>"+response.message+"</h2>"+errors);
108   - }else{
109   - $("#create_institution_errors").switchClass("hide-field", "show-field").html("<h2>"+response.message+"</h2>"+errors);
  104 + show_errors_in_each_field(response.errors);
  105 + }
  106 + }
  107 +
  108 + function create_error_list(response){
  109 + var errors = "<ul>";
  110 + var field_name;
  111 +
  112 + for(var error_key in response.errors) {
  113 + field_name = adjust_error_key(error_key);
  114 +
  115 + if(response.errors[error_key].length > 0){
  116 + errors += "<li><b>"+field_name+"</b>: "+response.errors[error_key]+"</li>";
110 117 }
111 118 }
  119 +
  120 + errors += "</ul>";
  121 + return errors;
  122 + }
  123 +
  124 +
  125 + function show_errors_in_each_field(errors) {
  126 + var error_keys = Object.keys(errors);
  127 +
  128 + // (field)|(field)|...
  129 + var verify_error = new RegExp("(" + error_keys.join(")|(") + ")" );
  130 +
  131 + var fields_with_errors = $("#institution_dialog .formfield input").filter(function(index, field) {
  132 + return verify_error.test(field.getAttribute("name"));
  133 + });
  134 +
  135 + fields_with_errors.addClass("highlight-error");
  136 + }
  137 +
  138 +
  139 + function adjust_error_key(error_key) {
  140 + var text = error_key.replace(/_/, " ");
  141 + text = text.charAt(0).toUpperCase() + text.slice(1);
  142 +
  143 + return text;
112 144 }
113 145  
114 146  
... ... @@ -306,6 +338,41 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
306 338 }
307 339 }
308 340  
  341 + function autoCompleteCity() {
  342 + var country_selected = $('#community_country').val()
  343 +
  344 + if(country_selected == "BR")
  345 + {
  346 + $('#community_city').autocomplete({
  347 + source : function(request, response){
  348 + $.ajax({
  349 + type: "GET",
  350 + url: AJAX_URL.auto_complete_city,
  351 + data: {city_name: request.term, state_name: $("#community_state").val()},
  352 + success: function(result){
  353 + response(result);
  354 +
  355 + // There are two autocompletes in this page, the last one is modal
  356 + // autocomplete just put it above the modal
  357 + $(".ui-autocomplete").last().css("z-index", 1000);
  358 + },
  359 + error: function(ajax, stat, errorThrown) {
  360 + console.log('Link not found : ' + errorThrown);
  361 + }
  362 + });
  363 + },
  364 +
  365 + minLength: 3
  366 + });
  367 + }
  368 + else
  369 + {
  370 + if ($('#community_city').data('autocomplete')) {
  371 + $('#community_city').autocomplete("destroy");
  372 + $('#community_city').removeData('autocomplete');
  373 + }
  374 + }
  375 + }
309 376  
310 377 function set_events() {
311 378 $("#create_institution_link").click(open_create_institution_modal);
... ... @@ -330,6 +397,11 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
330 397 add_mask_to_form_items();
331 398  
332 399 institution_autocomplete();
  400 +
  401 + autoCompleteCity();
  402 + $('#community_country').change(function(){
  403 + autoCompleteCity();
  404 + })
333 405 }
334 406  
335 407  
... ...
views/gov_user_plugin/_institution.html.erb
... ... @@ -10,8 +10,11 @@
10 10 <div class="errorExplanation" id="errorExplanation">
11 11 <h2> <%= _("Can`t create new Institution: #{flash[:errors].length} errors") %> </h2>
12 12 <ul>
13   - <% flash[:errors].each do |error| %>
14   - <li> <%= error %> </li>
  13 + <% flash[:errors].each do |key, value| %>
  14 + <% key_name = key.to_s.gsub("_", " ") %>
  15 + <% if value.length > 0 %>
  16 + <li> <%= _("<b>#{key_name.capitalize}</b> #{value.join()}") %> </li>
  17 + <% end %>
15 18 <% end %>
16 19 </ul>
17 20 </div>
... ... @@ -20,6 +23,10 @@
20 23 <div id = 'create_institution_errors' class='errorExplanation hide-field'></div>
21 24  
22 25 <div>
  26 + <div class="fields-required">
  27 + <span class="errorExplanation"><%= required_fields_message %></span>
  28 + </div>
  29 + <br/>
23 30 <%= labelled_form_for :community, :url => {:action=>"new_institution"}, :html => { :multipart => true, :id=>"institution_form" } do |f| %>
24 31 <div class="fields-required">
25 32 <span class="errorExplanation"><%= required_fields_message %></span>
... ... @@ -39,7 +46,8 @@
39 46 </label>
40 47 </div>
41 48 </span>
42   - <%= required f.text_field(:name, :value => params[:community][:name], :size => 55, :class => "#{params[:error_community_name]}" ) %>
  49 +
  50 + <%= required f.text_field(:name, :class => flash[:error_community_name]) %>
43 51 <%= content_tag :span, _("Institution name already exists"), :id=>"already_exists_text", :class=>"errorExplanation hide-field" %>
44 52  
45 53 <div class= <%= @error_corporate_name %> >
... ... @@ -53,49 +61,56 @@
53 61  
54 62 <span class='optional-field'>
55 63 <div class="formfield type-text">
56   - <%= hidden_field_tag "acronym_translate", _("Acronym") %>
57   - <%= hidden_field_tag "fantasy_name_translate", _("Fantasy name") %>
58   - <%= inst.label("acronym" ,_("Acronym"), :class=>"formlabel") %>
59   - <%= inst.text_field(:acronym, :value => params[:institutions][:acronym], :class => "#{params[:error_institution_acronym]}") %>
  64 + <%= inst.label "corporate_name", _("Corporate Name"), :class=>"formlabel" %>
  65 + <%= required inst.text_field(:corporate_name, :class => flash[:error_institution_corporate_name]) %>
60 66 </div>
61 67 </span>
62 68  
63   - <%= required select_country(_('Country'), 'community', 'country', {:class => "type-select #{params[:error_community_country]}", :id => "community_country"}) %>
  69 + <%= required select_country(_('Country'), 'community', 'country', {:class => "type-select #{flash[:error_community_country]}", :id => "community_country"}) %>
64 70  
65 71 <span class='required-field'>
66 72 <div class="formfield">
67 73 <label for="community_state" class="formlabel"><%= _("State") %></label>
68   - <%= f.select(:state, @state_options, :selected => params[:community][:state], :class => "#{params[:error_community_state]}") %>
  74 + <%= f.select(:state, @state_list.collect {|state| [state.name, state.name]}, {}, {:class => flash[:error_community_state]}) %>
69 75 </div>
70 76 </span>
71 77  
72   - <%= required f.text_field(:city, :value => params[:community][:city], :id => "city_field", :class => "#{params[:error_community_city]}") %>
  78 + <%= required f.text_field(:city, :class => flash[:error_community_city]) %>
73 79  
74 80  
75 81 <span class='required-field'>
76 82 <div class="formfield type-text">
77 83 <%= inst.label("cnpj" ,_("CNPJ"), :class=>"formlabel") %>
78   - <%= required inst.text_field(:cnpj, :value => params[:institutions][:cnpj], :placeholder=>"99.999.999/9999-99", :class=>"intitution_cnpj_field #{params[:error_institution_cnpj]}") %>
  84 + <%= required inst.text_field(:cnpj, :placeholder=>"99.999.999/9999-99", :class=>"intitution_cnpj_field #{flash[:error_institution_cnpj]}") %>
  85 + </div>
  86 + </span>
  87 +
  88 + <span class='optional-field'>
  89 + <div class="formfield type-text">
  90 + <%= hidden_field_tag "acronym_translate", _("Acronym") %>
  91 + <%= hidden_field_tag "fantasy_name_translate", _("Fantasy name") %>
  92 + <%= inst.label("acronym" ,_("Acronym"), :class=>"formlabel") %>
  93 + <%= inst.text_field(:acronym) %>
79 94 </div>
80 95 </span>
81 96  
82 97 <span class='required-field public-institutions-fields'>
83 98 <div class="formfield type-text">
84 99 <%= inst.label("governmental_sphere_id" ,_("Governmental Sphere:"), :class=>"formlabel") %>
85   - <%= inst.select(:governmental_sphere, @governmental_sphere_options, :selected => params[:institutions][:governmental_sphere], :class => "#{params[:error_institution_governmental_sphere]}" )%>
  100 + <%= inst.select(:governmental_sphere, @governmental_sphere, {:selected=>0})%>
86 101 </div>
87 102 </span>
88 103  
89 104 <span class='required-field public-institutions-fields'>
90 105 <div class="formfield type-text">
91 106 <%= inst.label("governmental_power_id" ,_("Governmental Power:"), :class=>"formlabel") %>
92   - <%= inst.select(:governmental_power, @governmental_power_options, :selected => params[:institutions][:governmental_power], :class => "#{params[:error_institution_governmental_power]}")%>
  107 + <%= inst.select(:governmental_power, @governmental_power, {:selected=>0})%>
93 108 </div>
94 109 </span>
95 110 <span class='required-field public-institutions-fields'>
96 111 <div class="formfield type-text">
97 112 <%= inst.label("juridical_nature_id" ,_("Juridical Nature:"), :class=>"formlabel") %>
98   - <%= inst.select(:juridical_nature, @juridical_nature_options, :selected => params[:institutions][:juridical_nature], :class => "#{params[:error_institution_juridical_nature]}")%>
  113 + <%= inst.select(:juridical_nature, @juridical_nature, {:selected=>0})%>
99 114 </div>
100 115 </span>
101 116  
... ...