Commit 449ea30f10f1a8bab798638d441e750f400d05a5
1 parent
89830abf
Exists in
gov-user-refactoring
gov-user: make communities_block extensions through proper hotspots
Closes #408
Showing
6 changed files
with
83 additions
and
117 deletions
Show diff stats
src/noosfero-spb/gov_user/lib/ext/communities_block.rb
| ... | ... | @@ -1,45 +0,0 @@ |
| 1 | -require_dependency 'communities_block' | |
| 2 | - | |
| 3 | -class CommunitiesBlock | |
| 4 | - | |
| 5 | - def profile_list | |
| 6 | - result = get_visible_profiles | |
| 7 | - result.slice(0..get_limit-1) | |
| 8 | - end | |
| 9 | - | |
| 10 | - def profile_count | |
| 11 | - profile_list.count | |
| 12 | - end | |
| 13 | - | |
| 14 | - private | |
| 15 | - | |
| 16 | - def get_visible_profiles | |
| 17 | - visible_profiles = profiles.visible.includes( | |
| 18 | - [:image,:domains,:preferred_domain,:environment] | |
| 19 | - ) | |
| 20 | - | |
| 21 | - delete_communities = [] | |
| 22 | - valid_communities_string = Community.get_valid_communities_string | |
| 23 | - Community.all.each{|community| delete_communities << community.id unless eval(valid_communities_string)} | |
| 24 | - | |
| 25 | - visible_profiles = visible_profiles.where(["profiles.id NOT IN (?)", delete_communities]) unless delete_communities.empty? | |
| 26 | - | |
| 27 | - if !prioritize_profiles_with_image | |
| 28 | - return visible_profiles.all( | |
| 29 | - :limit => get_limit, | |
| 30 | - :order => 'profiles.updated_at DESC' | |
| 31 | - ).sort_by {rand} | |
| 32 | - elsif profiles.visible.with_image.count >= get_limit | |
| 33 | - return visible_profiles.with_image.all( | |
| 34 | - :limit => get_limit * 5, | |
| 35 | - :order => 'profiles.updated_at DESC' | |
| 36 | - ).sort_by {rand} | |
| 37 | - else | |
| 38 | - visible_profiles = visible_profiles.with_image.sort_by {rand} + | |
| 39 | - visible_profiles.without_image.all( | |
| 40 | - :limit => get_limit * 5, :order => 'profiles.updated_at DESC' | |
| 41 | - ).sort_by {rand} | |
| 42 | - return visible_profiles | |
| 43 | - end | |
| 44 | - end | |
| 45 | -end |
src/noosfero-spb/gov_user/lib/ext/community.rb
| ... | ... | @@ -6,20 +6,4 @@ class Community |
| 6 | 6 | def institution? |
| 7 | 7 | return !institution.nil? |
| 8 | 8 | end |
| 9 | - | |
| 10 | - def remove_of_community_search_institution? | |
| 11 | - return institution? | |
| 12 | - end | |
| 13 | - | |
| 14 | - def self.get_valid_communities_string | |
| 15 | - remove_of_communities_methods = Community.instance_methods.select{|m| m =~ /remove_of_community_search/} | |
| 16 | - valid_communities_string = "!(" | |
| 17 | - remove_of_communities_methods.each do |method| | |
| 18 | - valid_communities_string += "community.send('#{method}') || " | |
| 19 | - end | |
| 20 | - valid_communities_string = valid_communities_string[0..-5] | |
| 21 | - valid_communities_string += ")" | |
| 22 | - | |
| 23 | - valid_communities_string | |
| 24 | - end | |
| 25 | 9 | end | ... | ... |
src/noosfero-spb/gov_user/lib/gov_user_plugin.rb
| ... | ... | @@ -252,18 +252,10 @@ class GovUserPlugin < Noosfero::Plugin |
| 252 | 252 | end |
| 253 | 253 | |
| 254 | 254 | def filter_search_scope(scope, asset) |
| 255 | - # Select only communities that are not related to any institution. | |
| 256 | 255 | if asset.to_s == 'communities' |
| 257 | - scope = scope.joins('LEFT OUTER JOIN gov_user_plugin_institutions as institutions | |
| 258 | - ON profiles.id = institutions.community_id'). | |
| 259 | - where('institutions.community_id IS NULL') | |
| 260 | - return [scope, asset] | |
| 261 | - # Select only communities that are related to an institution. | |
| 256 | + return [filter_communities(scope), asset] | |
| 262 | 257 | elsif asset.to_s == 'gov_user_plugin/institutions' |
| 263 | - scope = scope.joins('INNER JOIN gov_user_plugin_institutions as institutions | |
| 264 | - ON profiles.id = institutions.community_id') | |
| 265 | - return [scope, asset] | |
| 266 | - # Go with the flow. | |
| 258 | + return [filter_institutions(scope), asset] | |
| 267 | 259 | else |
| 268 | 260 | return [scope, asset] |
| 269 | 261 | end |
| ... | ... | @@ -282,6 +274,16 @@ class GovUserPlugin < Noosfero::Plugin |
| 282 | 274 | [{:name => 'institution', :block => block, :common_profile_list_block => class_name_underscored}] |
| 283 | 275 | end |
| 284 | 276 | |
| 277 | + def filter_profile_list_block_scope(scope, klass) | |
| 278 | + if klass == CommunitiesBlock | |
| 279 | + return [filter_communities(scope), klass] | |
| 280 | + elsif klass == GovUserPlugin::InstitutionsBlock | |
| 281 | + return [filter_institutions(scope), klass] | |
| 282 | + else | |
| 283 | + return [scope, klass] | |
| 284 | + end | |
| 285 | + end | |
| 286 | + | |
| 285 | 287 | private |
| 286 | 288 | |
| 287 | 289 | def call_model_transaction(model,name) |
| ... | ... | @@ -349,15 +351,28 @@ class GovUserPlugin < Noosfero::Plugin |
| 349 | 351 | } |
| 350 | 352 | end |
| 351 | 353 | |
| 352 | - def update_user_institutions(user) | |
| 353 | - context.params[:user][:institution_ids].each do |institution_id| | |
| 354 | - institution = GovUserPlugin::Institution.find institution_id | |
| 355 | - user.institutions << institution | |
| 354 | + def update_user_institutions(user) | |
| 355 | + context.params[:user][:institution_ids].each do |institution_id| | |
| 356 | + institution = GovUserPlugin::Institution.find institution_id | |
| 357 | + user.institutions << institution | |
| 356 | 358 | |
| 357 | - if institution.community.admins.blank? | |
| 358 | - institution.community.add_admin(user.person) | |
| 359 | - end | |
| 359 | + if institution.community.admins.blank? | |
| 360 | + institution.community.add_admin(user.person) | |
| 360 | 361 | end |
| 361 | - user.save unless user.institution_ids.empty? | |
| 362 | 362 | end |
| 363 | + user.save unless user.institution_ids.empty? | |
| 364 | + end | |
| 365 | + | |
| 366 | + # Select only communities that are not related to any institution. | |
| 367 | + def filter_communities(scope) | |
| 368 | + scope.joins('LEFT OUTER JOIN gov_user_plugin_institutions as institutions | |
| 369 | + ON profiles.id = institutions.community_id'). | |
| 370 | + where('institutions.community_id IS NULL') | |
| 371 | + end | |
| 372 | + | |
| 373 | + # Select only communities that are related to an institution. | |
| 374 | + def filter_institutions(scope) | |
| 375 | + scope.joins('INNER JOIN gov_user_plugin_institutions as institutions | |
| 376 | + ON profiles.id = institutions.community_id') | |
| 377 | + end | |
| 363 | 378 | end | ... | ... |
src/noosfero-spb/gov_user/lib/gov_user_plugin/institutions_block.rb
| ... | ... | @@ -3,10 +3,6 @@ class GovUserPlugin::InstitutionsBlock < CommunitiesBlock |
| 3 | 3 | _('Institutions') |
| 4 | 4 | end |
| 5 | 5 | |
| 6 | - def profile_count | |
| 7 | - profile_list.count | |
| 8 | - end | |
| 9 | - | |
| 10 | 6 | def default_title |
| 11 | 7 | n_('{#} institution', '{#} institutions', profile_count) |
| 12 | 8 | end |
| ... | ... | @@ -33,38 +29,4 @@ class GovUserPlugin::InstitutionsBlock < CommunitiesBlock |
| 33 | 29 | '' |
| 34 | 30 | end |
| 35 | 31 | end |
| 36 | - | |
| 37 | - def profile_list | |
| 38 | - result = get_visible_profiles | |
| 39 | - | |
| 40 | - result = result.select { |p| p.class == Community && p.institution? } | |
| 41 | - | |
| 42 | - result.slice(0..get_limit-1) | |
| 43 | - end | |
| 44 | - | |
| 45 | - def profiles | |
| 46 | - owner.communities | |
| 47 | - end | |
| 48 | - | |
| 49 | - private | |
| 50 | - | |
| 51 | - def get_visible_profiles | |
| 52 | - include_list = [:image,:domains,:preferred_domain,:environment] | |
| 53 | - visible_profiles = profiles.visible.includes(include_list) | |
| 54 | - | |
| 55 | - if !prioritize_profiles_with_image | |
| 56 | - visible_profiles.all(:limit => get_limit, | |
| 57 | - :order => 'profiles.updated_at DESC' | |
| 58 | - ).sort_by{ rand } | |
| 59 | - elsif profiles.visible.with_image.count >= get_limit | |
| 60 | - visible_profiles.with_image.all(:limit => get_limit * 5, | |
| 61 | - :order => 'profiles.updated_at DESC' | |
| 62 | - ).sort_by{ rand } | |
| 63 | - else | |
| 64 | - visible_profiles.with_image.sort_by{ rand } + | |
| 65 | - visible_profiles.without_image.all(:limit => get_limit * 5, | |
| 66 | - :order => 'profiles.updated_at DESC' | |
| 67 | - ).sort_by{ rand } | |
| 68 | - end | |
| 69 | - end | |
| 70 | 32 | end | ... | ... |
src/noosfero-spb/gov_user/test/unit/communities_block.rb
0 → 100644
| ... | ... | @@ -0,0 +1,23 @@ |
| 1 | +require File.dirname(__FILE__) + '/../../../../test/test_helper' | |
| 2 | +require File.dirname(__FILE__) + '/../helpers/institution_test_helper' | |
| 3 | + | |
| 4 | +class CommunitiesBlockTest < ActiveSupport::TestCase | |
| 5 | + def setup | |
| 6 | + environment = Environment.default | |
| 7 | + environment.enable_plugin(GovUserPlugin) | |
| 8 | + end | |
| 9 | + | |
| 10 | + should 'not list instutitions' do | |
| 11 | + person = create_user('some-user').person | |
| 12 | + institution = InstitutionTestHelper.create_private_institution("inst name", "IN", "country", "state", | |
| 13 | + "city", "00.111.222/3333-44") | |
| 14 | + community = fast_create(Community) | |
| 15 | + institution.community.add_member(person) | |
| 16 | + community.add_member(person) | |
| 17 | + | |
| 18 | + block = CommunitiesBlock.new | |
| 19 | + block.expects(:owner).at_least_once.returns(person) | |
| 20 | + | |
| 21 | + assert_equivalent block.profiles, [community] | |
| 22 | + end | |
| 23 | +end | ... | ... |
src/noosfero-spb/gov_user/test/unit/gov_user_plugin/institutions_block_test.rb
| ... | ... | @@ -2,6 +2,11 @@ require File.dirname(__FILE__) + '/../../../../../test/test_helper' |
| 2 | 2 | require File.dirname(__FILE__) + '/../../helpers/plugin_test_helper' |
| 3 | 3 | |
| 4 | 4 | class GovUserPlugin::InstitutionsBlockTest < ActiveSupport::TestCase |
| 5 | + def setup | |
| 6 | + environment = Environment.default | |
| 7 | + environment.enable_plugin(GovUserPlugin) | |
| 8 | + end | |
| 9 | + | |
| 5 | 10 | include PluginTestHelper |
| 6 | 11 | should 'inherit from Block' do |
| 7 | 12 | assert_kind_of Block, GovUserPlugin::InstitutionsBlock.new |
| ... | ... | @@ -48,4 +53,26 @@ class GovUserPlugin::InstitutionsBlockTest < ActiveSupport::TestCase |
| 48 | 53 | assert_equivalent [institution.community], block.profiles |
| 49 | 54 | end |
| 50 | 55 | |
| 56 | + | |
| 57 | + should 'not list communities' do | |
| 58 | + user = create_person("Jose_Augusto", | |
| 59 | + "jose_augusto@email.com", | |
| 60 | + "aaaaaaa", | |
| 61 | + "aaaaaaa", | |
| 62 | + 'jose@secondary.com', | |
| 63 | + "DF", | |
| 64 | + "Gama" | |
| 65 | + ) | |
| 66 | + institution = InstitutionTestHelper.create_private_institution("inst name", "IN", "country", "state", | |
| 67 | + "city", "00.111.222/3333-44") | |
| 68 | + community = fast_create(Community) | |
| 69 | + institution.community.add_member(user) | |
| 70 | + community.add_member(user) | |
| 71 | + | |
| 72 | + block = GovUserPlugin::InstitutionsBlock.new | |
| 73 | + block.expects(:owner).at_least_once.returns(user) | |
| 74 | + | |
| 75 | + assert_equivalent block.profiles, [institution.community] | |
| 76 | + end | |
| 77 | + | |
| 51 | 78 | end | ... | ... |