Commit cc6c80785789a92abb0f80ce85f46021408183b9

Authored by Luciano Prestes
Committed by David Silva
1 parent bf872916
Exists in master and in 79 other branches add_sisp_to_chef, add_super_archives_plugin, api_for_colab, automates_core_packing, backup_not_prod, changes_in_buttons_on_content_panel, colab_automated_login, colab_spb_plugin_recipe, colab_widgets_settings, design_validation, dev_env_minimal, disable_email_dev, fix_breadcrumbs_position, fix_categories_software_link, fix_edit_institution, fix_edit_software_with_another_license, fix_get_license_info, fix_gitlab_assets_permission, fix_list_style_inside_article, fix_list_style_on_folder_elements, fix_members_pagination, fix_merge_request_url, fix_models_translations, fix_no_license, fix_software_api, fix_software_block_migration, fix_software_communities_translations, fix_software_communities_unit_test, fix_style_create_institution_admin_panel, fix_superarchives_imports, fix_sym_links_noosfero, focus_search_field_theme, gov-user-refactoring, gov-user-refactoring-rails4, header_fix, institution_modal_on_rating, kalibro-conf-refactoring, kalibro-processor-package, lxc_settings, margin_fix, mezuro_cookbook, prezento, refactor_download_block, refactor_software_communities, refactor_software_for_sisp, register_page, release-process, release-process-v2, remove-unused-images, remove_broken_theme, remove_secondary_email_from_user, remove_sisp_buttons, removing_super_archives_email, review_message, scope2method, signals_user_noosfero, sisp_catalog_header, sisp_colab_config, sisp_dev, sisp_dev_master, sisp_simple_version, software_as_organization, software_catalog_style_fix, software_communities_html_refactor, software_infos_api, spb_minimal_env, spb_to_rails4, spec_refactor, stable-4.1, stable-4.2, stable-4.x, temp_soft_comm_refactoring, theme_header, theme_javascript_refactory, thread_dropdown, thread_page, update_search_by_categories, update_software_api, update_softwares_boxes

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 3 include InstitutionHelper
4 4  
5 5 def check_reactivate_account
6   - if request.xhr? and params[:email]
  6 + if request.xhr? && params[:email]
7 7 result = ""
8 8 user = User.where(:email => params[:email])
9 9  
... ... @@ -18,7 +18,7 @@ class MpogSoftwarePluginController &lt; ApplicationController
18 18 def hide_registration_incomplete_percentage
19 19 response = false
20 20  
21   - if request.xhr? and params[:hide]
  21 + if request.xhr? && params[:hide]
22 22 session[:hide_incomplete_percentage] = true
23 23 response = session[:hide_incomplete_percentage]
24 24 end
... ... @@ -52,113 +52,61 @@ class MpogSoftwarePluginController &lt; ApplicationController
52 52 end
53 53  
54 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 73 end
90 74 end
91 75  
92 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 82 end
101 83  
102 84 def download
103 85 respond_to do |format|
104 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 89 end
140 90 end
141 91  
142 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 99 render :json => list.to_json
152 100 end
153 101  
154 102 def get_categories
  103 + redirect_to "/" if !request.xhr? || params[:query].blank?
  104 +
155 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 111 render :json => list.to_json
164 112 end
... ... @@ -171,19 +119,18 @@ class MpogSoftwarePluginController &lt; ApplicationController
171 119 end
172 120  
173 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 125 when "database"
179   - model = DatabaseDescription
  126 + DatabaseDescription
180 127 when "software_language"
181   - model = ProgrammingLanguage
  128 + ProgrammingLanguage
182 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 134 {:id=>db.id, :label=>db.name}
188 135 }
189 136 other = [model.select("id, name").last].collect { |db|
... ... @@ -199,6 +146,8 @@ class MpogSoftwarePluginController &lt; ApplicationController
199 146 protected
200 147  
201 148 def get_state_list
  149 + redirect_to "/" unless request.xhr?
  150 +
202 151 NationalRegion.find(:all, :conditions=>["national_region_type_id = ?", 2], :order=>"name")
203 152 end
204 153  
... ... @@ -260,4 +209,78 @@ class MpogSoftwarePluginController &lt; ApplicationController
260 209 end
261 210 software_list
262 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 286 end
... ...