Commit 529ab61915d5a08d14adf7459ed82cf466fd2227

Authored by Thiago Ribeiro
2 parents 53326c17 23aa4a1c
Exists in master and in 70 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, 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_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_broken_theme, remove_secondary_email_from_user, remove_sisp_buttons, removing_super_archives_email, review_message, scope2method, signals_user_noosfero, sisp_catalog_header, sisp_dev_master, software_as_organization, software_catalog_style_fix, software_communities_html_refactor, software_infos_api, spb_to_rails4, spec_refactor, stable-4.1, stable-4.2, temp_soft_comm_refactoring, theme_header, theme_javascript_refactory, thread_dropdown, thread_page, update_search_by_categories, update_softwares_boxes

Merge branch 'refactor_gov_user' into 'master'

Refactor gov user

See merge request !51
src/noosfero-spb/gov_user/codeclimate.yml 0 → 100644
@@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
  1 +# This is a sample .codeclimate.yml configured for Engine analysis on Code
  2 +# Climate Platform. For an overview of the Code Climate Platform, see here:
  3 +# http://docs.codeclimate.com/article/300-the-codeclimate-platform
  4 +
  5 +# Under the engines key, you can configure which engines will analyze your repo.
  6 +# Each key is an engine name. For each value, you need to specify enabled: true
  7 +# to enable the engine as well as any other engines-specific configuration.
  8 +
  9 +# For more details, see here:
  10 +# http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
  11 +
  12 +# For a list of all available engines, see here:
  13 +# http://docs.codeclimate.com/article/296-engines-available-engines
  14 +
  15 +engines:
  16 +# to turn on an engine, add it here and set enabled to `true`
  17 +# to turn off an engine, set enabled to `false` or remove it
  18 + rubocop:
  19 + enabled: true
  20 + golint:
  21 + enabled: true
  22 + gofmt:
  23 + enabled: true
  24 + eslint:
  25 + enabled: true
  26 + csslint:
  27 + enabled: true
  28 +
  29 +# Engines can analyze files and report issues on them, but you can separately
  30 +# decide which files will receive ratings based on those issues. This is
  31 +# specified by path patterns under the ratings key.
  32 +
  33 +# For more details see here:
  34 +# http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
  35 +
  36 +# Note: If the ratings key is not specified, this will result in a 0.0 GPA on your dashboard.
  37 +
  38 +ratings:
  39 + paths:
  40 + - **.rb
  41 + - **.js
  42 + - **.css
  43 +# - lib/**
  44 +# - "**.rb"
  45 +# - "**.go"
  46 +
  47 +# You can globally exclude files from being analyzed by any engine using the
  48 +# exclude_paths key.
  49 +
  50 +exclude_paths:
  51 + - public/vendor/*
  52 + - features/**/*
  53 +#- spec/**/*
  54 +#- vendor/**/*
src/noosfero-spb/gov_user/controllers/gov_user_plugin_controller.rb
1 #aqui deve ter so usuario e instituicao 1 #aqui deve ter so usuario e instituicao
2 class GovUserPluginController < ApplicationController 2 class GovUserPluginController < ApplicationController
  3 + VERIFY_ERRORS_IN = [
  4 + :name, :country, :state, :city, :corporate_name, :cnpj,
  5 + :governmental_sphere, :governmental_power, :juridical_nature, :sisp
  6 + ]
3 7
4 def hide_registration_incomplete_percentage 8 def hide_registration_incomplete_percentage
5 response = false 9 response = false
@@ -13,15 +17,7 @@ class GovUserPluginController &lt; ApplicationController @@ -13,15 +17,7 @@ class GovUserPluginController &lt; ApplicationController
13 end 17 end
14 18
15 def create_institution 19 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] ||= {} 20 + create_institution_view_variables
25 21
26 if request.xhr? 22 if request.xhr?
27 render :layout=>false 23 render :layout=>false
@@ -30,26 +26,10 @@ class GovUserPluginController &lt; ApplicationController @@ -30,26 +26,10 @@ class GovUserPluginController &lt; ApplicationController
30 end 26 end
31 end 27 end
32 28
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 29 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]} 30 + create_institution_view_variables
47 31
48 @url_token = split_http_referer request.original_url() 32 @url_token = split_http_referer request.original_url()
49 -  
50 - params[:community] ||= {}  
51 - params[:institutions] ||= {}  
52 -  
53 end 33 end
54 34
55 def new_institution 35 def new_institution
@@ -77,7 +57,7 @@ class GovUserPluginController &lt; ApplicationController @@ -77,7 +57,7 @@ class GovUserPluginController &lt; ApplicationController
77 def institution_already_exists 57 def institution_already_exists
78 redirect_to "/" if !request.xhr? || params[:name].blank? 58 redirect_to "/" if !request.xhr? || params[:name].blank?
79 59
80 - already_exists = !Community.where(:name=>params[:name]).empty? 60 + already_exists = !Institution.find_by_name(params[:name]).nil?
81 61
82 render :json=>already_exists.to_json 62 render :json=>already_exists.to_json
83 end 63 end
@@ -85,18 +65,18 @@ class GovUserPluginController &lt; ApplicationController @@ -85,18 +65,18 @@ class GovUserPluginController &lt; ApplicationController
85 def get_institutions 65 def get_institutions
86 redirect_to "/" if !request.xhr? || params[:query].blank? 66 redirect_to "/" if !request.xhr? || params[:query].blank?
87 67
88 - list = Institution.search_institution(params[:query]).map{ |institution| 68 + institutions = Institution.search_institution(params[:query]).select([:id, :name])
  69 + institutions_list = institutions.map { |institution|
89 {:value=>institution.name, :id=>institution.id} 70 {:value=>institution.name, :id=>institution.id}
90 } 71 }
91 72
92 - render :json => list.to_json 73 + render :json => institutions_list.to_json
93 end 74 end
94 75
95 def get_brazil_states 76 def get_brazil_states
96 redirect_to "/" unless request.xhr? 77 redirect_to "/" unless request.xhr?
97 78
98 - state_list = get_state_list()  
99 - render :json=>state_list.collect {|state| state.name }.to_json 79 + render :json=>get_state_list().to_json
100 end 80 end
101 81
102 def get_field_data 82 def get_field_data
@@ -122,6 +102,24 @@ class GovUserPluginController &lt; ApplicationController @@ -122,6 +102,24 @@ class GovUserPluginController &lt; ApplicationController
122 102
123 protected 103 protected
124 104
  105 + def split_http_referer http_referer=""
  106 + split_list = http_referer.split("/")
  107 + split_list.last
  108 + end
  109 +
  110 + def create_institution_view_variables
  111 + params[:community] ||= {}
  112 + params[:institutions] ||= {}
  113 +
  114 + @show_sisp_field = environment.admins.include?(current_user.person)
  115 + @governmental_sphere = get_governmental_spheres()
  116 + @governmental_power = get_governmental_powers()
  117 + @juridical_nature = get_juridical_natures()
  118 +
  119 + state_list = get_state_list()
  120 + @state_options = state_list.zip(state_list).prepend([_('Select a state'), '-1'])
  121 + end
  122 +
125 def get_model_by_params_field 123 def get_model_by_params_field
126 case params[:field] 124 case params[:field]
127 when "software_language" 125 when "software_language"
@@ -132,11 +130,26 @@ class GovUserPluginController &lt; ApplicationController @@ -132,11 +130,26 @@ class GovUserPluginController &lt; ApplicationController
132 end 130 end
133 131
134 def get_state_list 132 def get_state_list
135 - NationalRegion.find(  
136 - :all,  
137 - :conditions=>["national_region_type_id = ?", 2],  
138 - :order=>"name"  
139 - ) 133 + NationalRegion.select(:name).where(:national_region_type_id => 2).order(:name).map &:name
  134 + end
  135 +
  136 + def get_governmental_spheres
  137 + spheres = [[_("Select a Governmental Sphere"), 0]]
  138 + spheres.concat get_model_as_option_list(GovernmentalSphere)
  139 + end
  140 +
  141 + def get_governmental_powers
  142 + powers = [[_("Select a Governmental Power"), 0]]
  143 + powers.concat get_model_as_option_list(GovernmentalPower)
  144 + end
  145 +
  146 + def get_juridical_natures
  147 + natures = [[_("Select a Juridical Nature"), 0]]
  148 + natures.concat get_model_as_option_list(JuridicalNature)
  149 + end
  150 +
  151 + def get_model_as_option_list model
  152 + model.select([:id, :name]).map {|m| [m.name, m.id]}
140 end 153 end
141 154
142 def set_institution_type 155 def set_institution_type
@@ -236,16 +249,20 @@ class GovUserPluginController &lt; ApplicationController @@ -236,16 +249,20 @@ class GovUserPluginController &lt; ApplicationController
236 institution.valid? if institution 249 institution.valid? if institution
237 institution.community.valid? if institution.community 250 institution.community.valid? if institution.community
238 251
239 - flash[:error_community_name] = institution.community.errors.include?(:name) ? "highlight-error" : ""  
240 - flash[:error_community_country] = institution.errors.include?(:country) ? "highlight-error" : ""  
241 - flash[:error_community_state] = institution.errors.include?(:state) ? "highlight-error" : ""  
242 - flash[:error_community_city] = institution.errors.include?(:city) ? "highlight-error" : ""  
243 - flash[:error_institution_corporate_name] = institution.errors.include?(:corporate_name) ? "highlight-error" : ""  
244 - flash[:error_institution_cnpj] = institution.errors.include?(:cnpj) ? "highlight-error" : ""  
245 - flash[:error_institution_governmental_sphere] = institution.errors.include?(:governmental_sphere) ? "highlight-error" : ""  
246 - flash[:error_institution_governmental_power] = institution.errors.include?(:governmental_power) ? "highlight-error" : ""  
247 - flash[:error_institution_juridical_nature] = institution.errors.include?(:juridical_nature) ? "highlight-error" : ""  
248 - flash[:error_institution_sisp] = institution.errors.include?(:sisp) ? "highlight-error" : "" 252 + dispatch_flash_errors institution, "institution"
  253 + dispatch_flash_errors institution.community, "community"
  254 + end
  255 +
  256 + def dispatch_flash_errors model, flash_key_base
  257 + model.errors.messages.keys.each do |error_key|
  258 + flash_key = "error_#{flash_key_base}_#{error_key}".to_sym
  259 +
  260 + if VERIFY_ERRORS_IN.include? error_key
  261 + flash[flash_key] = "highlight-error"
  262 + else
  263 + flash[flash_key] = ""
  264 + end
  265 + end
249 end 266 end
250 267
251 end 268 end
src/noosfero-spb/gov_user/public/app.js
  1 +/* globals modulejs */
  2 +
1 (function() { 3 (function() {
2 'use strict'; 4 'use strict';
3 5
src/noosfero-spb/gov_user/public/initializer.js
  1 +/* globals modulejs */
  2 +
1 (function() { 3 (function() {
2 'use strict'; 4 'use strict';
3 5
src/noosfero-spb/gov_user/public/lib/noosfero-root.js
  1 +/* globals modulejs */
  2 +
1 modulejs.define('NoosferoRoot', function() { 3 modulejs.define('NoosferoRoot', function() {
2 'use strict'; 4 'use strict';
3 5
src/noosfero-spb/gov_user/public/lib/select-element.js
  1 +/* globals modulejs */
  2 +
1 modulejs.define('SelectElement', function() { 3 modulejs.define('SelectElement', function() {
2 'use strict'; 4 'use strict';
3 5
src/noosfero-spb/gov_user/public/lib/select-field-choices.js
  1 +/* globals modulejs */
  2 +
1 modulejs.define('SelectFieldChoices', ['jquery', 'SelectElement'], function($, SelectElement) { 3 modulejs.define('SelectFieldChoices', ['jquery', 'SelectElement'], function($, SelectElement) {
2 'use strict'; 4 'use strict';
3 5
src/noosfero-spb/gov_user/public/views/complete-registration.js
  1 +/* globals modulejs */
  2 +
1 modulejs.define('CompleteRegistration', ['jquery', 'NoosferoRoot'], function($, NoosferoRoot) { 3 modulejs.define('CompleteRegistration', ['jquery', 'NoosferoRoot'], function($, NoosferoRoot) {
2 'use strict'; 4 'use strict';
3 5
src/noosfero-spb/gov_user/public/views/control-panel.js
  1 +/* globals modulejs */
  2 +
1 modulejs.define('ControlPanel', ['jquery'], function($) { 3 modulejs.define('ControlPanel', ['jquery'], function($) {
2 'use strict'; 4 'use strict';
3 5
src/noosfero-spb/gov_user/public/views/create-institution.js
  1 +/* globals modulejs */
  2 +
1 modulejs.define('CreateInstitution', ['jquery', 'NoosferoRoot', 'SelectElement'], function($, NoosferoRoot, SelectElement) { 3 modulejs.define('CreateInstitution', ['jquery', 'NoosferoRoot', 'SelectElement'], function($, NoosferoRoot, SelectElement) {
2 'use strict'; 4 'use strict';
3 5
@@ -221,6 +223,11 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;] @@ -221,6 +223,11 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
221 223
222 select : function (event, selected) { 224 select : function (event, selected) {
223 $("#institution_selected").val(selected.item.id).attr("data-name", selected.item.label); 225 $("#institution_selected").val(selected.item.id).attr("data-name", selected.item.label);
  226 +
  227 + // +("") -> 0; +("1") -> 1...
  228 + if (+($("#institution_selected").val()) !== 0) {
  229 + add_new_institution();
  230 + }
224 } 231 }
225 }); 232 });
226 } 233 }
@@ -234,27 +241,27 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;] @@ -234,27 +241,27 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
234 } 241 }
235 242
236 243
237 - function add_new_institution(evt) {  
238 - evt.preventDefault(); 244 + function add_new_institution() {
239 var selected = $("#institution_selected"); 245 var selected = $("#institution_selected");
240 - var institution_already_added = $(".institutions_added li[data-institution='"+selected.val()+"']").length; 246 + var already_added_to_list = is_institution_already_added_to_list(selected.val());
241 247
242 - if(selected.val().length > 0 && institution_already_added === 0) { 248 + if(selected.val().length > 0 && !already_added_to_list) {
243 //field that send the institutions to the server 249 //field that send the institutions to the server
244 $(".institution_container").append(get_clone_institution_data(selected.val())); 250 $(".institution_container").append(get_clone_institution_data(selected.val()));
245 251
246 // Visualy add the selected institution to the list 252 // Visualy add the selected institution to the list
247 add_selected_institution_to_list(selected.val(), selected.attr("data-name")); 253 add_selected_institution_to_list(selected.val(), selected.attr("data-name"));
248 254
249 - // clean the institution flag  
250 - selected.val("").attr("data-name", "");  
251 - $("#input_institution").val("");  
252 -  
253 $(".remove-institution").click(remove_institution); 255 $(".remove-institution").click(remove_institution);
254 } 256 }
255 } 257 }
256 258
257 259
  260 + function is_institution_already_added_to_list(institution_id) {
  261 + return $(".institutions_added li[data-institution='"+institution_id+"']").length !== 0;
  262 + }
  263 +
  264 +
258 function remove_institution(evt) { 265 function remove_institution(evt) {
259 evt.preventDefault(); 266 evt.preventDefault();
260 var code = $(this).parent().attr("data-institution"); 267 var code = $(this).parent().attr("data-institution");
@@ -369,8 +376,6 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;] @@ -369,8 +376,6 @@ modulejs.define(&#39;CreateInstitution&#39;, [&#39;jquery&#39;, &#39;NoosferoRoot&#39;, &#39;SelectElement&#39;]
369 376
370 $("#community_name").keyup(institution_already_exists); 377 $("#community_name").keyup(institution_already_exists);
371 378
372 - $("#add_new_institution").click(add_new_institution);  
373 -  
374 $(".remove-institution").click(remove_institution); 379 $(".remove-institution").click(remove_institution);
375 380
376 $("#community_country").change(function(){ 381 $("#community_country").change(function(){
src/noosfero-spb/gov_user/public/views/gov-user-comments-extra-fields.js
  1 +/* globals modulejs */
  2 +
1 modulejs.define("GovUserCommentsExtraFields", ['jquery','CreateInstitution'], function($,CreateInstitution) { 3 modulejs.define("GovUserCommentsExtraFields", ['jquery','CreateInstitution'], function($,CreateInstitution) {
2 4
3 function set_events() { 5 function set_events() {
src/noosfero-spb/gov_user/public/views/new-community.js
  1 +/* globals modulejs */
  2 +
1 modulejs.define("NewCommunity", ['jquery'], function($) { 3 modulejs.define("NewCommunity", ['jquery'], function($) {
2 4
3 function replace_mandatory_message() { 5 function replace_mandatory_message() {
@@ -8,7 +10,7 @@ modulejs.define(&quot;NewCommunity&quot;, [&#39;jquery&#39;], function($) { @@ -8,7 +10,7 @@ modulejs.define(&quot;NewCommunity&quot;, [&#39;jquery&#39;], function($) {
8 function remove_image_builder_text() { 10 function remove_image_builder_text() {
9 $("label:contains('Image builder')").hide(); 11 $("label:contains('Image builder')").hide();
10 } 12 }
11 - 13 +
12 function hide_organization_template_fields(){ 14 function hide_organization_template_fields(){
13 $('#template-options').hide(); 15 $('#template-options').hide();
14 } 16 }
src/noosfero-spb/gov_user/public/views/user-edit-profile.js
  1 +/* globals modulejs */
  2 +
1 modulejs.define('UserEditProfile', ['jquery', 'SelectElement', 'SelectFieldChoices', 'CreateInstitution'], function($, SelectElement, SelectFieldChoices, CreateInstitution) { 3 modulejs.define('UserEditProfile', ['jquery', 'SelectElement', 'SelectFieldChoices', 'CreateInstitution'], function($, SelectElement, SelectFieldChoices, CreateInstitution) {
2 'use strict'; 4 'use strict';
3 5
src/noosfero-spb/gov_user/views/person_editor_extras.html.erb
@@ -19,8 +19,7 @@ @@ -19,8 +19,7 @@
19 </div> 19 </div>
20 20
21 <%= content_tag(:div, _("No institution found"), :id=>"institution_empty_ajax_message", :class=>"errorExplanation hide-field") %> 21 <%= content_tag(:div, _("No institution found"), :id=>"institution_empty_ajax_message", :class=>"errorExplanation hide-field") %>
22 - <%= link_to(_("Add new institution"), "#", :class=>'button with-text icon-add', :id => 'add_new_institution') %>  
23 - <%= link_to(_("Create new institution"), "#", :id=>"create_institution_link", :class=>'button with-text icon-add') %> 22 + <%= link_to(_("Create new institution"), "#", :id=>"create_institution_link", :class=>'button with-text icon-add') %>
24 <%= content_tag(:div, "", :id=>"institution_dialog") %> 23 <%= content_tag(:div, "", :id=>"institution_dialog") %>
25 24
26 <%= hidden_field_tag("user[institution_ids][]", "", :class => 'user_institutions') %> 25 <%= hidden_field_tag("user[institution_ids][]", "", :class => 'user_institutions') %>