Commit 15dcbd865c900c4a023f24e8354031554651bf73

Authored by Fabio Teixeira
1 parent c94eae34
Exists in gov_user_refactory

Better shared logic in create_institution

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Showing 1 changed file with 45 additions and 36 deletions   Show diff stats
controllers/gov_user_plugin_controller.rb
@@ -13,15 +13,7 @@ class GovUserPluginController &lt; ApplicationController @@ -13,15 +13,7 @@ class GovUserPluginController &lt; ApplicationController
13 end 13 end
14 14
15 def create_institution 15 def create_institution
16 - @show_sisp_field = environment.admins.include?(current_user.person)  
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]}  
21 - @state_options = [[_('Select a state'), '-1']] | @state_list.collect {|state| [state.name, state.name]}  
22 -  
23 - params[:community] ||= {}  
24 - params[:institutions] ||= {} 16 + create_institution_view_variables
25 17
26 if request.xhr? 18 if request.xhr?
27 render :layout=>false 19 render :layout=>false
@@ -30,26 +22,10 @@ class GovUserPluginController &lt; ApplicationController @@ -30,26 +22,10 @@ class GovUserPluginController &lt; ApplicationController
30 end 22 end
31 end 23 end
32 24
33 - def split_http_referer http_referer  
34 - split_list = []  
35 - split_list = http_referer.split("/")  
36 - @url_token = split_list.last  
37 - return @url_token  
38 - end  
39 -  
40 def create_institution_admin 25 def create_institution_admin
41 - @show_sisp_field = environment.admins.include?(current_user.person)  
42 - @state_list = get_state_list()  
43 - @governmental_sphere = [[_("Select a Governmental Sphere"), 0]]|GovernmentalSphere.all.map {|s| [s.name, s.id]}  
44 - @governmental_power = [[_("Select a Governmental Power"), 0]]|GovernmentalPower.all.map {|g| [g.name, g.id]}  
45 - @juridical_nature = [[_("Select a Juridical Nature"), 0]]|JuridicalNature.all.map {|j| [j.name, j.id]}  
46 - @state_options = [[_('Select a state'), '-1']] | @state_list.collect {|state| [state.name, state.name]} 26 + create_institution_view_variables
47 27
48 @url_token = split_http_referer request.original_url() 28 @url_token = split_http_referer request.original_url()
49 -  
50 - params[:community] ||= {}  
51 - params[:institutions] ||= {}  
52 -  
53 end 29 end
54 30
55 def new_institution 31 def new_institution
@@ -77,7 +53,7 @@ class GovUserPluginController &lt; ApplicationController @@ -77,7 +53,7 @@ class GovUserPluginController &lt; ApplicationController
77 def institution_already_exists 53 def institution_already_exists
78 redirect_to "/" if !request.xhr? || params[:name].blank? 54 redirect_to "/" if !request.xhr? || params[:name].blank?
79 55
80 - already_exists = !Community.where(:name=>params[:name]).empty? 56 + already_exists = !Institution.find_by_name(params[:name]).nil?
81 57
82 render :json=>already_exists.to_json 58 render :json=>already_exists.to_json
83 end 59 end
@@ -85,18 +61,18 @@ class GovUserPluginController &lt; ApplicationController @@ -85,18 +61,18 @@ class GovUserPluginController &lt; ApplicationController
85 def get_institutions 61 def get_institutions
86 redirect_to "/" if !request.xhr? || params[:query].blank? 62 redirect_to "/" if !request.xhr? || params[:query].blank?
87 63
88 - list = Institution.search_institution(params[:query]).map{ |institution| 64 + institutions = Institution.search_institution(params[:query]).select([:id, :name])
  65 + institutions_list = institutions.map { |institution|
89 {:value=>institution.name, :id=>institution.id} 66 {:value=>institution.name, :id=>institution.id}
90 } 67 }
91 68
92 - render :json => list.to_json 69 + render :json => institutions_list.to_json
93 end 70 end
94 71
95 def get_brazil_states 72 def get_brazil_states
96 redirect_to "/" unless request.xhr? 73 redirect_to "/" unless request.xhr?
97 74
98 - state_list = get_state_list()  
99 - render :json=>state_list.collect {|state| state.name }.to_json 75 + render :json=>get_state_list().to_json
100 end 76 end
101 77
102 def get_field_data 78 def get_field_data
@@ -122,6 +98,24 @@ class GovUserPluginController &lt; ApplicationController @@ -122,6 +98,24 @@ class GovUserPluginController &lt; ApplicationController
122 98
123 protected 99 protected
124 100
  101 + def split_http_referer http_referer=""
  102 + split_list = http_referer.split("/")
  103 + split_list.last
  104 + end
  105 +
  106 + def create_institution_view_variables
  107 + params[:community] ||= {}
  108 + params[:institutions] ||= {}
  109 +
  110 + @show_sisp_field = environment.admins.include?(current_user.person)
  111 + @governmental_sphere = get_governmental_spheres()
  112 + @governmental_power = get_governmental_powers()
  113 + @juridical_nature = get_juridical_natures()
  114 +
  115 + state_list = get_state_list()
  116 + @state_options = state_list.zip(state_list).prepend([_('Select a state'), '-1'])
  117 + end
  118 +
125 def get_model_by_params_field 119 def get_model_by_params_field
126 case params[:field] 120 case params[:field]
127 when "software_language" 121 when "software_language"
@@ -132,11 +126,26 @@ class GovUserPluginController &lt; ApplicationController @@ -132,11 +126,26 @@ class GovUserPluginController &lt; ApplicationController
132 end 126 end
133 127
134 def get_state_list 128 def get_state_list
135 - NationalRegion.find(  
136 - :all,  
137 - :conditions=>["national_region_type_id = ?", 2],  
138 - :order=>"name"  
139 - ) 129 + NationalRegion.select(:name).where(:national_region_type_id => 2).order(:name).map &:name
  130 + end
  131 +
  132 + def get_governmental_spheres
  133 + spheres = [[_("Select a Governmental Sphere"), 0]]
  134 + spheres.concat get_model_as_option_list(GovernmentalSphere)
  135 + end
  136 +
  137 + def get_governmental_powers
  138 + powers = [[_("Select a Governmental Power"), 0]]
  139 + powers.concat get_model_as_option_list(GovernmentalPower)
  140 + end
  141 +
  142 + def get_juridical_natures
  143 + natures = [[_("Select a Juridical Nature"), 0]]
  144 + natures.concat get_model_as_option_list(JuridicalNature)
  145 + end
  146 +
  147 + def get_model_as_option_list model
  148 + model.select([:id, :name]).map {|m| [m.name, m.id]}
140 end 149 end
141 150
142 def set_institution_type 151 def set_institution_type