Commit cc6c80785789a92abb0f80ce85f46021408183b9

Authored by Luciano Prestes
Committed by David Silva
1 parent bf872916

Refactor MpogSoftwarePluginController

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
Showing 1 changed file with 114 additions and 91 deletions   Show diff stats
controllers/mpog_software_plugin_controller.rb
@@ -3,7 +3,7 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -3,7 +3,7 @@ class MpogSoftwarePluginController &lt; ApplicationController
3 include InstitutionHelper 3 include InstitutionHelper
4 4
5 def check_reactivate_account 5 def check_reactivate_account
6 - if request.xhr? and params[:email] 6 + if request.xhr? && params[:email]
7 result = "" 7 result = ""
8 user = User.where(:email => params[:email]) 8 user = User.where(:email => params[:email])
9 9
@@ -18,7 +18,7 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -18,7 +18,7 @@ class MpogSoftwarePluginController &lt; ApplicationController
18 def hide_registration_incomplete_percentage 18 def hide_registration_incomplete_percentage
19 response = false 19 response = false
20 20
21 - if request.xhr? and params[:hide] 21 + if request.xhr? && params[:hide]
22 session[:hide_incomplete_percentage] = true 22 session[:hide_incomplete_percentage] = true
23 response = session[:hide_incomplete_percentage] 23 response = session[:hide_incomplete_percentage]
24 end 24 end
@@ -52,113 +52,61 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -52,113 +52,61 @@ class MpogSoftwarePluginController &lt; ApplicationController
52 end 52 end
53 53
54 def new_institution 54 def new_institution
55 - if !params[:community].nil? and !params[:institutions].nil?  
56 - response_message = {} 55 + redirect_to "/" if params[:community].blank? || params[:institutions].blank?
57 56
58 - institution_template = Community["institution"]  
59 - if (!institution_template.blank? && institution_template.is_template)  
60 - params[:community][:template_id] = institution_template.id unless params[:community].blank?  
61 - end 57 + response_message = {}
62 58
63 - institution = private_create_institution()  
64 - if environment.admins.include?(current_user.person) and params[:edit_institution_page] == false  
65 - environment.admins.each do |adm|  
66 - institution.community.add_admin(adm)  
67 - end  
68 - end  
69 - response_message = if institution.errors.full_messages.empty? and institution.community.errors.full_messages.empty? and institution.valid? and institution.save  
70 - {:success => true, :message => _("Institution successful created!"), :institution_data=>{:name=>institution.name, :id=>institution.id}}  
71 - else  
72 - {:success => false, :message => _("Institution could not be created!"), :errors => institution.errors.full_messages << institution.community.errors.full_messages}  
73 - end 59 + institution_template = Community["institution"]
  60 + add_template_in_params institution_template
74 61
75 - if request.xhr?  
76 - render :json => response_message.to_json  
77 - else  
78 - session[:notice] = response_message[:message] # consume the notice 62 + institution = private_create_institution
  63 + add_environment_admins_to_institution institution
79 64
80 - if response_message[:success]  
81 - redirect_to :controller => "/admin_panel", :action => "index"  
82 - else  
83 - flash[:errors] = response_message[:errors]  
84 - redirect_to :controller => "mpog_software_plugin", :action => "create_institution_admin"  
85 - end  
86 - end  
87 - else  
88 - redirect_to "/" 65 + response_message = save_institution institution
  66 +
  67 + if request.xhr? #User create institution
  68 + render :json => response_message.to_json
  69 + else #Admin create institution
  70 + session[:notice] = response_message[:message] # consume the notice
  71 +
  72 + redirect_depending_on_institution_creation response_message
89 end 73 end
90 end 74 end
91 75
92 def institution_already_exists 76 def institution_already_exists
93 - if request.xhr? and !params[:name].nil?  
94 - already_exists = !Community.where(:name=>params[:name]).empty? 77 + redirect_to "/" if !request.xhr? || params[:name].blank?
95 78
96 - render :json=>already_exists.to_json  
97 - else  
98 - redirect_to "/"  
99 - end 79 + already_exists = !Community.where(:name=>params[:name]).empty?
  80 +
  81 + render :json=>already_exists.to_json
100 end 82 end
101 83
102 def download 84 def download
103 respond_to do |format| 85 respond_to do |format|
104 format.html 86 format.html
105 - format.xml do  
106 - softwares = software_list_to_correct_format(SoftwareInfo.all)  
107 - send_data softwares.to_xml(  
108 - :skip_types => true,  
109 - :only => %w[name acronym demonstration_url e_arq e_mag e_ping features icp_brasil objectives operating_platform languages_list database_list]),  
110 - :type => 'text/xml',  
111 - :disposition => "attachment; filename=softwares.xml"  
112 - end  
113 -  
114 - format.csv do  
115 - softwares = software_list_to_correct_format(SoftwareInfo.all)  
116 - csv_content = ""  
117 - softwares.each { |s|  
118 - csv_content << "name;acronym;demonstration_url;e_arq;e_mag;e_ping;features;icp_brasil;objectives;operating_platform\n"  
119 - csv_content << CSV.generate_line([s['name'], s['acronym'], s['demonstration_url'], s['e_arq'], s['e_mag'], s['e_ping'], s['features'], s['icp_brasil'], s['objectives'], s['operating_platform']], {:col_sep => ';'})  
120 -  
121 - csv_content << "\nlanguage_name;language_version;language_operating_system\n"  
122 - s[:languages_list].each { |sl|  
123 - csv_content << CSV.generate_line([sl[:name], sl[:version], sl[:operating_system]], {:col_sep => ';'})  
124 - }  
125 -  
126 - csv_content << "\ndatabase_name;database_version;database_operating_system\n"  
127 - s[:database_list].each { |dl|  
128 - csv_content << CSV.generate_line([dl[:name], dl[:version], dl[:operating_system]], {:col_sep => ';'})  
129 - }  
130 -  
131 - csv_content << "\n\n"  
132 - }  
133 - if csv_content.blank?  
134 - csv_content = "name;acronym;demonstration_url;e_arq;e_mag;e_ping;features;icp_brasil;objectives;operating_platform\n"  
135 - end  
136 -  
137 - render :text => csv_content, :content_type => 'text/csv', :layout => false  
138 - end 87 + format_xml_download format
  88 + format_csv_download format
139 end 89 end
140 end 90 end
141 91
142 def get_institutions 92 def get_institutions
143 - list = [] 93 + redirect_to "/" if !request.xhr? || params[:query].blank?
144 94
145 - if request.xhr? and params[:query]  
146 - list = Institution.search_institution(params[:query]).map{ |institution|  
147 - {:value=>institution.name, :id=>institution.id}  
148 - }  
149 - end 95 + list = Institution.search_institution(params[:query]).map{ |institution|
  96 + {:value=>institution.name, :id=>institution.id}
  97 + }
150 98
151 render :json => list.to_json 99 render :json => list.to_json
152 end 100 end
153 101
154 def get_categories 102 def get_categories
  103 + redirect_to "/" if !request.xhr? || params[:query].blank?
  104 +
155 list = [] 105 list = []
156 106
157 - if request.xhr? and params[:query]  
158 - Category.where("name ILIKE ?", "%#{params[:query]}%").collect { |c|  
159 - list << {:label=>c.name, :id=>c.id} if c.name != "Software"  
160 - }  
161 - end 107 + Category.where("name ILIKE ?", "%#{params[:query]}%").collect { |c|
  108 + list << {:label=>c.name, :id=>c.id} if c.name != "Software"
  109 + }
162 110
163 render :json => list.to_json 111 render :json => list.to_json
164 end 112 end
@@ -171,19 +119,18 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -171,19 +119,18 @@ class MpogSoftwarePluginController &lt; ApplicationController
171 end 119 end
172 120
173 def get_field_data 121 def get_field_data
174 - return render :json=>{} if !request.xhr? or params[:query].nil? or params[:field].nil? 122 + return render :json=>{} if !request.xhr? || params[:query].nil? || params[:field].nil?
175 123
176 - model = nil  
177 - case params[:field] 124 + model = case params[:field]
178 when "database" 125 when "database"
179 - model = DatabaseDescription 126 + DatabaseDescription
180 when "software_language" 127 when "software_language"
181 - model = ProgrammingLanguage 128 + ProgrammingLanguage
182 else 129 else
183 - model = DatabaseDescription  
184 - end 130 + DatabaseDescription
  131 + end
185 132
186 - data = model.where("name ILIKE ?", "%#{params[:query]}%").select("id, name").collect {|db| 133 + data = model.where("name ILIKE ?", "%#{params[:query]}%").select("id, name").collect { |db|
187 {:id=>db.id, :label=>db.name} 134 {:id=>db.id, :label=>db.name}
188 } 135 }
189 other = [model.select("id, name").last].collect { |db| 136 other = [model.select("id, name").last].collect { |db|
@@ -199,6 +146,8 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -199,6 +146,8 @@ class MpogSoftwarePluginController &lt; ApplicationController
199 protected 146 protected
200 147
201 def get_state_list 148 def get_state_list
  149 + redirect_to "/" unless request.xhr?
  150 +
202 NationalRegion.find(:all, :conditions=>["national_region_type_id = ?", 2], :order=>"name") 151 NationalRegion.find(:all, :conditions=>["national_region_type_id = ?", 2], :order=>"name")
203 end 152 end
204 153
@@ -260,4 +209,78 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -260,4 +209,78 @@ class MpogSoftwarePluginController &lt; ApplicationController
260 end 209 end
261 software_list 210 software_list
262 end 211 end
  212 +
  213 + def add_template_in_params institution_template
  214 + if (!institution_template.blank? && institution_template.is_template)
  215 + params[:community][:template_id] = institution_template.id unless params[:community].blank?
  216 + end
  217 + end
  218 +
  219 + def add_environment_admins_to_institution institution
  220 + if environment.admins.include?(current_user.person) && params[:edit_institution_page] == false
  221 + environment.admins.each do |adm|
  222 + institution.community.add_admin(adm)
  223 + end
  224 + end
  225 + end
  226 +
  227 + def save_institution institution
  228 + if institution.errors.full_messages.empty? and institution.community.errors.full_messages.empty? and institution.valid? and institution.save
  229 + {:success => true, :message => _("Institution successful created!"), :institution_data=>{:name=>institution.name, :id=>institution.id}}
  230 + else
  231 + {:success => false, :message => _("Institution could not be created!"), :errors => institution.errors.full_messages << institution.community.errors.full_messages}
  232 + end
  233 + end
  234 +
  235 + def redirect_depending_on_institution_creation response_message
  236 + if response_message[:success]
  237 + redirect_to :controller => "/admin_panel", :action => "index"
  238 + else
  239 + flash[:errors] = response_message[:errors]
  240 + redirect_to :controller => "mpog_software_plugin", :action => "create_institution_admin"
  241 + end
  242 + end
  243 +
  244 + def format_xml_download format
  245 + format.xml do
  246 + softwares = software_list_to_correct_format(SoftwareInfo.all)
  247 + send_data(
  248 + softwares.to_xml(
  249 + :skip_types => true,
  250 + :only => %w[name acronym demonstration_url e_arq e_mag e_ping features icp_brasil objectives operating_platform languages_list database_list]
  251 + ),
  252 + :type => 'text/xml',
  253 + :disposition => "attachment; filename=softwares.xml")
  254 + end
  255 + end
  256 +
  257 + def format_csv_download format
  258 + format.csv do
  259 + softwares = software_list_to_correct_format(SoftwareInfo.all)
  260 + csv_content = ""
  261 +
  262 + softwares.each do |s|
  263 + csv_content << "name;acronym;demonstration_url;e_arq;e_mag;e_ping;features;icp_brasil;objectives;operating_platform\n"
  264 + csv_content << CSV.generate_line([s['name'], s['acronym'], s['demonstration_url'], s['e_arq'], s['e_mag'], s['e_ping'], s['features'], s['icp_brasil'], s['objectives'], s['operating_platform']], {:col_sep => ';'})
  265 +
  266 + csv_content << "\nlanguage_name;language_version;language_operating_system\n"
  267 + s[:languages_list].each { |sl|
  268 + csv_content << CSV.generate_line([sl[:name], sl[:version], sl[:operating_system]], {:col_sep => ';'})
  269 + }
  270 +
  271 + csv_content << "\ndatabase_name;database_version;database_operating_system\n"
  272 + s[:database_list].each { |dl|
  273 + csv_content << CSV.generate_line([dl[:name], dl[:version], dl[:operating_system]], {:col_sep => ';'})
  274 + }
  275 +
  276 + csv_content << "\n\n"
  277 + end
  278 +
  279 + if csv_content.blank?
  280 + csv_content = "name;acronym;demonstration_url;e_arq;e_mag;e_ping;features;icp_brasil;objectives;operating_platform\n"
  281 + end
  282 +
  283 + render :text => csv_content, :content_type => 'text/csv', :layout => false
  284 + end
  285 + end
263 end 286 end