From f60de0fe2cd9d185c892fe1f0a16542d788f1e48 Mon Sep 17 00:00:00 2001 From: Gabriela Navarro Date: Thu, 9 Apr 2015 18:16:06 -0300 Subject: [PATCH] Add the institution table and references. --- controllers/gov_user_plugin_controller.rb | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ controllers/gov_user_plugin_myprofile_controller.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++ db/migrate/20140528193835_create_institutions_table.rb | 13 +++++++++++++ db/migrate/20140617125143_add_new_fields_institution.rb | 27 +++++++++++++++++++++++++++ db/migrate/20140617132133_create_governmental_spheres.rb | 19 +++++++++++++++++++ db/migrate/20140617132451_create_governmental_powers.rb | 9 +++++++++ db/migrate/20140617134556_add_references_to_institution.rb | 15 +++++++++++++++ db/migrate/20140630183326_add_relation_between_community_and_institution.rb | 13 +++++++++++++ db/migrate/20140812143218_remove_field_role_from_user.rb | 13 +++++++++++++ db/migrate/20140814125947_add_new_fields_to_public_institution.rb | 11 +++++++++++ db/migrate/20140814131606_create_juridical_natures_table.rb | 11 +++++++++++ db/migrate/20140814134827_add_juridical_nature_reference_to_institutions_table.rb | 13 +++++++++++++ db/migrate/20140815194530_register_institution_modification.rb | 13 +++++++++++++ db/migrate/20140818195821_remove_institution_from_user.rb | 9 +++++++++ db/migrate/20140818200738_create_institution_user_relation_table.rb | 12 ++++++++++++ db/migrate/20141103183013_add_corporate_name_to_institution.rb | 9 +++++++++ db/seeds.rb | 19 +++++++++++++++++++ lib/ext/community.rb | 10 ++++++++++ lib/ext/search_controller.rb | 40 ++++++++++++++++++++++++++++++++++++++++ lib/ext/search_helper.rb | 8 ++++++++ lib/gov_user_plugin.rb | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- lib/governmental_power.rb | 13 +++++++++++++ lib/governmental_sphere.rb | 7 +++++++ lib/institution.rb | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/institutions_block.rb | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/institutions_users.rb | 4 ++++ lib/juridical_nature.rb | 15 +++++++++++++++ lib/private_institution.rb | 3 +++ lib/public_institution.rb | 15 +++++++++++++++ test/functional/gov_user_plugin_controller_test.rb | 257 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/functional/gov_user_plugin_myprofile_controller.rb | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/functional/profile_editor_controller_test.rb | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/functional/search_controller_test.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/helpers/institution_test_helper.rb | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/helpers/plugin_test_helper.rb | 40 ++++++++++++++++++++++++++++++++++++++++ views/gov_user_plugin/_institution.html.erb | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ views/gov_user_plugin/create_institution.html.erb | 1 + views/gov_user_plugin/create_institution_admin.html.erb | 1 + views/gov_user_plugin_myprofile/_public_software_info.html.erb | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ views/gov_user_plugin_myprofile/edit_institution.html.erb | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ views/search/institutions.html.erb | 16 ++++++++++++++++ 41 files changed, 1913 insertions(+), 1 deletion(-) create mode 100644 controllers/gov_user_plugin_controller.rb create mode 100644 controllers/gov_user_plugin_myprofile_controller.rb create mode 100644 db/migrate/20140528193835_create_institutions_table.rb create mode 100644 db/migrate/20140617125143_add_new_fields_institution.rb create mode 100644 db/migrate/20140617132133_create_governmental_spheres.rb create mode 100644 db/migrate/20140617132451_create_governmental_powers.rb create mode 100644 db/migrate/20140617134556_add_references_to_institution.rb create mode 100644 db/migrate/20140630183326_add_relation_between_community_and_institution.rb create mode 100644 db/migrate/20140812143218_remove_field_role_from_user.rb create mode 100644 db/migrate/20140814125947_add_new_fields_to_public_institution.rb create mode 100644 db/migrate/20140814131606_create_juridical_natures_table.rb create mode 100644 db/migrate/20140814134827_add_juridical_nature_reference_to_institutions_table.rb create mode 100644 db/migrate/20140815194530_register_institution_modification.rb create mode 100644 db/migrate/20140818195821_remove_institution_from_user.rb create mode 100644 db/migrate/20140818200738_create_institution_user_relation_table.rb create mode 100644 db/migrate/20141103183013_add_corporate_name_to_institution.rb create mode 100644 db/seeds.rb create mode 100644 lib/ext/community.rb create mode 100644 lib/ext/search_controller.rb create mode 100644 lib/ext/search_helper.rb create mode 100644 lib/governmental_power.rb create mode 100644 lib/governmental_sphere.rb create mode 100644 lib/institution.rb create mode 100644 lib/institutions_block.rb create mode 100644 lib/institutions_users.rb create mode 100644 lib/juridical_nature.rb create mode 100644 lib/private_institution.rb create mode 100644 lib/public_institution.rb create mode 100644 test/functional/gov_user_plugin_controller_test.rb create mode 100644 test/functional/gov_user_plugin_myprofile_controller.rb create mode 100644 test/functional/profile_editor_controller_test.rb create mode 100644 test/functional/search_controller_test.rb create mode 100644 test/helpers/institution_test_helper.rb create mode 100644 views/gov_user_plugin/_institution.html.erb create mode 100644 views/gov_user_plugin/create_institution.html.erb create mode 100644 views/gov_user_plugin/create_institution_admin.html.erb create mode 100644 views/gov_user_plugin_myprofile/_public_software_info.html.erb create mode 100644 views/gov_user_plugin_myprofile/edit_institution.html.erb create mode 100644 views/search/institutions.html.erb diff --git a/controllers/gov_user_plugin_controller.rb b/controllers/gov_user_plugin_controller.rb new file mode 100644 index 0000000..dd47862 --- /dev/null +++ b/controllers/gov_user_plugin_controller.rb @@ -0,0 +1,210 @@ +#aqui deve ter so usuario e instituicao +class GovUserPluginController < ApplicationController + + def hide_registration_incomplete_percentage + response = false + + if request.xhr? && params[:hide] + session[:hide_incomplete_percentage] = true + response = session[:hide_incomplete_percentage] + end + + render :json=>response.to_json + end + + def create_institution + @show_sisp_field = environment.admins.include?(current_user.person) + @state_list = get_state_list() + + if request.xhr? + render :layout=>false + else + redirect_to "/" + end + end + + def split_http_referer http_referer + split_list = [] + split_list = http_referer.split("/") + @url_token = split_list.last + return @url_token + end + + def create_institution_admin + @show_sisp_field = environment.admins.include?(current_user.person) + @state_list = get_state_list() + + @url_token = split_http_referer request.original_url() + end + + def new_institution + redirect_to "/" if params[:community].blank? || params[:institutions].blank? + + response_message = {} + + institution_template = Community["institution"] + add_template_in_params institution_template + + institution = private_create_institution + add_environment_admins_to_institution institution + + response_message = save_institution institution + + if request.xhr? #User create institution + render :json => response_message.to_json + else #Admin create institution + session[:notice] = response_message[:message] # consume the notice + + redirect_depending_on_institution_creation response_message + end + end + + def institution_already_exists + redirect_to "/" if !request.xhr? || params[:name].blank? + + already_exists = !Community.where(:name=>params[:name]).empty? + + render :json=>already_exists.to_json + end + + def get_institutions + redirect_to "/" if !request.xhr? || params[:query].blank? + + list = Institution.search_institution(params[:query]).map{ |institution| + {:value=>institution.name, :id=>institution.id} + } + + render :json => list.to_json + end + + def get_brazil_states + redirect_to "/" unless request.xhr? + + state_list = get_state_list() + render :json=>state_list.collect {|state| state.name }.to_json + end + + def get_field_data + condition = !request.xhr? || params[:query].nil? || params[:field].nil? + return render :json=>{} if condition + + model = get_model_by_params_field + + data = model.where("name ILIKE ?", "%#{params[:query]}%").select("id, name") + .collect { |db| + {:id=>db.id, :label=>db.name} + } + + other = [model.select("id, name").last].collect { |db| + {:id=>db.id, :label=>db.name} + } + + # Always has other in the list + data |= other + + render :json=> data + end + + protected + + def get_state_list + NationalRegion.find( + :all, + :conditions=>["national_region_type_id = ?", 2], + :order=>"name" + ) + end + + def set_institution_type + institution_params = params[:institutions].except( + :governmental_power, + :governmental_sphere, + :juridical_nature + ) + if params[:institutions][:type] == "PublicInstitution" + PublicInstitution::new institution_params + else + PrivateInstitution::new institution_params + end + end + + def set_public_institution_fields institution + inst_fields = params[:institutions] + + begin + gov_power = GovernmentalPower.find inst_fields[:governmental_power] + gov_sphere = GovernmentalSphere.find inst_fields[:governmental_sphere] + jur_nature = JuridicalNature.find inst_fields[:juridical_nature] + + institution.juridical_nature = jur_nature + institution.governmental_power = gov_power + institution.governmental_sphere = gov_sphere + rescue + institution.errors.add( + :governmental_fields, + _("Could not find Governmental Power or Governmental Sphere") + ) + end + end + + def private_create_institution + community = Community.new(params[:community]) + community.environment = environment + institution = set_institution_type + + institution.name = community[:name] + institution.community = community + + if institution.type == "PublicInstitution" + set_public_institution_fields institution + end + + institution.date_modification = DateTime.now + institution.save + + institution + end + + def add_template_in_params institution_template + com_fields = params[:community] + if !institution_template.blank? && institution_template.is_template + com_fields[:template_id]= institution_template.id unless com_fields.blank? + end + end + + def add_environment_admins_to_institution institution + edit_page = params[:edit_institution_page] == false + if environment.admins.include?(current_user.person) && edit_page + environment.admins.each do |adm| + institution.community.add_admin(adm) + end + end + end + + def save_institution institution + inst_errors = institution.errors.full_messages + com_errors = institution.community.errors.full_messages + + if inst_errors.empty? && com_errors.empty? && institution.valid? && institution.save + { :success => true, + :message => _("Institution successful created!"), + :institution_data => {:name=>institution.name, :id=>institution.id} + } + else + { :success => false, + :message => _("Institution could not be created!"), + :errors => inst_errors << com_errors + } + end + end + + def redirect_depending_on_institution_creation response_message + if response_message[:success] + redirect_to :controller => "/admin_panel", :action => "index" + else + flash[:errors] = response_message[:errors] + redirect_to :controller => "software_communities_plugin", :action => "create_institution_admin" + end + end + +end diff --git a/controllers/gov_user_plugin_myprofile_controller.rb b/controllers/gov_user_plugin_myprofile_controller.rb new file mode 100644 index 0000000..4d14d3d --- /dev/null +++ b/controllers/gov_user_plugin_myprofile_controller.rb @@ -0,0 +1,44 @@ +class GovUserPluginMyprofileController < MyProfileController + append_view_path File.join(File.dirname(__FILE__) + '/../views') + + def index + end + + def edit_institution + @show_sisp_field = environment.admins.include?(current_user.person) + @state_list = NationalRegion.find(:all, :conditions => + { :national_region_type_id => 2 }, + :order => 'name') + @institution = @profile.institution + update_institution if request.post? + end + + private + + def update_institution + @institution.community.update_attributes(params[:community]) + @institution.update_attributes(params[:institutions].except(:governmental_power, :governmental_sphere, :juridical_nature)) + if @institution.type == "PublicInstitution" + begin + governmental_updates + rescue + @institution.errors.add(:governmental_fields, + _("Could not find Governmental Power or Governmental Sphere")) + end + end + flash[:errors] = @institution.errors.full_messages unless @institution.valid? + end + + def governmental_updates + gov_power = GovernmentalPower.find params[:institutions][:governmental_power] + gov_sphere = GovernmentalSphere.find params[:institutions][:governmental_sphere] + jur_nature = JuridicalNature.find params[:institutions][:juridical_nature] + + @institution.juridical_nature = jur_nature + @institution.governmental_power = gov_power + @institution.governmental_sphere = gov_sphere + @institution.save + end + + +end diff --git a/db/migrate/20140528193835_create_institutions_table.rb b/db/migrate/20140528193835_create_institutions_table.rb new file mode 100644 index 0000000..294c791 --- /dev/null +++ b/db/migrate/20140528193835_create_institutions_table.rb @@ -0,0 +1,13 @@ +class CreateInstitutionsTable < ActiveRecord::Migration + def self.up + create_table :institutions do |t| + t.string :name + + t.timestamps + end + end + + def self.down + drop_table :institutions + end +end diff --git a/db/migrate/20140617125143_add_new_fields_institution.rb b/db/migrate/20140617125143_add_new_fields_institution.rb new file mode 100644 index 0000000..70ecf88 --- /dev/null +++ b/db/migrate/20140617125143_add_new_fields_institution.rb @@ -0,0 +1,27 @@ +class AddNewFieldsInstitution < ActiveRecord::Migration + def up + add_column :institutions, :acronym, :string + add_column :institutions, :unit_code, :integer + add_column :institutions, :parent_code, :integer + add_column :institutions, :unit_type, :string + add_column :institutions, :juridical_nature, :string + add_column :institutions, :sub_juridical_nature, :string + add_column :institutions, :normalization_level, :string + add_column :institutions, :version, :string + add_column :institutions, :cnpj, :string + add_column :institutions, :type, :string + end + + def down + remove_column :institutions, :acronym + remove_column :institutions, :unit_code + remove_column :institutions, :parent_code + remove_column :institutions, :unit_type + remove_column :institutions, :juridical_nature + remove_column :institutions, :sub_juridical_nature + remove_column :institutions, :normalization_level + remove_column :institutions, :version + remove_column :institutions, :cnpj + remove_column :institutions, :type + end +end diff --git a/db/migrate/20140617132133_create_governmental_spheres.rb b/db/migrate/20140617132133_create_governmental_spheres.rb new file mode 100644 index 0000000..22c1fc7 --- /dev/null +++ b/db/migrate/20140617132133_create_governmental_spheres.rb @@ -0,0 +1,19 @@ +class CreateGovernmentalSpheres < ActiveRecord::Migration + def change + create_table :governmental_spheres do |t| + t.string :name + t.string :acronym + t.integer :unit_code + t.integer :parent_code + t.string :unit_type + t.string :juridical_nature + t.string :sub_juridical_nature + t.string :normalization_level + t.string :version + t.string :cnpj + t.string :type + + t.timestamps + end + end +end diff --git a/db/migrate/20140617132451_create_governmental_powers.rb b/db/migrate/20140617132451_create_governmental_powers.rb new file mode 100644 index 0000000..ce88ee7 --- /dev/null +++ b/db/migrate/20140617132451_create_governmental_powers.rb @@ -0,0 +1,9 @@ +class CreateGovernmentalPowers < ActiveRecord::Migration + def change + create_table :governmental_powers do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20140617134556_add_references_to_institution.rb b/db/migrate/20140617134556_add_references_to_institution.rb new file mode 100644 index 0000000..e5203e1 --- /dev/null +++ b/db/migrate/20140617134556_add_references_to_institution.rb @@ -0,0 +1,15 @@ +class AddReferencesToInstitution < ActiveRecord::Migration + def up + change_table :institutions do |t| + t.references :governmental_power + t.references :governmental_sphere + end + end + + def down + change_table :institutions do |t| + t.remove_references :governmental_power + t.remove_references :governmental_sphere + end + end +end diff --git a/db/migrate/20140630183326_add_relation_between_community_and_institution.rb b/db/migrate/20140630183326_add_relation_between_community_and_institution.rb new file mode 100644 index 0000000..4dde5a9 --- /dev/null +++ b/db/migrate/20140630183326_add_relation_between_community_and_institution.rb @@ -0,0 +1,13 @@ +class AddRelationBetweenCommunityAndInstitution < ActiveRecord::Migration + def up + change_table :institutions do |t| + t.references :community + end + end + + def down + change_table :institutions do |t| + t.remove_references :community + end + end +end diff --git a/db/migrate/20140812143218_remove_field_role_from_user.rb b/db/migrate/20140812143218_remove_field_role_from_user.rb new file mode 100644 index 0000000..e4d4fc6 --- /dev/null +++ b/db/migrate/20140812143218_remove_field_role_from_user.rb @@ -0,0 +1,13 @@ +class RemoveFieldRoleFromUser < ActiveRecord::Migration + def up + change_table :users do |t| + t.remove :role + end + end + + def down + change_table :users do |t| + t.string :role + end + end +end diff --git a/db/migrate/20140814125947_add_new_fields_to_public_institution.rb b/db/migrate/20140814125947_add_new_fields_to_public_institution.rb new file mode 100644 index 0000000..232f50a --- /dev/null +++ b/db/migrate/20140814125947_add_new_fields_to_public_institution.rb @@ -0,0 +1,11 @@ +class AddNewFieldsToPublicInstitution < ActiveRecord::Migration + def up + add_column :institutions, :sisp, :boolean, :default => false + remove_column :institutions, :juridical_nature + end + + def down + remove_column :institutions, :sisp + add_column :institutions, :juridical_nature, :string + end +end diff --git a/db/migrate/20140814131606_create_juridical_natures_table.rb b/db/migrate/20140814131606_create_juridical_natures_table.rb new file mode 100644 index 0000000..f2c9c18 --- /dev/null +++ b/db/migrate/20140814131606_create_juridical_natures_table.rb @@ -0,0 +1,11 @@ +class CreateJuridicalNaturesTable < ActiveRecord::Migration + def up + create_table :juridical_natures do |t| + t.string :name + end + end + + def down + drop_table :juridical_natures + end +end diff --git a/db/migrate/20140814134827_add_juridical_nature_reference_to_institutions_table.rb b/db/migrate/20140814134827_add_juridical_nature_reference_to_institutions_table.rb new file mode 100644 index 0000000..03baff8 --- /dev/null +++ b/db/migrate/20140814134827_add_juridical_nature_reference_to_institutions_table.rb @@ -0,0 +1,13 @@ +class AddJuridicalNatureReferenceToInstitutionsTable < ActiveRecord::Migration + def up + change_table :institutions do |t| + t.references :juridical_nature + end + end + + def down + change_table :institutions do |t| + t.remove_references :juridical_nature + end + end +end diff --git a/db/migrate/20140815194530_register_institution_modification.rb b/db/migrate/20140815194530_register_institution_modification.rb new file mode 100644 index 0000000..3183ec0 --- /dev/null +++ b/db/migrate/20140815194530_register_institution_modification.rb @@ -0,0 +1,13 @@ +class RegisterInstitutionModification < ActiveRecord::Migration + def up + change_table :institutions do |t| + t.string :date_modification + end + end + + def down + change_table :institutions do |t| + t.remove :date_modification + end + end +end diff --git a/db/migrate/20140818195821_remove_institution_from_user.rb b/db/migrate/20140818195821_remove_institution_from_user.rb new file mode 100644 index 0000000..37486cd --- /dev/null +++ b/db/migrate/20140818195821_remove_institution_from_user.rb @@ -0,0 +1,9 @@ +class RemoveInstitutionFromUser < ActiveRecord::Migration + def up + remove_column :users, :institution_id + end + + def down + add_column :users, :institution_id + end +end diff --git a/db/migrate/20140818200738_create_institution_user_relation_table.rb b/db/migrate/20140818200738_create_institution_user_relation_table.rb new file mode 100644 index 0000000..7068d72 --- /dev/null +++ b/db/migrate/20140818200738_create_institution_user_relation_table.rb @@ -0,0 +1,12 @@ +class CreateInstitutionUserRelationTable < ActiveRecord::Migration + def up + create_table :institutions_users do |t| + t.belongs_to :user + t.belongs_to :institution + end + end + + def down + drop_table :institutions_users + end +end diff --git a/db/migrate/20141103183013_add_corporate_name_to_institution.rb b/db/migrate/20141103183013_add_corporate_name_to_institution.rb new file mode 100644 index 0000000..69d4a19 --- /dev/null +++ b/db/migrate/20141103183013_add_corporate_name_to_institution.rb @@ -0,0 +1,9 @@ +class AddCorporateNameToInstitution < ActiveRecord::Migration + def up + add_column :institutions, :corporate_name, :string + end + + def down + remove_column :institutions, :corporate_name + end +end diff --git a/db/seeds.rb b/db/seeds.rb new file mode 100644 index 0000000..cd02d6b --- /dev/null +++ b/db/seeds.rb @@ -0,0 +1,19 @@ +# encoding: UTF-8 +powers = ["Executivo", "Legislativo", "Judiciario", "Não se aplica"] +spheres = ["Federal", "Estadual", "Distrital", "Municipal"] +jur_natures = ["Administracao Direta", "Autarquia", "Empresa Publica", "Fundacao", + "Orgao Autonomo", "Sociedade", "Sociedade Civil", + "Sociedade de Economia Mista" + ] + +powers.each do |power| + GovernmentalPower.create(:name => power) +end + +spheres.each do |sphere| + GovernmentalSphere.create(:name => sphere) +end + +jur_natures.each do |jur_nature| + JuridicalNature.create(:name => jur_nature) +end diff --git a/lib/ext/community.rb b/lib/ext/community.rb new file mode 100644 index 0000000..c53fd84 --- /dev/null +++ b/lib/ext/community.rb @@ -0,0 +1,10 @@ +require_dependency 'community' + +class Community + has_one :institution, :dependent=>:destroy + + + def institution? + return !institution.nil? + end +end diff --git a/lib/ext/search_controller.rb b/lib/ext/search_controller.rb new file mode 100644 index 0000000..1ca78b0 --- /dev/null +++ b/lib/ext/search_controller.rb @@ -0,0 +1,40 @@ +require_dependency 'search_controller' + +class SearchController + + def communities + results = filter_communities_list do |community| + !community.institution? + end + results = results.paginate(:per_page => 24, :page => params[:page]) + @searches[@asset] = {:results => results} + @search = results + end + + def institutions + @titles[:institutions] = _("Institution Catalog") + results = filter_communities_list{|community| community.institution?} + results = results.paginate(:per_page => 24, :page => params[:page]) + @searches[@asset] = {:results => results} + @search = results + end + + def filter_communities_list + unfiltered_list = visible_profiles(Community) + + unless params[:query].nil? + unfiltered_list = unfiltered_list.select do |com| + com.name.downcase =~ /#{params[:query].downcase}/ + end + end + + communities_list = [] + unfiltered_list.each do |profile| + if profile.class == Community && !profile.is_template? && yield(profile) + communities_list << profile + end + end + + communities_list + end +end diff --git a/lib/ext/search_helper.rb b/lib/ext/search_helper.rb new file mode 100644 index 0000000..aae5940 --- /dev/null +++ b/lib/ext/search_helper.rb @@ -0,0 +1,8 @@ +require_dependency 'search_helper' + +module SearchHelper + + COMMON_PROFILE_LIST_BLOCK ||= [] + COMMON_PROFILE_LIST_BLOCK << :institutions + +end diff --git a/lib/gov_user_plugin.rb b/lib/gov_user_plugin.rb index f3f7a2f..d6e5302 100644 --- a/lib/gov_user_plugin.rb +++ b/lib/gov_user_plugin.rb @@ -37,7 +37,7 @@ class GovUserPlugin < Noosfero::Plugin def profile_editor_transaction_extras single_hash_transactions = { :user => 'user', - :instituton => 'instituton' + :instituton => 'instituton' } single_hash_transactions.each do |model, transaction| @@ -45,6 +45,61 @@ class GovUserPlugin < Noosfero::Plugin end end + def profile_editor_controller_filters + block = proc do + if request.post? && params[:institution] + is_admin = environment.admins.include?(current_user.person) + + unless is_admin + institution = profile.user.institutions + + if !params[:institution].blank? && !params[:institution][:sisp].nil? + if params[:institution][:sisp] != institution.sisp + params[:institution][:sisp] = institution.sisp + end + end + end + end + end + + [{ + :type => 'before_filter', + :method_name => 'validate_institution_sisp_field_access', + :options => { :only => :edit }, + :block => block + }] + end + + def profile_tabs + if context.profile.community? + return profile_tabs_institution if context.profile.institution? + end + end + + def control_panel_buttons + if context.profile.institution? + return institution_info_button + end + end + + def self.extra_blocks + { + InstitutionsBlock => { :type => [Environment, Person] } + } + end + + def custom_user_registration_attributes(user) + return if context.params[:user][:institution_ids].nil? + context.params[:user][:institution_ids].delete('') + + update_user_institutions(user) + + user.institutions.each do |institution| + community = institution.community + community.add_member user.person + end + end + def profile_editor_extras profile = context.profile @@ -108,6 +163,9 @@ class GovUserPlugin < Noosfero::Plugin empty_fields end + + protected + def user_transaction user_editor_institution_actions @@ -116,10 +174,95 @@ class GovUserPlugin < Noosfero::Plugin end end + def institution_transaction + institution.date_modification = DateTime.now + institution.save + institution_models = %w(governmental_power governmental_sphere + juridical_nature) + + institution_models.each do |model| + call_institution_transaction(model) + end + + if context.params.has_key?(:institution) + Institution.transaction do + context.profile. + institution. + update_attributes!(context.params[:institution]) + end + end + end + private def call_model_transaction(model,name) send(name + '_transaction') if context.params.key?(model.to_sym) end + # Add and remove the user from it's institutions communities + def user_editor_institution_actions + user = context.profile.user + + old_communities = [] + context.profile.user.institutions.each do |institution| + old_communities << institution.community + end + + new_communities = [] + unless context.params[:user][:institution_ids].nil? + context.params[:user][:institution_ids].delete('') + + context.params[:user][:institution_ids].each do |id| + new_communities << Institution.find(id).community + end + end + + manage_user_institutions(user, old_communities, new_communities) + end + + def institution_info_button + { + :title => _('Institution Info'), + :icon => 'edit-profile-group control-panel-instituton-link', + :url => { + :controller => 'software_communities_plugin_myprofile', + :action => 'edit_institution' + } + } + end + + def manage_user_institutions(user, old_communities, new_communities) + leave_communities = (old_communities - new_communities) + enter_communities = (new_communities - old_communities) + + leave_communities.each do |community| + community.remove_member(user.person) + user.institutions.delete(community.institution) + end + + enter_communities.each do |community| + community.add_member(user.person) + user.institutions << community.institution + end + end + + def profile_tabs_institution + { :title => _('Institution'), + :id => 'intitution-fields', + :content => Proc::new do render :partial => 'profile/institution_tab' end, + :start => true + } + end + + def update_user_institutions(user) + context.params[:user][:institution_ids].each do |institution_id| + institution = Institution.find institution_id + user.institutions << institution + + if institution.community.admins.blank? + institution.community.add_admin(user.person) + end + end + user.save unless user.institution_ids.empty? + end end diff --git a/lib/governmental_power.rb b/lib/governmental_power.rb new file mode 100644 index 0000000..cb7ca36 --- /dev/null +++ b/lib/governmental_power.rb @@ -0,0 +1,13 @@ +class GovernmentalPower < ActiveRecord::Base + attr_accessible :name + + validates :name, :presence=>true, :uniqueness=>true + has_many :institutions + + def public_institutions + Institution.where( + :type=>"PublicInstitution", + :governmental_power_id=>self.id + ) + end +end diff --git a/lib/governmental_sphere.rb b/lib/governmental_sphere.rb new file mode 100644 index 0000000..aef8f8f --- /dev/null +++ b/lib/governmental_sphere.rb @@ -0,0 +1,7 @@ +class GovernmentalSphere < ActiveRecord::Base + attr_accessible :name + + validates :name, :presence=>true, :uniqueness=>true + + has_many :institutions +end diff --git a/lib/institution.rb b/lib/institution.rb new file mode 100644 index 0000000..cfcb317 --- /dev/null +++ b/lib/institution.rb @@ -0,0 +1,122 @@ +class Institution < ActiveRecord::Base + + SEARCH_FILTERS = { + :order => %w[], + :display => %w[compact] + } + + def self.default_search_display + 'compact' + end + + belongs_to :governmental_power + belongs_to :governmental_sphere + belongs_to :juridical_nature + + has_and_belongs_to_many :users + + attr_accessible :name, :acronym, :unit_code, :parent_code, :unit_type, + :sub_juridical_nature, :normalization_level, + :version, :cnpj, :type, :governmental_power, + :governmental_sphere, :sisp, :juridical_nature, + :corporate_name + + validates :name, :presence=>true, :uniqueness=>true + + validates :corporate_name, :presence => true + + before_save :verify_institution_type + + belongs_to :community + + scope :search_institution, lambda{ |value| + where("name ilike ? OR acronym ilike ?", "%#{value}%", "%#{value}%" ) + } + + validate :validate_country, :validate_state, :validate_city, + :verify_institution_type, :validate_cnpj, :validate_format_cnpj + + + protected + + def verify_institution_type + valid_institutions_type = ["PublicInstitution", "PrivateInstitution"] + + unless valid_institutions_type.include? self.type + self.errors.add( + :type, + _("invalid, only public and private institutions are allowed.") + ) + + return false + end + return true + end + + def validate_country + if(self.community.blank? || + self.community.country.blank? && + self.errors[:country].blank?) + + self.errors.add(:country, _("can't be blank")) + return false + end + return true + end + + def validate_state + if(self.community.blank? || + self.errors[:state].blank? && + self.community.state.blank?) + + if self.community.country == "BR" + self.errors.add(:state, _("can't be blank")) + return false + else + return true + end + end + return true + end + + def validate_city + if(self.community.blank? || + self.errors[:city].blank? && + self.community.city.blank?) + + if self.community.country == "BR" + self.errors.add(:city, _("can't be blank")) + return false + else + return true + end + end + return true + end + + def validate_format_cnpj + return true if !self.community.blank? && self.community.country != "BR" + + format = /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/ + + if !self.cnpj.blank? && format.match(self.cnpj) + return true + else + self.errors.add(:cnpj, _("invalid format")) + return false + end + end + + def validate_cnpj + if !self.community.blank? && self.community.country == "BR" + if self.errors[:cnpj].blank? && self.cnpj.blank? + self.errors.add(:cnpj, _("can't be blank")) + return false + else + return true + end + else + return true + end + end +end diff --git a/lib/institutions_block.rb b/lib/institutions_block.rb new file mode 100644 index 0000000..8ef8f59 --- /dev/null +++ b/lib/institutions_block.rb @@ -0,0 +1,71 @@ +class InstitutionsBlock < CommunitiesBlock + + def self.description + _('Institutions') + end + + def profile_count + profile_list.count + end + + def default_title + n_('{#} institution', '{#} institutions', profile_count) + end + + def help + _('This block displays the institutions in which the user is a member.') + end + + def footer + owner = self.owner + case owner + when Profile + lambda do |context| + link_to s_('institutions|View all'), :profile => owner.identifier, + :controller => 'profile', :action => 'communities', + :type => 'Institution' + end + when Environment + lambda do |context| + link_to s_('institutions|View all'), :controller => 'search', + :action => 'communities', :type => 'Institution' + end + else + '' + end + end + + def profile_list + result = get_visible_profiles + + result = result.select { |p| p.class == Community && p.institution? } + + result.slice(0..get_limit-1) + end + + def profiles + owner.communities + end + + private + + def get_visible_profiles + include_list = [:image,:domains,:preferred_domain,:environment] + visible_profiles = profiles.visible.includes(include_list) + + if !prioritize_profiles_with_image + visible_profiles.all(:limit => get_limit, + :order => 'profiles.updated_at DESC' + ).sort_by{ rand } + elsif profiles.visible.with_image.count >= get_limit + visible_profiles.with_image.all(:limit => get_limit * 5, + :order => 'profiles.updated_at DESC' + ).sort_by{ rand } + else + visible_profiles.with_image.sort_by{ rand } + + visible_profiles.without_image.all(:limit => get_limit * 5, + :order => 'profiles.updated_at DESC' + ).sort_by{ rand } + end + end +end diff --git a/lib/institutions_users.rb b/lib/institutions_users.rb new file mode 100644 index 0000000..e47da86 --- /dev/null +++ b/lib/institutions_users.rb @@ -0,0 +1,4 @@ +class InstitutionUser < ActiveRecord::Base + belongs_to :user + belongs_to :institution +end diff --git a/lib/juridical_nature.rb b/lib/juridical_nature.rb new file mode 100644 index 0000000..1bb17f8 --- /dev/null +++ b/lib/juridical_nature.rb @@ -0,0 +1,15 @@ +class JuridicalNature < ActiveRecord::Base + attr_accessible :name + + has_many :institutions + + validates_presence_of :name + validates_uniqueness_of :name + + def public_institutions + Institution.where( + :type=>"PublicInstitution", + :juridical_nature_id=>self.id + ) + end +end diff --git a/lib/private_institution.rb b/lib/private_institution.rb new file mode 100644 index 0000000..1ee0f70 --- /dev/null +++ b/lib/private_institution.rb @@ -0,0 +1,3 @@ +class PrivateInstitution < Institution + validates :cnpj, :uniqueness=>true, :allow_nil=>true, :allow_blank=>true +end diff --git a/lib/public_institution.rb b/lib/public_institution.rb new file mode 100644 index 0000000..84ade57 --- /dev/null +++ b/lib/public_institution.rb @@ -0,0 +1,15 @@ +class PublicInstitution < Institution + validates :governmental_power, :governmental_sphere, :juridical_nature, + :presence=>true + + validates :acronym, :allow_blank => true, :allow_nil => true, + :uniqueness=>true + + validates :cnpj, :uniqueness=>true + + validates_format_of( + :cnpj, + :with => /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/, + :allow_nil => true, :allow_blank => true + ) +end diff --git a/test/functional/gov_user_plugin_controller_test.rb b/test/functional/gov_user_plugin_controller_test.rb new file mode 100644 index 0000000..60a4066 --- /dev/null +++ b/test/functional/gov_user_plugin_controller_test.rb @@ -0,0 +1,257 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../helpers/institution_test_helper' +require File.dirname(__FILE__) + '/../../controllers/gov_user_plugin_controller' + +class GovUserPluginController; def rescue_action(e) raise e end; end +class GovUserPluginControllerTest < ActionController::TestCase + + + def setup + @admin = create_user("adminuser").person + @admin.stubs(:has_permission?).returns("true") + @controller.stubs(:current_user).returns(@admin.user) + + @environment = Environment.default + @environment.enabled_plugins = ['SoftwareCommunitiesPlugin'] + @environment.add_admin(@admin) + @environment.save + + @gov_power = GovernmentalPower.create(:name=>"Some Gov Power") + @gov_sphere = GovernmentalSphere.create(:name=>"Some Gov Sphere") + @juridical_nature = JuridicalNature.create(:name => "Autarquia") + @response = ActionController::TestResponse.new + + @institution_list = [] + @institution_list << InstitutionTestHelper.create_public_institution( + "Ministerio Publico da Uniao", + "MPU", + "BR", + "DF", + "Gama", + @juridical_nature, + @gov_power, + @gov_sphere, + "12.345.678/9012-45" + ) + @institution_list << InstitutionTestHelper.create_public_institution( + "Tribunal Regional da Uniao", + "TRU", + "BR", + "DF", + "Brasilia", + @juridical_nature, + @gov_power, + @gov_sphere, + "12.345.678/9012-90" + ) + end + + should "Search for institution with acronym" do + xhr :get, :get_institutions, :query=>"TRU" + + json_response = ActiveSupport::JSON.decode(@response.body) + + assert_equal "Tribunal Regional da Uniao", json_response[0]["value"] + end + + should "Search for institution with name" do + xhr :get, :get_institutions, :query=>"Minis" + + json_response = ActiveSupport::JSON.decode(@response.body) + + assert_equal "Ministerio Publico da Uniao", json_response[0]["value"] + end + + should "search with name or acronym and return a list with institutions" do + xhr :get, :get_institutions, :query=>"uni" + + json_response = ActiveSupport::JSON.decode(@response.body) + + assert_equal "Ministerio Publico da Uniao", json_response[0]["value"] + assert_equal "Tribunal Regional da Uniao", json_response[1]["value"] + end + + should "method create_institution return the html for modal" do + @controller.stubs(:current_user).returns(@admin.user) + xhr :get, :create_institution + assert_template 'create_institution' + end + + should "create new institution with ajax without acronym" do + @controller.stubs(:verify_recaptcha).returns(true) + + fields = InstitutionTestHelper.generate_form_fields( + "foo bar", + "BR", + "DF", + "Brasilia", + "12.234.567/8900-10", + "PublicInstitution" + ) + fields[:institutions][:governmental_power] = @gov_power.id + fields[:institutions][:governmental_sphere] = @gov_sphere.id + fields[:institutions][:juridical_nature] = @juridical_nature.id + + xhr :get, :new_institution, fields + + json_response = ActiveSupport::JSON.decode(@response.body) + + assert json_response["success"] + end + + should "not create a institution that already exists" do + @controller.stubs(:verify_recaptcha).returns(true) + + fields = InstitutionTestHelper.generate_form_fields( + "Ministerio Publico da Uniao", + "BR", + "DF", + "Brasilia", + "12.234.567/8900-10", + "PublicInstitution" + ) + fields[:institutions][:governmental_power] = @gov_power.id + fields[:institutions][:governmental_sphere] = @gov_sphere.id + fields[:institutions][:juridical_nature] = @juridical_nature.id + + xhr :get, :new_institution, fields + + json_response = ActiveSupport::JSON.decode(@response.body) + + assert !json_response["success"] + end + + should "not create a institution without cnpj" do + @controller.stubs(:verify_recaptcha).returns(true) + + fields = InstitutionTestHelper.generate_form_fields( + "Some Private Institution", + "BR", + "DF", + "Brasilia", + "", + "PrivateInstitution" + ) + fields[:institutions][:acronym] = "SPI" + + xhr :get, :new_institution, fields + + json_response = ActiveSupport::JSON.decode(@response.body) + + assert !json_response["success"] + end + + should "verify if institution name already exists" do + xhr :get, :institution_already_exists, :name=>"Ministerio Publico da Uniao" + assert_equal "true", @response.body + + xhr :get, :institution_already_exists, :name=>"Another name here" + assert_equal "false", @response.body + end + + should "hide registration incomplete message" do + xhr :get, :hide_registration_incomplete_percentage, :hide=>true + assert_equal "true", @response.body + end + + should "not hide registration incomplete message" do + xhr :get, :hide_registration_incomplete_percentage, :hide=>false + assert_equal "false", @response.body + end + + should "Create new institution with method post" do + @controller.stubs(:verify_recaptcha).returns(true) + + fields = InstitutionTestHelper.generate_form_fields( + "Some Private Institution", + "BR", + "DF", + "Brasilia", + "12.345.567/8900-10", + "PrivateInstitution" + ) + fields[:institutions][:acronym] = "SPI" + + post :new_institution, fields + + assert_redirected_to(controller: "admin_panel", action: "index") + end + + should "not create new institution with method post without cnpj" do + @controller.stubs(:verify_recaptcha).returns(true) + + fields = InstitutionTestHelper.generate_form_fields( + "Some Private Institution", + "BR", + "DF", + "Brasilia", + "", + "PrivateInstitution" + ) + fields[:institutions][:acronym] = "SPI" + + post :new_institution, fields + + assert_redirected_to(controller: "software_communities_plugin", action: "create_institution_admin") + end + + should "Create foreign institution without city, state and cnpj by post" do + @controller.stubs(:verify_recaptcha).returns(true) + + fields = InstitutionTestHelper.generate_form_fields( + "Foreign institution", + "AZ", + "", + "", + "", + "PrivateInstitution" + ) + fields[:institutions][:acronym] = "FI" + + post :new_institution, fields + + assert_redirected_to(controller: "admin_panel", action: "index") + end + + should "Create foreign institution without city, state and cnpj by ajax" do + @controller.stubs(:verify_recaptcha).returns(true) + + fields = InstitutionTestHelper.generate_form_fields( + "Foreign institution", + "AZ", + "", + "", + "", + "PrivateInstitution" + ) + fields[:institutions][:acronym] = "FI" + + xhr :post, :new_institution, fields + + json_response = ActiveSupport::JSON.decode(@response.body) + assert json_response["success"] + end + + should "add environment admins to institution when created via admin panel" do + @controller.stubs(:verify_recaptcha).returns(true) + admin2 = create_user("another_admin").person + admin2.stubs(:has_permission?).returns("true") + @environment.add_admin(admin2) + @environment.save + + fields = InstitutionTestHelper.generate_form_fields( + "Private Institution", + "BR", + "DF", + "Brasilia", + "12.323.557/8900-10", + "PrivateInstitution" + ) + fields[:institutions][:acronym] = "PI" + fields[:edit_institution_page] = false + post :new_institution, fields + + assert(Institution.last.community.admins.include?(admin2) ) + end + +end diff --git a/test/functional/gov_user_plugin_myprofile_controller.rb b/test/functional/gov_user_plugin_myprofile_controller.rb new file mode 100644 index 0000000..61c48b1 --- /dev/null +++ b/test/functional/gov_user_plugin_myprofile_controller.rb @@ -0,0 +1,105 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../helpers/institution_test_helper' +require( +File.dirname(__FILE__) + +'/../../controllers/gov_user_plugin_myprofile_controller' +) + +class GovUserPluginMyprofileController; def rescue_action(e) raise e end; +end + +class GovUserPluginMyprofileControllerTest < ActionController::TestCase + def setup + @controller = GovUserPluginMyprofileController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @person = create_user('person').person + @offer = create_user('Angela Silva') + @offer_1 = create_user('Ana de Souza') + @offer_2 = create_user('Angelo Roberto') + + login_as(@person.user_login) + @environment = Environment.default + @environment.enable_plugin('GovUserPlugin') + @environment.save! + end + should "user edit its community institution" do + govPower = GovernmentalPower.create(:name=>"Some Gov Power") + govSphere = GovernmentalSphere.create(:name=>"Some Gov Sphere") + juridical_nature = JuridicalNature.create(:name => "Autarquia") + + institution = InstitutionTestHelper.create_public_institution( + "Ministerio Publico da Uniao", + "MPU", + "BR", + "DF", + "Gama", + juridical_nature, + govPower, + govSphere, + "12.345.678/9012-45" + ) + + identifier = institution.community.identifier + + fields = InstitutionTestHelper.generate_form_fields( + "institution new name", + "BR", + "DF", + "Gama", + "12.345.678/9012-45", + "PrivateInstitution" + ) + + post( + :edit_institution, + :profile=>institution.community.identifier, + :community=>fields[:community], + :institutions=>fields[:institutions] + ) + + institution = Community[identifier].institution + assert_not_equal "Ministerio Publico da Uniao", institution.community.name + end + + should "not user edit its community institution with wrong values" do + govPower = GovernmentalPower.create(:name=>"Some Gov Power") + govSphere = GovernmentalSphere.create(:name=>"Some Gov Sphere") + juridical_nature = JuridicalNature.create(:name => "Autarquia") + + institution = InstitutionTestHelper.create_public_institution( + "Ministerio Publico da Uniao", + "MPU", + "BR", + "DF", + "Gama", + juridical_nature, + govPower, + govSphere, + "12.345.678/9012-45" + ) + + identifier = institution.community.identifier + + fields = InstitutionTestHelper.generate_form_fields( + "", + "BR", + "DF", + "Gama", + "6465465465", + "PrivateInstitution" + ) + + post( + :edit_institution, + :profile=>institution.community.identifier, + :community=>fields[:community], + :institutions=>fields[:institutions] + ) + + institution = Community[identifier].institution + assert_equal "Ministerio Publico da Uniao", institution.community.name + assert_equal "12.345.678/9012-45", institution.cnpj + end + +end diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb new file mode 100644 index 0000000..52de533 --- /dev/null +++ b/test/functional/profile_editor_controller_test.rb @@ -0,0 +1,112 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../helpers/institution_test_helper' +require( +File.dirname(__FILE__) + +'/../../../../app/controllers/my_profile/profile_editor_controller' +) + +class ProfileEditorController; def rescue_action(e) raise e end; end + +class ProfileEditorControllerTest < ActionController::TestCase + + def setup + @controller = ProfileEditorController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @profile = create_user('default_user').person + + Environment.default.affiliate( + @profile, + [Environment::Roles.admin(Environment.default.id)] + + Profile::Roles.all_roles(Environment.default.id) + ) + + @environment = Environment.default + @environment.enabled_plugins = ['GovUserPlugin'] + admin = create_user("adminuser").person + admin.stubs(:has_permission?).returns("true") + login_as('adminuser') + @environment.add_admin(admin) + @environment.save + + @govPower = GovernmentalPower.create(:name=>"Some Gov Power") + @govSphere = GovernmentalSphere.create(:name=>"Some Gov Sphere") + @juridical_nature = JuridicalNature.create(:name => "Autarquia") + + @institution_list = [] + @institution_list << InstitutionTestHelper.create_public_institution( + "Ministerio Publico da Uniao", + "MPU", + "BR", + "DF", + "Gama", + @juridical_nature, + @govPower, + @govSphere, + "12.345.678/9012-45" + ) + + @institution_list << InstitutionTestHelper.create_public_institution( + "Tribunal Regional da Uniao", + "TRU", + "BR", + "DF", + "Brasilia", + @juridical_nature, + @govPower, + @govSphere, + "12.345.678/9012-90" + ) + end + + should "add new institution for user into edit profile" do + user = create_basic_user + + params_user = Hash.new + params_user[:institution_ids] = [] + + @institution_list.each do |institution| + params_user[:institution_ids] << institution.id + end + + post :edit, :profile => User.last.person.identifier, :user => params_user + + assert_equal @institution_list.count, User.last.institutions.count + end + + should "remove institutions for user into edit profile" do + user = create_basic_user + + @institution_list.each do |institution| + user.institutions << institution + end + user.save! + + params_user = Hash.new + params_user[:institution_ids] = [] + + assert_equal @institution_list.count, User.last.institutions.count + + post :edit, :profile => User.last.person.identifier, :user => params_user + + assert_equal 0, User.last.institutions.count + end + + protected + + def create_basic_user + user = fast_create(User) + user.person = fast_create(Person) + user.person.user = user + user.save! + user.person.save! + user + end + + def create_community name + community = fast_create(Community) + community.name = name + community.save + community + end +end diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb new file mode 100644 index 0000000..30da38f --- /dev/null +++ b/test/functional/search_controller_test.rb @@ -0,0 +1,60 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../helpers/plugin_test_helper' +require( +File.dirname(__FILE__) + +'/../../../../app/controllers/public/search_controller' +) + +class SearchController; def rescue_action(e) raise e end; end + +class SearchControllerTest < ActionController::TestCase + include PluginTestHelper + + def setup + @environment = Environment.default + @environment.enabled_plugins = ['SoftwareCommunitiesPlugin'] + @environment.save + + @controller = SearchController.new + @request = ActionController::TestRequest.new + @request.stubs(:ssl?).returns(:false) + @response = ActionController::TestResponse.new + end + + should "communities searches don't have institution" do + community = create_community("New Community") + institution = create_private_institution( + "New Private Institution", + "NPI" , + "Brazil", + "DF", + "Gama", + "66.544.314/0001-63" + ) + + get :communities, :query => "New" + + assert_includes assigns(:searches)[:communities][:results], community + assert_not_includes assigns(:searches)[:communities][:results], institution + end + + should "institutions_search don't have community" do + community = create_community("New Community") + institution = create_private_institution( + "New Private Institution", + "NPI" , + "Brazil", + "DF", + "Gama", + "66.544.314/0001-63" + ) + + get :institutions, :query => "New" + + assert_includes( + assigns(:searches)[:institutions][:results], + institution.community + ) + assert_not_includes assigns(:searches)[:institutions][:results], community + end +end diff --git a/test/helpers/institution_test_helper.rb b/test/helpers/institution_test_helper.rb new file mode 100644 index 0000000..1f4df2f --- /dev/null +++ b/test/helpers/institution_test_helper.rb @@ -0,0 +1,59 @@ +module InstitutionTestHelper + + def self.generate_form_fields name, country, state, city, cnpj, type + fields = { + :community => { + :name => name, + :country => country, + :state => state, + :city => city + }, + :institutions => { + :cnpj=> cnpj, + :type => type, + :acronym => "", + :governmental_power => "", + :governmental_sphere => "", + :juridical_nature => "", + :corporate_name => "coporate default" + } + } + fields + end + + def self.create_public_institution name, acronym, country, state, city, juridical_nature, gov_p, gov_s, cnpj + institution = PublicInstitution.new + institution.community = institution_community(name, country, state, city) + institution.name = name + institution.juridical_nature = juridical_nature + institution.acronym = acronym + institution.governmental_power = gov_p + institution.governmental_sphere = gov_s + institution.cnpj = cnpj + institution.corporate_name = "corporate default" + institution.save + institution + end + + def self.create_private_institution name, acronym, country, state, city, cnpj + institution = PrivateInstitution.new + institution.community = institution_community(name, country, state, city) + institution.name = name + institution.acronym = acronym + institution.cnpj = cnpj + institution.corporate_name = "corporate default" + institution.save + + institution + end + + def self.institution_community name, country, state, city + institution_community = Community::new + institution_community.name = name + institution_community.country = country + institution_community.state = state + institution_community.city = city + institution_community.save + institution_community + end +end \ No newline at end of file diff --git a/test/helpers/plugin_test_helper.rb b/test/helpers/plugin_test_helper.rb index 46ea88f..e83a713 100644 --- a/test/helpers/plugin_test_helper.rb +++ b/test/helpers/plugin_test_helper.rb @@ -1,4 +1,7 @@ +require File.dirname(__FILE__) + '/../helpers/institution_test_helper' + module PluginTestHelper + def create_person name, email, password, password_confirmation, secondary_email, state, city user = create_user( name.to_slug, @@ -34,4 +37,41 @@ module PluginTestHelper user end + + def create_public_institution *params + InstitutionTestHelper.create_public_institution *params + end + + def create_community name + community = fast_create(Community) + community.name = name + community.save + community + end + + + def create_private_institution name, acronym, country, state, city, cnpj + InstitutionTestHelper.create_private_institution( + name, + acronym, + country, + state, + city, + cnpj + ) + end + + def create_public_institution *params + InstitutionTestHelper.create_public_institution *params + end + + def create_community_institution name, country, state, city + community = fast_create(Community) + community.name = name + community.country = country + community.state = state + community.city = city + community.save + community + end end diff --git a/views/gov_user_plugin/_institution.html.erb b/views/gov_user_plugin/_institution.html.erb new file mode 100644 index 0000000..7b59d89 --- /dev/null +++ b/views/gov_user_plugin/_institution.html.erb @@ -0,0 +1,129 @@ +

<%= _('New Institution') %>

+ +<% if environment.enabled?('admin_must_approve_new_communities') %> +
+ <%= _("Note that the creation of communities in this environment is restricted. Your request to create this new community will be sent to %{environment} administrators and will be approved or rejected according to their methods and criteria.") % { :environment => environment.name }%> +
+<%end %> + +<% unless flash[:errors].nil? %> +
+

<%= _("Can`t create new Institution: #{flash[:errors].length} errors") %>

+ +
+<% end %> + +
+ +
+
+ <%= _("All fields with (*) are mandatory") %> +
+
+ <%= labelled_form_for :community, :url => {:action=>"new_institution"}, :html => { :multipart => true, :id=>"institution_form" } do |f| %> + <%= hidden_field_tag "edit_institution_page", false %> + <%= fields_for :institutions do |inst| %> + +
+ + + +
+
+ + <%= required f.text_field(:name) %> + <%= content_tag :span, _("Institution name already exists"), :id=>"already_exists_text", :class=>"errorExplanation hide-field" %> + + +
+ <%= inst.label "corporate_name", _("Corporate Name"), :class=>"formlabel" %> + <%= required inst.text_field(:corporate_name) %> +
+
+ + <%= required select_country(_('Country'), 'community', 'country', {:class => 'type-select', :id => "community_country"}) %> + + +
+ + <%= f.select(:state, @state_list.collect {|state| [state.name, state.name]}) %> +
+
+ + <%= required f.text_field(:city) %> + + + +
+ <%= inst.label("cnpj" ,_("CNPJ"), :class=>"formlabel") %> + <%= required inst.text_field(:cnpj, :placeholder=>"99.999.999/9999-99", :class=>"intitution_cnpj_field") %> +
+
+ + +
+ <%= hidden_field_tag "acronym_translate", _("Acronym") %> + <%= hidden_field_tag "fantasy_name_translate", _("Fantasy name") %> + <%= inst.label("acronym" ,_("Acronym"), :class=>"formlabel") %> + <%= inst.text_field(:acronym) %> +
+
+ + +
+ <%= inst.label("governmental_sphere_id" ,_("Governmental Sphere:"), :class=>"formlabel") %> + <%= inst.select(:governmental_sphere, [[_("Select a Governmental Sphere"), 0]]|GovernmentalSphere.all.map {|s| [s.name, s.id]}, {:selected=>0})%> +
+
+ + +
+ <%= inst.label("governmental_power_id" ,_("Governmental Power:"), :class=>"formlabel") %> + <%= inst.select(:governmental_power, [[_("Select a Governmental Power"), 0]]|GovernmentalPower.all.map {|g| [g.name, g.id]}, {:selected=>0})%> +
+
+ +
+ <%= inst.label("juridical_nature_id" ,_("Juridical Nature:"), :class=>"formlabel") %> + <%= inst.select(:juridical_nature, [[_("Select a Juridical Nature"), 0]]|JuridicalNature.all.map {|j| [j.name, j.id]}, {:selected=>0})%> +
+
+ + +
+ <%= _("SISP?") %> + <% if @show_sisp_field %> + <%= inst.label("sisp" ,_("Yes")) %> + <%= inst.radio_button(:sisp, true) %> + <%= inst.label("sisp" ,_("No")) %> + <%= inst.radio_button(:sisp, false, :checked=>"checked") %> + <% else %> + <%= inst.label("sisp", _("No")) %> + <% end %> +
+
+
+ + <% if @url_token == "create_institution_admin" %> + <%= submit_button :save, _('Save') %> + <%else%> +
+ <%= link_to(_('Save'), '#', :id=>'save_institution_button', :class=>'button with-text icon-add') %> +
+ <%= hidden_field_tag :institution_error_message, _("Could not send the form data to the server") %> + <%end%> + + <% end %> + + <% end %> +
+<%= hidden_field_tag :loading_message, _("Creating institution") %> diff --git a/views/gov_user_plugin/create_institution.html.erb b/views/gov_user_plugin/create_institution.html.erb new file mode 100644 index 0000000..037140f --- /dev/null +++ b/views/gov_user_plugin/create_institution.html.erb @@ -0,0 +1 @@ +<%= render :partial => "institution" %> diff --git a/views/gov_user_plugin/create_institution_admin.html.erb b/views/gov_user_plugin/create_institution_admin.html.erb new file mode 100644 index 0000000..037140f --- /dev/null +++ b/views/gov_user_plugin/create_institution_admin.html.erb @@ -0,0 +1 @@ +<%= render :partial => "institution" %> diff --git a/views/gov_user_plugin_myprofile/_public_software_info.html.erb b/views/gov_user_plugin_myprofile/_public_software_info.html.erb new file mode 100644 index 0000000..1f3466c --- /dev/null +++ b/views/gov_user_plugin_myprofile/_public_software_info.html.erb @@ -0,0 +1,102 @@ +
+ <% if @disabled_public_software_field == true %> + <%= check_box_tag("software[public_software]", "true", @software_info.public_software?, :disabled => "disabled") %> + <%= label_tag _("Public Software"), _("Public software"), :class => "public_software_disabled" %> + <% else %> + <%= check_box_tag("software[public_software]", "true", @software_info.public_software?) %> + <%= label_tag _("Public Software"), _("Public software"), :class => "public_software_enabled" %> + <% end %> +
+

<%= _("Public Software") %>

+
+ <%= label_tag _("Adherent to e-PING ?") %> + + <%= label_tag "e_ping_true", "Yes" %> + <%= radio_button_tag("software[e_ping]", true, @software_info.e_ping)%> + <%= label_tag "e_ping_false", "No"%> + <%= radio_button_tag("software[e_ping]", false, !@software_info.e_ping)%> +
+ +
+ <%= label_tag _("Adherent to e-MAG ?") %> + + <%= label_tag "e_mag_true", "Yes"%> + <%= radio_button_tag("software[e_mag]", true, @software_info.e_mag)%> + <%= label_tag "e_mag_false", "No"%> + <%= radio_button_tag("software[e_mag]", false, !@software_info.e_mag)%> +
+ +
+ <%= label_tag _("Adherent to ICP-Brasil ?") %> + + <%= label_tag "icp_brasil_true", "Yes"%> + <%= radio_button_tag("software[icp_brasil]", true, @software_info.icp_brasil)%> + <%= label_tag "icp_brasil_false", "No"%> + <%= radio_button_tag("software[icp_brasil]", false, !@software_info.icp_brasil)%> +
+ +
+ <%= label_tag _("Adherent to e-ARQ ?") %> + + <%= label_tag "e_arq_true", "Yes"%> + <%= radio_button_tag("software[e_arq]", true, @software_info.e_arq)%> + <%= label_tag "e_arq_false", "No"%> + <%= radio_button_tag("software[e_arq]", false, !@software_info.e_arq)%> +
+ +
+ <%= label_tag _("Internacionalizable ?") %> + + <%= label_tag "intern_true", "Yes" %> + <%= radio_button_tag("software[intern]", true, @software_info.intern)%> + <%= label_tag "intern_false", "No"%> + <%= radio_button_tag("software[intern]", false, !@software_info.intern)%> +
+
+
+ +
+

<%= _("Operating Platform") %>

+ <%= text_area_tag "software[operating_platform]", @software_info.operating_platform, :cols => 40, :rows => 5%> +
+ +
+

<%= _("Features") %>

+ <%= text_area_tag "software[features]", @software_info.features, :maxlength=>"4000", :cols => 40, :rows => 5%> +
+ +
+

<%= _("Demonstration url") %>

+ <%= text_field_tag("software[demonstration_url]", @software_info.demonstration_url) %> +
+ +
+

<%= _("Libraries") %>

+ + <%= render :partial => 'library_fields', :locals => {:object_name => 'community', :profile => @community, :libraries => @list_libraries } %> +
+ +
+ +
+

<%= _("Operating Systems") %>

+ + <%= render :partial => 'operating_system_fields', :locals => {:object_name => 'community', :profile => @community, :operating_systems_fields => @list_operating_systems} %> +
+
+ +
+
+

<%= _("Programming languages") %>

+ + <%= render :partial => 'language_fields', :locals => { :object_name => 'community', :profile => @community, :languages => @list_languages } %> +
+ +
+
+

<%= _("Databases") %>

+ + <%= render :partial => 'database_fields', :locals => {:object_name => 'community', :profile => @community, :database => @list_databases } %> +
+ +
diff --git a/views/gov_user_plugin_myprofile/edit_institution.html.erb b/views/gov_user_plugin_myprofile/edit_institution.html.erb new file mode 100644 index 0000000..db95e3a --- /dev/null +++ b/views/gov_user_plugin_myprofile/edit_institution.html.erb @@ -0,0 +1,119 @@ +

<%= _('Edit Institution') %>

+ +<% if environment.enabled?('admin_must_approve_new_communities') %> +
+ <%= _("Note that the creation of communities in this environment is restricted. Your request to create this new community will be sent to %{environment} administrators and will be approved or rejected according to their methods and criteria.") % { :environment => environment.name }%> +
+<%end %> + +<% unless flash[:errors].nil? %> +
+

<%= _("Can`t create new Institution: #{flash[:errors].length} errors") %>

+ +
+<% end %> + +
+ +
+
+ <%= _("All fields with (*) are mandatory") %> +
+
+ <%= labelled_form_for :community,:html => { :multipart => true, :id=>"institution_form" } do |f| %> + <%= hidden_field_tag "edit_institution_page", true %> + <%= fields_for :institutions do |inst| %> + +
+ + + +
+
+ + <%= required f.text_field(:name, :value => @institution.community.name) %> + <%= content_tag :span, _("Institution name already exists"), :id=>"already_exists_text", :class=>"errorExplanation hide-field" %> + + +
+ <%= inst.label "corporate_name", _("Corporate Name"), :class=>"formlabel" %> + <%= required inst.text_field(:corporate_name, :value => @institution.corporate_name) %> +
+
+ + <%= required select_country(_('Country'), 'community', 'country', {:class => 'type-select', :id => "community_country"}, :selected => @institution.community.country) %> + + +
+ + <%= f.select(:state, @state_list.collect {|state| [state.name, state.name]}, :selected => @institution.community.state) %> +
+
+ + <%= required f.text_field(:city, :value => @institution.community.city) %> + + + +
+ <%= inst.label("cnpj" ,_("CNPJ"), :class=>"formlabel") %> + <%= required inst.text_field(:cnpj, :placeholder=>"99.999.999/9999-99", :class=>"intitution_cnpj_field", :value => @institution.cnpj) %> +
+
+ + +
+ <%= hidden_field_tag "acronym_translate", _("Acronym") %> + <%= hidden_field_tag "fantasy_name_translate", _("Fantasy name") %> + <%= inst.label("acronym" ,_("Acronym"), :class=>"formlabel") %> + <%= inst.text_field(:acronym, :value => @institution.acronym) %> +
+
+ + +
+ <%= inst.label("governmental_sphere_id" ,_("Governmental Sphere:"), :class=>"formlabel") %> + <%= inst.select(:governmental_sphere, [[_("Select a Governmental Sphere"), 0]]|GovernmentalSphere.all.map {|s| [s.name, s.id]}, {:selected=>@institution.governmental_power_id})%> +
+
+ + +
+ <%= inst.label("governmental_power_id" ,_("Governmental Power:"), :class=>"formlabel") %> + <%= inst.select(:governmental_power, [[_("Select a Governmental Power"), 0]]|GovernmentalPower.all.map {|g| [g.name, g.id]}, {:selected=> @institution.governmental_sphere_id})%> +
+
+ +
+ <%= inst.label("juridical_nature_id" ,_("Juridical Nature:"), :class=>"formlabel") %> + <%= inst.select(:juridical_nature, [[_("Select a Juridical Nature"), 0]]|JuridicalNature.all.map {|j| [j.name, j.id]}, {:selected=> @institution.juridical_nature_id})%> +
+
+ + +
+ <%= _("SISP?") %> + <% if @show_sisp_field %> + <%= inst.label("sisp" ,_("Yes")) %> + <%= inst.radio_button(:sisp, true, :checked=>(@institution.sisp ? true : false)) %> + <%= inst.label("sisp" ,_("No")) %> + <%= inst.radio_button(:sisp, false, :checked=>(@institution.sisp ? false : true)) %> + <% else %> + <%= inst.label("sisp", _("No")) %> + <% end %> +
+
+
+ + <%= submit_button :save, _('Save') %> + <% end %> +<% end %> + diff --git a/views/search/institutions.html.erb b/views/search/institutions.html.erb new file mode 100644 index 0000000..1b33ed2 --- /dev/null +++ b/views/search/institutions.html.erb @@ -0,0 +1,16 @@ +<%= search_page_title( @titles[@asset], @category ) %> + +<%= render :partial => 'search_form', :locals => { :hint => _("Type words about the %s you're looking for") % @asset.to_s.singularize } %> + +<%= display_results(@searches, @asset) %> +<% if params[:display] != 'map' %> + <%= pagination_links @searches[@asset][:results] %> +<% end %> + +
+ +<% if @asset == :product %> + <%= javascript_tag do %> + jQuery('.search-product-price-details').altBeautify(); + <% end %> +<% end %> -- libgit2 0.21.2