Commit 673c1bceda0380d7c226ae648b0fb6e87e3d05a2

Authored by Fabio Teixeira
Committed by Gabriela Navarro
1 parent 79b3cc89

fix_institution: Both admin's create and user's modal use the same action

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
controllers/mpog_software_plugin_controller.rb
@@ -48,19 +48,6 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -48,19 +48,6 @@ class MpogSoftwarePluginController &lt; ApplicationController
48 48
49 def create_institution_admin 49 def create_institution_admin
50 @url_token = split_http_referer request.original_url() 50 @url_token = split_http_referer request.original_url()
51 -  
52 - if request.post?  
53 - governmental = {}  
54 - juridical = {}  
55 - governmental[:power] = params[:institutions][:governmental_power]  
56 - governmental[:sphere] = params[:institutions][:governmental_sphere]  
57 - juridical[:nature] = params[:institutions][:juridical_nature]  
58 - params[:community][:country] = "BR"  
59 - params[:institutions].delete :governmental_power  
60 - params[:institutions].delete :governmental_sphere  
61 - params[:institutions].delete :juridical_nature  
62 - redirect_to :action => "new_institution", :community => params[:community], :institution => params[:institutions], :governmental => governmental, :juridical => juridical  
63 - end  
64 end 51 end
65 52
66 def split_http_referer http_referer 53 def split_http_referer http_referer
@@ -70,24 +57,24 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -70,24 +57,24 @@ class MpogSoftwarePluginController &lt; ApplicationController
70 return @url_token 57 return @url_token
71 end 58 end
72 59
73 -  
74 def new_institution 60 def new_institution
75 - if !params[:community].nil? and !params[:institution].nil? 61 + if !params[:community].nil? and !params[:institutions].nil?
76 response_message = {} 62 response_message = {}
77 63
78 institution = private_create_institution 64 institution = private_create_institution
79 65
80 response_message = if institution.errors.full_messages.empty? and institution.valid? and institution.save 66 response_message = if institution.errors.full_messages.empty? and institution.valid? and institution.save
81 - {:success => true, :message => _("Institution successful created!"), :institution_data=>{:name=>institution.name, :id=>institution.id}}  
82 - else  
83 - {:success => false, :message => _("Institution could not be created!"), :errors => institution.errors.full_messages}  
84 - end  
85 - if request.xhr? and (split_http_referer(request.referer()) != "create_institution_admin")  
86 - render :json => response_message.to_json  
87 - else  
88 - session[:notice] = response_message[:message] # consume the notice  
89 - redirect_to :controller => "/admin_panel", :action => "index"  
90 - end 67 + {:success => true, :message => _("Institution successful created!"), :institution_data=>{:name=>institution.name, :id=>institution.id}}
  68 + else
  69 + {:success => false, :message => _("Institution could not be created!"), :errors => institution.errors.full_messages}
  70 + end
  71 +
  72 + if request.xhr?
  73 + render :json => response_message.to_json
  74 + else
  75 + session[:notice] = response_message[:message] # consume the notice
  76 + redirect_to :controller => "/admin_panel", :action => "index"
  77 + end
91 else 78 else
92 redirect_to "/" 79 redirect_to "/"
93 end 80 end
@@ -182,10 +169,10 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -182,10 +169,10 @@ class MpogSoftwarePluginController &lt; ApplicationController
182 community = Community.new(params[:community]) 169 community = Community.new(params[:community])
183 community.environment = environment 170 community.environment = environment
184 171
185 - institution = if params[:institution][:type] == "PublicInstitution"  
186 - PublicInstitution::new params[:institution] 172 + institution = if params[:institutions][:type] == "PublicInstitution"
  173 + PublicInstitution::new params[:institutions].except(:governmental_power, :governmental_sphere, :juridical_nature)
187 else 174 else
188 - PrivateInstitution::new params[:institution] 175 + PrivateInstitution::new params[:institutions].except(:governmental_power, :governmental_sphere, :juridical_nature)
189 end 176 end
190 177
191 institution.name = community[:name] 178 institution.name = community[:name]
@@ -193,9 +180,9 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -193,9 +180,9 @@ class MpogSoftwarePluginController &lt; ApplicationController
193 180
194 if institution.type == "PublicInstitution" 181 if institution.type == "PublicInstitution"
195 begin 182 begin
196 - govPower = GovernmentalPower.find params[:governmental][:power]  
197 - govSphere = GovernmentalSphere.find params[:governmental][:sphere]  
198 - jur_nature = JuridicalNature.find params[:juridical][:nature] 183 + govPower = GovernmentalPower.find params[:institutions][:governmental_power]
  184 + govSphere = GovernmentalSphere.find params[:institutions][:governmental_sphere]
  185 + jur_nature = JuridicalNature.find params[:institutions][:juridical_nature]
199 186
200 institution.juridical_nature = jur_nature 187 institution.juridical_nature = jur_nature
201 institution.governmental_power = govPower 188 institution.governmental_power = govPower
@@ -211,7 +198,7 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -211,7 +198,7 @@ class MpogSoftwarePluginController &lt; ApplicationController
211 end 198 end
212 199
213 InstitutionHelper.register_institution_modification institution 200 InstitutionHelper.register_institution_modification institution
214 - 201 +
215 institution 202 institution
216 end 203 end
217 204
public/mpog-institution-validations.js
@@ -32,38 +32,21 @@ @@ -32,38 +32,21 @@
32 jQuery("#institutions_governmental_sphere option").selected(0); 32 jQuery("#institutions_governmental_sphere option").selected(0);
33 } 33 }
34 34
35 - function get_selected_institution_type() {  
36 - var radio_buttons = jQuery("input[type='radio'][name='type']");  
37 - var type = "";  
38 -  
39 - for( var i = 0; i < radio_buttons.length; i++ ) {  
40 - if( radio_buttons[i].checked ) {  
41 - type = radio_buttons[i].value;  
42 - break;  
43 - }  
44 - }  
45 -  
46 - return type;  
47 - }  
48 -  
49 function get_post_data() { 35 function get_post_data() {
50 return { 36 return {
51 - community : { name : jQuery("#community_name").val(), 37 + community : {
  38 + name : jQuery("#community_name").val(),
52 country : jQuery("#community_country").val(), 39 country : jQuery("#community_country").val(),
53 state : jQuery("#community_state").val(), 40 state : jQuery("#community_state").val(),
54 city : jQuery("#community_city").val() 41 city : jQuery("#community_city").val()
55 }, 42 },
56 - governmental : {  
57 - power : jQuery("#institutions_governmental_power").selected().val(),  
58 - sphere : jQuery("#institutions_governmental_sphere").selected().val()  
59 - },  
60 - juridical : {  
61 - nature : jQuery("#institutions_juridical_nature").selected().val()  
62 - },  
63 - institution : { 43 + institutions : {
64 cnpj: jQuery("#institutions_cnpj").val(), 44 cnpj: jQuery("#institutions_cnpj").val(),
65 - type: get_selected_institution_type(), 45 + type: jQuery("input[name='institutions[type]']:checked").val(),
66 acronym : jQuery("#institutions_acronym").val(), 46 acronym : jQuery("#institutions_acronym").val(),
  47 + governmental_power: jQuery("#institutions_governmental_power").selected().val(),
  48 + governmental_sphere: jQuery("#institutions_governmental_sphere").selected().val(),
  49 + juridical_nature: jQuery("#institutions_juridical_nature").selected().val()
67 }, 50 },
68 } 51 }
69 } 52 }
@@ -204,9 +187,12 @@ @@ -204,9 +187,12 @@
204 187
205 jQuery("#create_institution_link").click(open_create_institution_modal); 188 jQuery("#create_institution_link").click(open_create_institution_modal);
206 189
207 - jQuery("#type_PrivateInstitution").click(show_private_institutions_fields);  
208 -  
209 - jQuery("#type_PublicInstitution").click(show_public_institutions_fields); 190 + jQuery("input[type='radio']").click(function(){
  191 + if( this.value == "PublicInstitution" )
  192 + show_public_institutions_fields();
  193 + else
  194 + show_private_institutions_fields();
  195 + });
210 196
211 jQuery('#save_institution_button').click(save_institution); 197 jQuery('#save_institution_button').click(save_institution);
212 198
views/mpog_software_plugin/_institution.html.erb
@@ -20,11 +20,11 @@ @@ -20,11 +20,11 @@
20 <div id = 'create_institution_errors' class='errorExplanation hide-field'></div> 20 <div id = 'create_institution_errors' class='errorExplanation hide-field'></div>
21 21
22 <div> 22 <div>
23 - <%= labelled_form_for :community, :html => { :multipart => true, :id=>"institution_form" } do |f| %> 23 + <%= labelled_form_for :community, :url => {:action=>"new_institution"}, :html => { :multipart => true, :id=>"institution_form" } do |f| %>
24 <%= required f.text_field(:name) %> 24 <%= required f.text_field(:name) %>
25 <%= content_tag :span, _("Institution name already exists"), :id=>"already_exists_text", :class=>"errorExplanation hide-field" %> 25 <%= content_tag :span, _("Institution name already exists"), :id=>"already_exists_text", :class=>"errorExplanation hide-field" %>
26 26
27 - <%= required select_country(_('Country'), f, 'country', {:class => 'type-select', :id => "community_country"}) %> 27 + <%= required select_country(_('Country'), 'community', 'country', {:class => 'type-select', :id => "community_country"}) %>
28 <%= required f.text_field(:state) %> 28 <%= required f.text_field(:state) %>
29 <%= required f.text_field(:city) %> 29 <%= required f.text_field(:city) %>
30 30
@@ -39,10 +39,14 @@ @@ -39,10 +39,14 @@
39 39
40 <span class=''> 40 <span class=''>
41 <div class='formfield type-radio'> 41 <div class='formfield type-radio'>
42 - <%= label_tag("type_PublicInstitution", _("Public Institution")) %>  
43 - <%= radio_button_tag(:type,"PublicInstitution", true)%>  
44 - <%= label_tag("type_PrivateInstitution", _("Private Institution")) %>  
45 - <%= radio_button_tag(:type,"PrivateInstitution")%> 42 + <label> <%= _("Public Institution") %>
  43 + <%= radio_button_tag("institutions[type]", "PublicInstitution", true) %>
  44 + </label>
  45 +
  46 + <label>
  47 + <%= _("Private Institution") %>
  48 + <%= radio_button_tag("institutions[type]" ,"PrivateInstitution")%>
  49 + </label>
46 </div> 50 </div>
47 </span> 51 </span>
48 52