diff --git a/lib/mpog_software_plugin.rb b/lib/mpog_software_plugin.rb index 353dfee..6bd6e9e 100644 --- a/lib/mpog_software_plugin.rb +++ b/lib/mpog_software_plugin.rb @@ -11,18 +11,18 @@ class MpogSoftwarePlugin < Noosfero::Plugin include Rails.application.routes.url_helpers def self.plugin_name - "MpogSoftwarePlugin" + 'MpogSoftwarePlugin' end def self.plugin_description - _("Add Public Software and MPOG features.") + _('Add Public Software and MPOG features.') end def profile_editor_extras if context.profile.person? expanded_template('person_editor_extras.html.erb') elsif context.profile.respond_to?(:software_info) && - !context.profile.software_info.nil? + !context.profile.software_info.nil? if context.profile.software_info.first_edit? context.profile.software_info.first_edit = false @@ -33,16 +33,17 @@ class MpogSoftwarePlugin < Noosfero::Plugin end def profile_editor_transaction_extras - single_hash_transactions = {:user => "user", :software_info => "software_info", - :version => "license", :language => "language", - :operating_system => "operating_system", - :software_categories => "software_categories", - :instituton => "instituton", :library => "libraries"} - - single_hash_transactions.each do |model,transaction| - call_model_transaction(model,transaction) - end - + single_hash_transactions = { :user => 'user', + :software_info => 'software_info', + :version => 'license', :language => 'language', + :operating_system => 'operating_system', + :software_categories => 'software_categories', + :instituton => 'instituton', + :library => 'libraries' } + + single_hash_transactions.each do |model, transaction| + call_model_transaction(model, transaction) + end end def profile_editor_controller_filters @@ -63,8 +64,8 @@ class MpogSoftwarePlugin < Noosfero::Plugin end [{ - :type => "before_filter", - :method_name => "validate_institution_sisp_field_access", + :type => 'before_filter', + :method_name => 'validate_institution_sisp_field_access', :options => { :only => :edit }, :block => block }] @@ -72,7 +73,7 @@ class MpogSoftwarePlugin < Noosfero::Plugin def profile_tabs if context.profile.community? - return profile_tabs_software if context.profile.software? + profile_tabs_software if context.profile.software? profile_tabs_institution end end @@ -89,14 +90,14 @@ class MpogSoftwarePlugin < Noosfero::Plugin def self.extra_blocks { - SoftwaresBlock => {:type => [Environment, Person] }, - SoftwareInformationBlock => {:type => [Community] }, - InstitutionsBlock => {:type => [Environment, Person]}, - DownloadBlock => {:type => [Community]}, - RepositoryBlock => {:type => [Community]}, - CategoriesAndTagsBlock => {:type => [Community]}, - CategoriesSoftwareBlock => {:type => [Environment]}, - SearchCatalogBlock => {:type => [Environment]} + SoftwaresBlock => { :type => [Environment, Person] }, + SoftwareInformationBlock => { :type => [Community] }, + InstitutionsBlock => { :type => [Environment, Person] }, + DownloadBlock => { :type => [Community] }, + RepositoryBlock => { :type => [Community] }, + CategoriesAndTagsBlock => { :type => [Community] }, + CategoriesSoftwareBlock => { :type => [Environment] }, + SearchCatalogBlock => { :type => [Environment] } } end @@ -105,7 +106,7 @@ class MpogSoftwarePlugin < Noosfero::Plugin end def js_files - %w[ + %w( jquery.maskedinput.min.js spb-utils.js mpog-software.js @@ -116,11 +117,11 @@ class MpogSoftwarePlugin < Noosfero::Plugin mpog-search.js software-catalog.js mpog-software-block.js - ] + ) end def add_new_organization_buttons - Proc::new do + proc do button( :add, _('Create a new software'), @@ -137,34 +138,18 @@ class MpogSoftwarePlugin < Noosfero::Plugin def profile_blocks_extra_content return if context.session[:user].nil? || - !context.session[:hide_incomplete_percentage].blank? - - person = Person.where(:user_id=>context.session[:user]).first - - if context.profile && context.profile.person? && !person.nil? - @person = person - @percentege = calc_percentage_registration(person) + !context.session[:hide_incomplete_percentage].blank? - if @percentege >= 0 && @percentege <= 100 - expanded_template('incomplete_registration.html.erb') - end - end + person = Person.where(:user_id => context.session[:user]).first + puts "="*80,person,"="*80 + call_percentage_profile_template(person) end - def custom_user_registration_attributes user - unless context.params[:user][:institution_ids].nil? - context.params[:user][:institution_ids].delete("") + def custom_user_registration_attributes(user) + return if context.params[:user][:institution_ids].nil? + context.params[:user][:institution_ids].delete('') - 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 - end - user.save unless user.institution_ids.empty? + update_user_institutions(user) user.institutions.each do |institution| community = institution.community @@ -172,12 +157,12 @@ class MpogSoftwarePlugin < Noosfero::Plugin end end - def calc_percentage_registration person + def calc_percentage_registration(person) required_list = profile_required_list empty_fields = profile_required_empty_list person count = required_list[:person_fields].count + required_list[:user_fields].count - percentege = 100 - ((empty_fields.count*100)/count) + percentege = 100 - ((empty_fields.count * 100) / count) person.percentage_incomplete = percentege person.save(validate: false) percentege @@ -205,36 +190,31 @@ class MpogSoftwarePlugin < Noosfero::Plugin end def profile_required_list - fields = Hash.new - fields[:person_fields] = [ - "cell_phone", - "contact_phone", - "comercial_phone", - "country", - "city", - "state", - "organization_website", - "image", - "identifier", - "name" - ] - fields[:user_fields] = ["secondary_email", "email"] + fields = {} + fields[:person_fields] = %w('cell_phone', + 'contact_phone', + 'comercial_phone', + 'country', + 'city', + 'state', + 'organization_website', + 'image', + 'identifier', + 'name') + + fields[:user_fields] = %w('secondary_email', 'email') fields end - def profile_required_empty_list person + def profile_required_empty_list(person) empty_fields = [] required_list = profile_required_list required_list[:person_fields].each do |field| - if person.send(field).blank? - empty_fields << field.sub("_"," ") - end + empty_fields << field.sub('_',' ') if person.send(field).blank? end required_list[:user_fields].each do |field| - if person.user.send(field).blank? - empty_fields << field.sub("_"," ") - end + empty_fields << field.sub('_',' ') if person.user.send(field).blank? end empty_fields end @@ -271,7 +251,8 @@ class MpogSoftwarePlugin < Noosfero::Plugin def institution_transaction institution.date_modification = DateTime.now institution.save - institution_models = %w(governmental_power governmental_sphere juridical_nature) + institution_models = %w(governmental_power governmental_sphere + juridical_nature) institution_models.each do |model| call_institution_transaction(model) @@ -361,10 +342,10 @@ class MpogSoftwarePlugin < Noosfero::Plugin def software_categories_transaction ControlledVocabulary.transaction do - context.profile. - software_info. - software_categories. - update_attributes!(context.params[:software_categories]) + context.profile + .software_info + .software_categories + .update_attributes!(context.params[:software_categories]) end end @@ -381,7 +362,7 @@ class MpogSoftwarePlugin < Noosfero::Plugin new_communities = [] unless context.params[:user][:institution_ids].nil? - context.params[:user][:institution_ids].delete("") + context.params[:user][:institution_ids].delete('') context.params[:user][:institution_ids].each do |id| new_communities << Institution.find(id).community @@ -397,49 +378,49 @@ class MpogSoftwarePlugin < Noosfero::Plugin end def call_model_transaction(model,name) - send(name + '_transaction') if context.params.has_key?(model.to_sym) + send(name + '_transaction') if context.params.key?(model.to_sym) end def call_institution_transaction(model) - context.profile.institution.send(model + '_id = ', context.params[model.to_sym]) + context.profile.institution.send(model + '_id = ', + context.params[model.to_sym]) context.profile.institution.save! end - def software_info_button { - :title => _("Software Info"), - :icon => "edit-profile-group control-panel-software-link", + :title => _('Software Info'), + :icon => 'edit-profile-group control-panel-software-link', :url => { - :controller => "mpog_software_plugin_myprofile", - :action => "edit_software" + :controller => 'mpog_software_plugin_myprofile', + :action => 'edit_software' } } end def create_new_software_button { - :title => _("Create a new software"), - :icon => "design-editor", + :title => _('Create a new software'), + :icon => 'design-editor', :url => { - :controller => "mpog_software_plugin_myprofile", - :action => "new_software" + :controller => 'mpog_software_plugin_myprofile', + :action => 'new_software' } } end def institution_info_button { - :title => _("Institution Info"), - :icon => "edit-profile-group control-panel-instituton-link", + :title => _('Institution Info'), + :icon => 'edit-profile-group control-panel-instituton-link', :url => { - :controller => "mpog_software_plugin_myprofile", - :action => "edit_institution" + :controller => 'mpog_software_plugin_myprofile', + :action => 'edit_institution' } } end - def manage_user_institutions(leave_communities,enter_communities) + def manage_user_institutions(leave_communities, enter_communities) leave_communities = (old_communities - new_communities) enter_communities = (new_communities - old_communities) @@ -455,17 +436,40 @@ class MpogSoftwarePlugin < Noosfero::Plugin end def profile_tabs_software - { :title => _("Software"), + { :title => _('Software'), :id => 'mpog-fields', - :content => Proc::new do render :partial => 'software_tab' end, + :content => proc do render :partial => 'software_tab' end, :start => true } end def profile_tabs_institution - { :title => _("Institution"), + { :title => _('Institution'), :id => 'mpog-fields', :content => Proc::new do render :partial => 'institution_tab' end, :start => true } end + + def call_percentage_profile_template(person) + if context.profile && context.profile.person? && !person.nil? + @person = person + @percentege = calc_percentage_registration(person) + + if @percentege >= 0 && @percentege <= 100 + expanded_template('incomplete_registration.html.erb') + end + end + 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 -- libgit2 0.21.2