diff --git a/controllers/mpog_software_plugin_myprofile_controller.rb b/controllers/mpog_software_plugin_myprofile_controller.rb index f4c33f1..8dfc9f2 100644 --- a/controllers/mpog_software_plugin_myprofile_controller.rb +++ b/controllers/mpog_software_plugin_myprofile_controller.rb @@ -42,7 +42,7 @@ class MpogSoftwarePluginMyprofileController < MyProfileController return unless request.post? @software_info = constroy_software - software_info_insert_models.call(@list_libraries, 'libraries') + software_info_insert_models.call(@list_library, 'libraries') software_info_insert_models.call(@list_languages, 'software_languages') software_info_insert_models.call(@list_databases, 'software_databases') software_info_insert_models.call(@list_operating_systems, 'operating_systems') @@ -130,7 +130,7 @@ class MpogSoftwarePluginMyprofileController < MyProfileController end def create_list_model_helpers - @list_libraries = LibraryHelper.list_libraries(params[:library]) + @list_libraries = LibraryHelper.list_library(params[:library]) @list_languages = SoftwareLanguageHelper.list_language(params[:language]) @list_databases = DatabaseHelper.list_database(params[:database]) @list_operating_systems = OperatingSystemHelper.list_operating_system(params[:operating_system]) diff --git a/lib/library_helper.rb b/lib/library_helper.rb index 8e84f52..b39a0a8 100644 --- a/lib/library_helper.rb +++ b/lib/library_helper.rb @@ -1,7 +1,5 @@ -class LibraryHelper < DynamicTableHelper - MODEL_NAME ="library" - - def self.list_libraries new_libraries +module LibraryHelper + def self.list_library new_libraries return [] if new_libraries.nil? or new_libraries.length == 0 list_libraries = [] @@ -18,7 +16,7 @@ class LibraryHelper < DynamicTableHelper list_libraries end - def self.valid_list_libraries? list_libraries + def self.valid_list_library? list_libraries return true if list_libraries.nil? or list_libraries.length == 0 list_libraries.each do |library| diff --git a/lib/mpog_software_plugin.rb b/lib/mpog_software_plugin.rb index 2807914..1b79d67 100644 --- a/lib/mpog_software_plugin.rb +++ b/lib/mpog_software_plugin.rb @@ -18,14 +18,16 @@ class MpogSoftwarePlugin < Noosfero::Plugin end def profile_editor_extras - if context.profile.person? + profile = context.profile + + if profile.person? expanded_template('person_editor_extras.html.erb') - elsif context.profile.respond_to?(:software_info) && - !context.profile.software_info.nil? + elsif profile.respond_to?(:software_info) && + !profile.software_info.nil? - if context.profile.software_info.first_edit? - context.profile.software_info.first_edit = false - context.profile.software_info.save! + if profile.software_info.first_edit? + profile.software_info.first_edit = false + profile.software_info.save! expanded_template('first_edit_software_community_extras.html.erb') end end @@ -34,11 +36,12 @@ class MpogSoftwarePlugin < Noosfero::Plugin def profile_editor_transaction_extras single_hash_transactions = { :user => 'user', :software_info => 'software_info', - :version => 'license', :language => 'language', - :operating_system => 'operating_system', + :version => 'license', :language => 'generic_model', + :operating_system => 'generic_model', :software_categories => 'software_categories', :instituton => 'instituton', - :library => 'libraries' } + :library => 'generic_model', + :database => 'generic_model' } single_hash_transactions.each do |model, transaction| call_model_transaction(model, transaction) @@ -72,7 +75,7 @@ class MpogSoftwarePlugin < Noosfero::Plugin def profile_tabs if context.profile.community? - profile_tabs_software if context.profile.software? + return profile_tabs_software if context.profile.software? profile_tabs_institution end end @@ -190,21 +193,22 @@ class MpogSoftwarePlugin < Noosfero::Plugin def profile_required_list 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[: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) empty_fields = [] required_list = profile_required_list @@ -218,27 +222,6 @@ class MpogSoftwarePlugin < Noosfero::Plugin empty_fields end - def operating_system_transaction - OperatingSystem.transaction do - list_operating = OperatingSystemHelper.list_operating_system( - context.params[:operating_system] - ) - - if OperatingSystemHelper.valid_list_operating_system?(list_operating) - OperatingSystem.where( - :software_info_id => context.profile.software_info.id - ).destroy_all - - list_operating.each do |operating_system| - operating_system.software_info = context.profile.software_info - operating_system.save! - end - else - raise 'Invalid Operating System fields' - end - end - end - def user_transaction user_editor_institution_actions @@ -274,80 +257,12 @@ class MpogSoftwarePlugin < Noosfero::Plugin end end - def libraries_transaction - Library.transaction do - list_libraries = LibraryHelper.list_libraries(context.params[:library]) - - if LibraryHelper.valid_list_libraries?(list_libraries) - Library.where( - :software_info_id=> context.profile.software_info.id - ).destroy_all - - list_libraries.each do |library| - library.software_info_id = context.profile.software_info.id - library.save! - end - else - raise 'Invalid Library fields' - end - end - end - - def databases_transaction - SoftwareDatabase.transaction do - list_databases = DatabaseHelper.list_database(context.params[:database]) - - if DatabaseHelper.valid_list_database?(list_databases) - SoftwareDatabase.where( - :software_info_id => context.profile.software_info.id - ).destroy_all - - list_databases.each do |database| - database.software_info = context.profile.software_info - database.save! - end - else - raise 'Invalid Database fields' - end - end - end - def license_transaction license = LicenseInfo.find(context.params[:version]) context.profile.software_info.license_info = license context.profile.software_info.save! end - def language_transaction - SoftwareLanguage.transaction do - list_language = SoftwareLanguageHelper.list_language( - context.params[:language] - ) - - if SoftwareLanguageHelper.valid_list_language?(list_language) - SoftwareLanguage.where( - :software_info_id => context.profile.software_info.id - ).destroy_all - - list_language.each do |language| - language.software_info = context.profile.software_info - language.save! - end - else - raise 'Invalid Software Language fields' - end - end - end - - def software_categories_transaction - ControlledVocabulary.transaction do - context.profile - .software_info - .software_categories - .update_attributes!(context.params[:software_categories]) - end - end - private # Add and remove the user from it's institutions communities @@ -386,6 +301,34 @@ class MpogSoftwarePlugin < Noosfero::Plugin context.profile.institution.save! end + def generic_model_transaction + models_list = [ + [SoftwareLanguage, SoftwareLanguageHelper, 'language'], + [SoftwareDatabase, DatabaseHelper, 'database'], + [OperatingSystem, OperatingSystemHelper, 'operating_system'], + [Library, LibraryHelper, 'library'] + ] + models_list.each do |model| + list_of_model = 'list_'+model[2].to_s + model[0].transaction do + list = model[1].send(list_of_model, context.params[model[2].to_sym]) + + if model[2].send('valid_'+list_of_model+'?', list_of_model) + model[0].where( + :software_info_id => context.profile.software_info.id + ).destroy_all + + list.each do |model| + model.software_info = context.profile.software_info + model.save! + end + else + raise 'Invalid Software #{model[2]} fields' + end + end + end + end + def software_info_button { :title => _('Software Info'), diff --git a/test/unit/library_helper_test.rb b/test/unit/library_helper_test.rb index 34201dd..fd3298f 100644 --- a/test/unit/library_helper_test.rb +++ b/test/unit/library_helper_test.rb @@ -17,12 +17,12 @@ class LibraryHelperTest < ActiveSupport::TestCase should "return an empty list" do empty_list = [] - assert_equal [],LibraryHelper.list_libraries(empty_list) + assert_equal [],LibraryHelper.list_library(empty_list) end should "return a list with current library objects" do list_compare = [] - lib_table = LibraryHelper.list_libraries(@license_objects) + lib_table = LibraryHelper.list_library(@license_objects) assert_equal list_compare.class, lib_table.class end @@ -32,13 +32,13 @@ class LibraryHelperTest < ActiveSupport::TestCase end should "return a list with the same size of the parameter" do - list_compare = LibraryHelper.list_libraries(@license_objects) + list_compare = LibraryHelper.list_library(@license_objects) assert_equal @license_objects.count, list_compare.count end should "return false if list_database are empty or null" do list_compare = [] - assert_equal true, LibraryHelper.valid_list_libraries?(list_compare) + assert_equal true, LibraryHelper.valid_list_library?(list_compare) end should "return a html text with license name equals to linux" do -- libgit2 0.21.2