From da80901859490132a5657754dd7cff6fc370ebdc Mon Sep 17 00:00:00 2001 From: Fabio Teixeira Date: Thu, 11 Dec 2014 17:13:23 -0200 Subject: [PATCH] Refactor InstitutionBlock --- controllers/mpog_software_plugin_controller.rb | 2 -- lib/institutions_block.rb | 48 ++++++++++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/controllers/mpog_software_plugin_controller.rb b/controllers/mpog_software_plugin_controller.rb index a2554bc..2113bab 100644 --- a/controllers/mpog_software_plugin_controller.rb +++ b/controllers/mpog_software_plugin_controller.rb @@ -138,8 +138,6 @@ class MpogSoftwarePluginController < ApplicationController protected def get_state_list - redirect_to "/" unless request.xhr? - NationalRegion.find( :all, :conditions=>["national_region_type_id = ?", 2], diff --git a/lib/institutions_block.rb b/lib/institutions_block.rb index da31721..8ef8f59 100644 --- a/lib/institutions_block.rb +++ b/lib/institutions_block.rb @@ -21,11 +21,14 @@ class InstitutionsBlock < CommunitiesBlock case owner when Profile lambda do |context| - link_to s_('institutions|View all'), :profile => owner.identifier, :controller => 'profile', :action => 'communities', :type => 'Institution' + 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' + link_to s_('institutions|View all'), :controller => 'search', + :action => 'communities', :type => 'Institution' end else '' @@ -33,25 +36,9 @@ class InstitutionsBlock < CommunitiesBlock end def profile_list - result = nil - visible_profiles = profiles.visible.includes([:image,:domains,:preferred_domain,:environment]) - if !prioritize_profiles_with_image - result = visible_profiles.all(:limit => get_limit, :order => 'profiles.updated_at DESC').sort_by{ rand } - elsif profiles.visible.with_image.count >= get_limit - result = visible_profiles.with_image.all(:limit => get_limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand } - else - result = 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 - - communities_with_institution_list = [] - - result.each do |r| - if r.class == Community and r.institution? - communities_with_institution_list << r - end - end + result = get_visible_profiles - result = communities_with_institution_list + result = result.select { |p| p.class == Community && p.institution? } result.slice(0..get_limit-1) end @@ -60,4 +47,25 @@ class InstitutionsBlock < CommunitiesBlock 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 -- libgit2 0.21.2