From 0205a13b03575fc4ce040f7907b3eef0d4db455c Mon Sep 17 00:00:00 2001 From: Luciano Prestes Cavalcanti Date: Fri, 16 Oct 2015 14:54:58 -0300 Subject: [PATCH] Refactor software_communities. --- src/noosfero-spb/software_communities/controllers/software_communities_plugin_controller.rb | 16 +++++----------- src/noosfero-spb/software_communities/controllers/software_communities_plugin_myprofile_controller.rb | 38 +++++++++++++++++++++++--------------- src/noosfero-spb/software_communities/lib/create_software.rb | 14 +++++++++++--- src/noosfero-spb/software_communities/lib/database_helper.rb | 15 +++------------ src/noosfero-spb/software_communities/lib/library_helper.rb | 9 ++------- src/noosfero-spb/software_communities/lib/license_helper.rb | 10 +++++++--- src/noosfero-spb/software_communities/lib/operating_system_helper.rb | 8 ++------ src/noosfero-spb/software_communities/lib/software_communities_plugin.rb | 5 ++--- src/noosfero-spb/software_communities/lib/software_helper.rb | 18 +----------------- src/noosfero-spb/software_communities/lib/software_info.rb | 19 +++++++++++-------- src/noosfero-spb/software_communities/lib/software_language_helper.rb | 7 +++---- src/noosfero-spb/software_communities/test/functional/software_communities_plugin_myprofile_controller_test.rb | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- src/noosfero-spb/software_communities/test/unit/software_info_validation_test.rb | 1 + src/noosfero-spb/software_communities/views/_main_software_editor_extras.html.erb | 32 -------------------------------- src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_license_info_fields.html.erb | 4 ---- src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_main_software_editor_extras.html.erb | 2 +- 16 files changed, 167 insertions(+), 130 deletions(-) delete mode 100644 src/noosfero-spb/software_communities/views/_main_software_editor_extras.html.erb diff --git a/src/noosfero-spb/software_communities/controllers/software_communities_plugin_controller.rb b/src/noosfero-spb/software_communities/controllers/software_communities_plugin_controller.rb index d3bb2c5..0d59ad8 100644 --- a/src/noosfero-spb/software_communities/controllers/software_communities_plugin_controller.rb +++ b/src/noosfero-spb/software_communities/controllers/software_communities_plugin_controller.rb @@ -5,11 +5,9 @@ class SoftwareCommunitiesPluginController < ApplicationController def get_license_data return render :json=>{} if !request.xhr? || params[:query].nil? - data = if params[:query].empty? - LicenseInfo.all - else - LicenseInfo.where("version ILIKE ?", "%#{params[:query]}%").select("id, version") - end + data = LicenseHelper.find_licenses(params[:query]) if params[:query] + data ||= LicenseInfo.all + render :json=> data.collect { |license| {:id=>license.id, :label=>license.version} } @@ -44,11 +42,7 @@ class SoftwareCommunitiesPluginController < ApplicationController protected def get_model_by_params_field - case params[:field] - when "software_language" - return ProgrammingLanguage - else - return DatabaseDescription - end + return DatabaseDescription unless params[:field] == "software_language" + return ProgrammingLanguage end end diff --git a/src/noosfero-spb/software_communities/controllers/software_communities_plugin_myprofile_controller.rb b/src/noosfero-spb/software_communities/controllers/software_communities_plugin_myprofile_controller.rb index 582d788..4873301 100644 --- a/src/noosfero-spb/software_communities/controllers/software_communities_plugin_myprofile_controller.rb +++ b/src/noosfero-spb/software_communities/controllers/software_communities_plugin_myprofile_controller.rb @@ -9,13 +9,13 @@ class SoftwareCommunitiesPluginMyprofileController < MyProfileController @community = Community.new(params[:community]) @community.environment = environment - @software_info = SoftwareInfo.new(params[:software_info]) - @license_info = if params[:license].blank? or params[:license][:license_infos_id].blank? - LicenseInfo.new - else - LicenseInfo.find(params[:license][:license_infos_id]) - end + @license_info = LicenseInfo.find_by_id(params[:license][:license_infos_id]) if params[:license] + @license_info ||= LicenseInfo.new + + @software_info = SoftwareInfo.new(params[:software_info]) + @software_info.community = @community + @software_info.license_info = @license_info control_software_creation update_new_software_errors @@ -26,7 +26,7 @@ class SoftwareCommunitiesPluginMyprofileController < MyProfileController return unless request.post? - @software_info = constroy_software + @software_info = create_software software_info_insert_models.call(@list_libraries, 'libraries') software_info_insert_models.call(@list_languages, 'software_languages') software_info_insert_models.call(@list_databases, 'software_databases') @@ -70,17 +70,25 @@ class SoftwareCommunitiesPluginMyprofileController < MyProfileController def add_software_erros @errors = [] - @errors |= @community.errors.full_messages if @community + if @community + error = @community.errors.delete(:identifier) + @errors |= [_("Domain %s") % error.first ] if error + @errors |= @community.errors.full_messages + end @errors |= @software_info.errors.full_messages if @software_info @errors |= @license_info.errors.full_messages if @license_info end def control_software_creation - valid_models = request.post? && (@community.valid? && @software_info.valid? && @license_info.valid?) - if valid_models - send_software_to_moderation - else - add_software_erros + if request.post? + valid_models = @community.valid? + valid_models &= @software_info.valid? + valid_models &= @license_info.valid? + if valid_models + send_software_to_moderation + else + add_software_erros + end end end @@ -91,7 +99,7 @@ class SoftwareCommunitiesPluginMyprofileController < MyProfileController } end - def constroy_software + def create_software @software_info = @profile.software_info another_license_version = nil another_license_link = nil @@ -136,7 +144,7 @@ class SoftwareCommunitiesPluginMyprofileController < MyProfileController add_admin_to_community - if !environment.admins.include?(current_user.person) + if !environment.admins.include?(current_user.person) session[:notice] = _('Your new software request will be evaluated by an'\ 'administrator. You will be notified.') redirect_to user.admin_url diff --git a/src/noosfero-spb/software_communities/lib/create_software.rb b/src/noosfero-spb/software_communities/lib/create_software.rb index 7b8b914..2d313dd 100644 --- a/src/noosfero-spb/software_communities/lib/create_software.rb +++ b/src/noosfero-spb/software_communities/lib/create_software.rb @@ -5,12 +5,14 @@ class CreateSoftware < Task validates_presence_of :name attr_accessible :name, :finality, :repository_link, :requestor, :environment, - :reject_explanation, :license_info + :reject_explanation, :license_info, :identifier, :another_license_version, + :another_license_link alias :environment :target alias :environment= :target= - DATA_FIELDS = ['name', 'finality', 'license_info', 'repository_link'] + DATA_FIELDS = ['name', 'identifier', 'finality', 'license_info', 'repository_link', + 'another_license_version', 'another_license_link'] DATA_FIELDS.each do |field| settings_items field.to_sym end @@ -21,15 +23,21 @@ class CreateSoftware < Task template_id = software_template.id end + identifier = self.identifier + identifier ||= self.name.to_slug + community = Community.create!(:name => self.name, + :identifier => identifier, :template_id => template_id) community.environment = self.environment community.add_admin(self.requestor) - software = SoftwareInfo.create!(:finality => self.finality, + software = SoftwareInfo.new(:finality => self.finality, :repository_link => self.repository_link, :community_id => community.id, :license_info => self.license_info) + software.verify_license_info(self.another_license_version, self.another_license_link) + software.save! end def title diff --git a/src/noosfero-spb/software_communities/lib/database_helper.rb b/src/noosfero-spb/software_communities/lib/database_helper.rb index af7f5e2..94bf652 100644 --- a/src/noosfero-spb/software_communities/lib/database_helper.rb +++ b/src/noosfero-spb/software_communities/lib/database_helper.rb @@ -34,12 +34,7 @@ class DatabaseHelper < DynamicTableHelper def self.valid_list_database? list_databases return false if list_databases.nil? or list_databases.length == 0 - - list_databases.each do |database| - return false unless database.valid? - end - - true + return !list_databases.any?{|database| !database.valid?} end def self.database_as_tables(list_databases, disabled=false) @@ -51,14 +46,10 @@ class DatabaseHelper < DynamicTableHelper def self.database_html_structure(database_data, disabled) database_id = database_data[:database_description_id] - database_name = if database_data[:database_description_id].blank? - "" - else - DatabaseDescription.find( + database_name = database_id.blank? ? "" : DatabaseDescription.find( database_data[:database_description_id], :select=>"name" ).name - end data = { model_name: MODEL_NAME, @@ -83,4 +74,4 @@ class DatabaseHelper < DynamicTableHelper def self.add_dynamic_table database_as_tables(nil).first.call end -end \ No newline at end of file +end diff --git a/src/noosfero-spb/software_communities/lib/library_helper.rb b/src/noosfero-spb/software_communities/lib/library_helper.rb index dfb0953..579ebb7 100644 --- a/src/noosfero-spb/software_communities/lib/library_helper.rb +++ b/src/noosfero-spb/software_communities/lib/library_helper.rb @@ -20,12 +20,7 @@ class LibraryHelper < DynamicTableHelper def self.valid_list_library? list_libraries return true if list_libraries.nil? or list_libraries.length == 0 - - list_libraries.each do |library| - return false unless library.valid? - end - - true + return !list_libraries.any?{|library| !library.valid?} end def self.libraries_as_tables list_libraries, disabled=false @@ -59,4 +54,4 @@ class LibraryHelper < DynamicTableHelper def self.add_dynamic_table libraries_as_tables(nil).first.call end -end \ No newline at end of file +end diff --git a/src/noosfero-spb/software_communities/lib/license_helper.rb b/src/noosfero-spb/software_communities/lib/license_helper.rb index af390cb..8a21a2b 100644 --- a/src/noosfero-spb/software_communities/lib/license_helper.rb +++ b/src/noosfero-spb/software_communities/lib/license_helper.rb @@ -1,5 +1,9 @@ module LicenseHelper - def self.getListLicenses - LicenseInfo.all + def self.find_licenses query + licenses = LicenseInfo.where("version ILIKE ?", "%#{query}%").select("id, version") + licenses.reject!{|license| license.version == "Another"} + license_another = LicenseInfo.find_by_version("Another") + licenses << license_another if license_another + licenses end -end \ No newline at end of file +end diff --git a/src/noosfero-spb/software_communities/lib/operating_system_helper.rb b/src/noosfero-spb/software_communities/lib/operating_system_helper.rb index d5bd9a9..201ac05 100644 --- a/src/noosfero-spb/software_communities/lib/operating_system_helper.rb +++ b/src/noosfero-spb/software_communities/lib/operating_system_helper.rb @@ -25,12 +25,8 @@ class OperatingSystemHelper < DynamicTableHelper end def self.valid_list_operating_system? list_operating_system - return !(list_operating_system.nil? || list_operating_system.length == 0) - - list_operating_system.each do |operating_system| - return false unless operating_system.valid? - end - true + return false if (list_operating_system.nil? || list_operating_system.length == 0) + return !list_operating_system.any?{|os| !os.valid?} end def self.operating_system_as_tables(list_operating_system, disabled=false) diff --git a/src/noosfero-spb/software_communities/lib/software_communities_plugin.rb b/src/noosfero-spb/software_communities/lib/software_communities_plugin.rb index 20431ab..976ad14 100644 --- a/src/noosfero-spb/software_communities/lib/software_communities_plugin.rb +++ b/src/noosfero-spb/software_communities/lib/software_communities_plugin.rb @@ -22,8 +22,8 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin end def profile_tabs - if context.profile.community? - return profile_tabs_software if context.profile.software? + if context.profile.community? && context.profile.software? + return profile_tabs_software end end @@ -107,7 +107,6 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin is_admin ||= user_rating.organization.admins.include?(current_user.person) if is_admin and profile.software? - render :file => 'organization_ratings_extra_fields_show_data', :locals => {:user_rating => user_rating} end diff --git a/src/noosfero-spb/software_communities/lib/software_helper.rb b/src/noosfero-spb/software_communities/lib/software_helper.rb index a406c64..47ce223 100644 --- a/src/noosfero-spb/software_communities/lib/software_helper.rb +++ b/src/noosfero-spb/software_communities/lib/software_helper.rb @@ -23,22 +23,6 @@ module SoftwareHelper end def self.all_table_is_empty? table, ignored_fields=[] - filled_fields = [] - - table.each do |key, value| - unless ignored_fields.include? key - filled_fields << if value.empty? - false - else - true - end - end - end - - if filled_fields.include? true - false - else - true - end + return !table.keys.any?{|key| ignored_fields.include?(key) ? false : !table[key].empty?} end end diff --git a/src/noosfero-spb/software_communities/lib/software_info.rb b/src/noosfero-spb/software_communities/lib/software_info.rb index ffe5364..af080a7 100644 --- a/src/noosfero-spb/software_communities/lib/software_info.rb +++ b/src/noosfero-spb/software_communities/lib/software_info.rb @@ -84,7 +84,7 @@ class SoftwareInfo < ActiveRecord::Base validates_length_of :finality, :maximum => 120 validates_length_of :objectives, :maximum => 4000 validates_length_of :features, :maximum => 4000 - validates_presence_of :finality + validates_presence_of :finality, :community validate :validate_acronym @@ -166,13 +166,16 @@ class SoftwareInfo < ActiveRecord::Base another_license_link = attributes.delete(:another_license_link) software_info = SoftwareInfo.new(attributes) - if !environment.admins.include? requestor + unless environment.admins.include? requestor CreateSoftware.create!( attributes.merge( :requestor => requestor, :environment => environment, :name => name, - :license_info => license_info + :identifier => identifier, + :license_info => license_info, + :another_license_version => another_license_version, + :another_license_link => another_license_link ) ) else @@ -189,15 +192,15 @@ class SoftwareInfo < ActiveRecord::Base community.template_id = software_template.id end - software_info.license_info = license_info - software_info.save - community.software_info = software_info community.save! community.add_admin(requestor) + + software_info.community = community + software_info.license_info = license_info + software_info.verify_license_info(another_license_version, another_license_link) + software_info.save! end - software_info.verify_license_info(another_license_version, another_license_link) - software_info.save! software_info end diff --git a/src/noosfero-spb/software_communities/lib/software_language_helper.rb b/src/noosfero-spb/software_communities/lib/software_language_helper.rb index 296088d..38ba6e1 100644 --- a/src/noosfero-spb/software_communities/lib/software_language_helper.rb +++ b/src/noosfero-spb/software_communities/lib/software_language_helper.rb @@ -50,10 +50,9 @@ class SoftwareLanguageHelper < DynamicTableHelper def self.language_html_structure(language_data, disabled) language_id = language_data[:programming_language_id] - language_name = if language_data[:programming_language_id].blank? - "" - else - ProgrammingLanguage.find( + language_name = "" + unless language_data[:programming_language_id].blank? + language_name = ProgrammingLanguage.find( language_data[:programming_language_id], :select=>"name" ).name diff --git a/src/noosfero-spb/software_communities/test/functional/software_communities_plugin_myprofile_controller_test.rb b/src/noosfero-spb/software_communities/test/functional/software_communities_plugin_myprofile_controller_test.rb index 6c8a70a..c37616b 100644 --- a/src/noosfero-spb/software_communities/test/functional/software_communities_plugin_myprofile_controller_test.rb +++ b/src/noosfero-spb/software_communities/test/functional/software_communities_plugin_myprofile_controller_test.rb @@ -203,9 +203,100 @@ class SoftwareCommunitiesPluginMyprofileControllerTest < ActionController::TestC :profile => @person.identifier ) - assert_equal SoftwareInfo.last.license_info_id, license_another.id - assert_equal SoftwareInfo.last.license_info.id, nil - assert_equal SoftwareInfo.last.license_info.version, another_license_version - assert_equal SoftwareInfo.last.license_info.link, another_license_link + assert_equal license_another.id, SoftwareInfo.last.license_info_id + assert_equal nil, SoftwareInfo.last.license_info.id + assert_equal another_license_version, SoftwareInfo.last.license_info.version + assert_equal another_license_link, SoftwareInfo.last.license_info.link end + + should "create software_info after finish task with 'Another' license_info" do + license_another = LicenseInfo.create(:version => "Another", :link => "#") + + another_license_version = "Different License" + another_license_link = "http://diferent.link" + + post( + :new_software, + :community => { :name => "New Software", :identifier => "new-software" }, + :software_info => { :finality => "something", :repository_link => "" }, + :license => { :license_infos_id => license_another.id, + :version => another_license_version, + :link=> another_license_link + }, + :profile => @person.identifier + ) + + @environment.add_admin(@person) + Task.last.send('finish', @person) + + assert_equal license_another.id, SoftwareInfo.last.license_info_id + assert_equal nil, SoftwareInfo.last.license_info.id + assert_equal another_license_version, SoftwareInfo.last.license_info.version + assert_equal another_license_link, SoftwareInfo.last.license_info.link + end + + should "show error messages on create software_info" do + post( + :new_software, + :community => {}, + :software_info => {}, + :license => {}, + :profile => @person.identifier + ) + assert_includes @response.body, "Domain can't be blank" + assert_includes @response.body, "Name can't be blank" + assert_includes @response.body, "Finality can't be blank" + assert_includes @response.body, "Version can't be blank" + end + + should "show domain not available error" do + @environment.add_admin(@person) + + post( + :new_software, + :community => {:name =>"New Software", :identifier => "new-software"}, + :software_info => {:finality => "something", :repository_link => ""}, + :license =>{:license_infos_id => LicenseInfo.last.id}, + :profile => @person.identifier + ) + post( + :new_software, + :community => {:name =>"New Software", :identifier => "new-software"}, + :software_info => {:finality => "something", :repository_link => ""}, + :license =>{:license_infos_id => LicenseInfo.last.id}, + :profile => @person.identifier + ) + + assert_includes @response.body, "Domain is not available" + end + + should "create software with admin moderation" do + @environment.enable('admin_must_approve_new_communities') + + post( + :new_software, + :community => {:name =>"New Software", :identifier => "new-software"}, + :software_info => {:finality => "something", :repository_link => ""}, + :license =>{:license_infos_id => LicenseInfo.last.id}, + :profile => @person.identifier + ) + + @environment.add_admin(@person) + Task.last.send('finish', @person) + + assert_equal "New Software", Task.last.data[:name] + assert_equal "New Software", SoftwareInfo.last.community.name + end + + should "dont create software without accept task" do + assert_no_difference 'SoftwareInfo.count' do + post( + :new_software, + :community => {:name =>"New Software", :identifier => "new-software"}, + :software_info => {:finality => "something", :repository_link => ""}, + :license =>{:license_infos_id => LicenseInfo.last.id}, + :profile => @person.identifier + ) + end + end end diff --git a/src/noosfero-spb/software_communities/test/unit/software_info_validation_test.rb b/src/noosfero-spb/software_communities/test/unit/software_info_validation_test.rb index e42bf9b..b5324a6 100644 --- a/src/noosfero-spb/software_communities/test/unit/software_info_validation_test.rb +++ b/src/noosfero-spb/software_communities/test/unit/software_info_validation_test.rb @@ -49,6 +49,7 @@ class SoftwareInfoValidationTest < ActiveSupport::TestCase @software_info.features = "Do a lot of things" @software_info.objectives = "All tests should pass !" + @software_info.community = @community end def teardown diff --git a/src/noosfero-spb/software_communities/views/_main_software_editor_extras.html.erb b/src/noosfero-spb/software_communities/views/_main_software_editor_extras.html.erb deleted file mode 100644 index 942256d..0000000 --- a/src/noosfero-spb/software_communities/views/_main_software_editor_extras.html.erb +++ /dev/null @@ -1,32 +0,0 @@ -

<%= _('Software Information') %>

- -<%= label_tag("name", _('Name'), {:class => 'formlabel'}) %> - -
- <%= context.profile.environment.default_hostname %>/ - <%= text_field_tag(:name, context.profile.software_info.community.name) %> -
- -

<%= _("Finality") %>

-
- <%= text_field_tag(:finality, context.profile.software_info.finality) %> -
- -

<%= _("Licenses") %>

-
- <%= select_tag(:id, options_for_select(LicenseHelper.getListLicenses.collect{|l| [l.version, l.id]}, :selected => context.profile.software_info.license_info.id), :onchange => "get_license_link('version')") %> -
- -

<%= _("License link") %>

- <% LicenseHelper.getListLicenses.each do | license | %> - - <% end %> - - <%= context.profile.software_info.license_info.link %> -
- -
- <%= label_tag "repository_url", _("Link to Repository: ") %> - <%= text_field_tag(:reository_url) %> -
- diff --git a/src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_license_info_fields.html.erb b/src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_license_info_fields.html.erb index 3cde616..d706b46 100644 --- a/src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_license_info_fields.html.erb +++ b/src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_license_info_fields.html.erb @@ -1,7 +1,3 @@ -<% LicenseHelper.getListLicenses.each do | license | %> - -<% end %> - <%= text_field_tag "license_info[version]", license_version, :id=>"license_info_version", :class=>"license_info_version", :placeholder=>_('Autocomplete field, type some license') %> <%= hidden_field_tag "license[license_infos_id]", license_id, :id=>"license_info_id", :class=>"license_info_id", :data => {:label=>license_version} %> diff --git a/src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_main_software_editor_extras.html.erb b/src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_main_software_editor_extras.html.erb index a40b62d..45edfc1 100644 --- a/src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_main_software_editor_extras.html.erb +++ b/src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_main_software_editor_extras.html.erb @@ -5,7 +5,7 @@
> - <%= label_tag("software[acronym]", _('Short Name'), {:class => 'formlabel mandatory'}) %> + <%= label_tag("software[acronym]", _('Short Name'), {:class => 'formlabel'}) %> <%= text_field_tag("software[acronym]", @profile.software_info.acronym, :id => 'software_acronym_id', :maxlength=>"10") %>
-- libgit2 0.21.2