From 1c950250bb0f77c394c6feb87a86306a73a76974 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Sun, 16 Mar 2014 22:35:22 -0300 Subject: [PATCH] Revert part of the last commit in order to not remove the EnvironmentStatisticsBlock --- app/controllers/admin/environment_design_controller.rb | 4 +++- app/models/box.rb | 6 ++++++ app/models/environment.rb | 3 +++ app/models/environment_statistics_block.rb | 33 +++++++++++++++++++++++++++++++++ app/sweepers/profile_sweeper.rb | 10 ++++++++++ db/migrate/20140316171151_remove_environment_statistics_block.rb | 9 --------- test/functional/environment_design_controller_test.rb | 16 +++++++++++++++- test/unit/box_test.rb | 6 ++++++ test/unit/environment_statistics_block_test.rb | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 175 insertions(+), 11 deletions(-) create mode 100644 app/models/environment_statistics_block.rb delete mode 100644 db/migrate/20140316171151_remove_environment_statistics_block.rb create mode 100644 test/unit/environment_statistics_block_test.rb diff --git a/app/controllers/admin/environment_design_controller.rb b/app/controllers/admin/environment_design_controller.rb index 863fb64..da2e9b7 100644 --- a/app/controllers/admin/environment_design_controller.rb +++ b/app/controllers/admin/environment_design_controller.rb @@ -3,7 +3,9 @@ class EnvironmentDesignController < BoxOrganizerController protect 'edit_environment_design', :environment def available_blocks - @available_blocks ||= [ ArticleBlock, LoginBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, FeaturedProductsBlock, CategoriesBlock, RawHTMLBlock, TagsBlock ] + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + @available_blocks ||= [ ArticleBlock, LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, FeaturedProductsBlock, CategoriesBlock, RawHTMLBlock, TagsBlock ] @available_blocks += plugins.dispatch(:extra_blocks, :type => Environment) end diff --git a/app/models/box.rb b/app/models/box.rb index db104dc..e4b1c85 100644 --- a/app/models/box.rb +++ b/app/models/box.rb @@ -26,6 +26,9 @@ class Box < ActiveRecord::Base CategoriesBlock, CommunitiesBlock, EnterprisesBlock, + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + EnvironmentStatisticsBlock, FansBlock, FavoriteEnterprisesBlock, FeedReaderBlock, @@ -51,6 +54,9 @@ class Box < ActiveRecord::Base CommunitiesBlock, DisabledEnterpriseMessageBlock, EnterprisesBlock, + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + EnvironmentStatisticsBlock, FansBlock, FavoriteEnterprisesBlock, FeaturedProductsBlock, diff --git a/app/models/environment.rb b/app/models/environment.rb index c86a308..7c9de54 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -161,6 +161,9 @@ class Environment < ActiveRecord::Base # "left" area env.boxes[1].blocks << LoginBlock.new + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + env.boxes[1].blocks << EnvironmentStatisticsBlock.new env.boxes[1].blocks << RecentDocumentsBlock.new # "right" area diff --git a/app/models/environment_statistics_block.rb b/app/models/environment_statistics_block.rb new file mode 100644 index 0000000..d5ea2ea --- /dev/null +++ b/app/models/environment_statistics_block.rb @@ -0,0 +1,33 @@ +# TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from +# the Noosfero core soon, see ActionItem3045 + +class EnvironmentStatisticsBlock < Block + + def self.description + _('Environment stastistics (DEPRECATED)') + end + + def default_title + _('Statistics for %s') % owner.name + end + + def help + _('This block presents some statistics about your environment.') + end + + def content(args={}) + users = owner.people.visible.count + enterprises = owner.enterprises.visible.count + communities = owner.communities.visible.count + + info = [] + info << (n_('One user', '%{num} users', users) % { :num => users }) + unless owner.enabled?('disable_asset_enterprises') + info << (n__('One enterprise', '%{num} enterprises', enterprises) % { :num => enterprises }) + end + info << (n__('One community', '%{num} communities', communities) % { :num => communities }) + + block_title(title) + content_tag('ul', info.map {|item| content_tag('li', item) }.join("\n")) + end + +end diff --git a/app/sweepers/profile_sweeper.rb b/app/sweepers/profile_sweeper.rb index 454b593..e23f9b4 100644 --- a/app/sweepers/profile_sweeper.rb +++ b/app/sweepers/profile_sweeper.rb @@ -8,6 +8,9 @@ class ProfileSweeper # < ActiveRecord::Observer end def after_create(profile) + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + expire_statistics_block_cache(profile) end protected @@ -28,6 +31,13 @@ protected expire_blogs(profile) if profile.organization? end + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + def expire_statistics_block_cache(profile) + blocks = profile.environment.blocks.select { |b| b.kind_of?(EnvironmentStatisticsBlock) } + BlockSweeper.expire_blocks(blocks) + end + def expire_blogs(profile) profile.blogs.select{|b| !b.empty?}.each do |blog| pages = blog.posts.count / blog.posts_per_page + 1 diff --git a/db/migrate/20140316171151_remove_environment_statistics_block.rb b/db/migrate/20140316171151_remove_environment_statistics_block.rb deleted file mode 100644 index f402729..0000000 --- a/db/migrate/20140316171151_remove_environment_statistics_block.rb +++ /dev/null @@ -1,9 +0,0 @@ -class RemoveEnvironmentStatisticsBlock < ActiveRecord::Migration - def self.up - update("DELETE FROM blocks WHERE type = 'EnvironmentStatisticsBlock'") - end - - def self.down - say("Nothing to undo (cannot recover the data)") - end -end diff --git a/test/functional/environment_design_controller_test.rb b/test/functional/environment_design_controller_test.rb index cc80649..e0828ec 100644 --- a/test/functional/environment_design_controller_test.rb +++ b/test/functional/environment_design_controller_test.rb @@ -6,7 +6,9 @@ class EnvironmentDesignController; def rescue_action(e) raise e end; end class EnvironmentDesignControllerTest < ActionController::TestCase - ALL_BLOCKS = [ArticleBlock, LoginBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, FeaturedProductsBlock, CategoriesBlock, RawHTMLBlock, TagsBlock ] + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + ALL_BLOCKS = [ArticleBlock, LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, FeaturedProductsBlock, CategoriesBlock, RawHTMLBlock, TagsBlock ] def setup @controller = EnvironmentDesignController.new @@ -83,6 +85,18 @@ class EnvironmentDesignControllerTest < ActionController::TestCase assert_tag :tag => 'p', :attributes => { :id => 'no_portal_community' } end + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + should 'be able to edit EnvironmentStatisticsBlock' do + login_as(create_admin_user(Environment.default)) + b = EnvironmentStatisticsBlock.create! + e = Environment.default + e.boxes.create! + e.boxes.first.blocks << b + get :edit, :id => b.id + assert_tag :tag => 'input', :attributes => { :id => 'block_title' } + end + should 'be able to edit EnterprisesBlock' do login_as(create_admin_user(Environment.default)) b = EnterprisesBlock.create! diff --git a/test/unit/box_test.rb b/test/unit/box_test.rb index a1bf07f..cb5b903 100644 --- a/test/unit/box_test.rb +++ b/test/unit/box_test.rb @@ -33,6 +33,9 @@ class BoxTest < ActiveSupport::TestCase assert blocks.include?('categories-block') assert blocks.include?('communities-block') assert blocks.include?('enterprises-block') + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + assert blocks.include?('environment-statistics-block') assert blocks.include?('fans-block') assert blocks.include?('favorite-enterprises-block') assert blocks.include?('feed-reader-block') @@ -64,6 +67,9 @@ class BoxTest < ActiveSupport::TestCase assert blocks.include?('communities-block') assert blocks.include?('disabled-enterprise-message-block') assert blocks.include?('enterprises-block') + # TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from + # the Noosfero core soon, see ActionItem3045 + assert blocks.include?('environment-statistics-block') assert blocks.include?('fans-block') assert blocks.include?('favorite-enterprises-block') assert blocks.include?('featured-products-block') diff --git a/test/unit/environment_statistics_block_test.rb b/test/unit/environment_statistics_block_test.rb new file mode 100644 index 0000000..53d740e --- /dev/null +++ b/test/unit/environment_statistics_block_test.rb @@ -0,0 +1,99 @@ +# TODO EnvironmentStatisticsBlock is DEPRECATED and will be removed from +# the Noosfero core soon, see ActionItem3045 + +require File.dirname(__FILE__) + '/../test_helper' + +class EnvironmentStatisticsBlockTest < ActiveSupport::TestCase + + should 'inherit from Block' do + assert_kind_of Block, EnvironmentStatisticsBlock.new + end + + should 'describe itself' do + assert_not_equal Block.description, EnvironmentStatisticsBlock.description + end + + should 'provide a default title' do + owner = mock + owner.expects(:name).returns('my environment') + + block = EnvironmentStatisticsBlock.new + block.expects(:owner).returns(owner) + assert_equal 'Statistics for my environment', block.title + end + + should 'generate statistics' do + env = create(Environment) + user1 = create_user('testuser1', :environment_id => env.id) + user2 = create_user('testuser2', :environment_id => env.id) + + fast_create(Enterprise, :environment_id => env.id) + fast_create(Community, :environment_id => env.id) + + block = EnvironmentStatisticsBlock.new + env.boxes.first.blocks << block + + content = block.content + + assert_match(/One enterprise/, content) + assert_match(/2 users/, content) + assert_match(/One community/, content) + end + + should 'generate statistics including private profiles' do + env = create(Environment) + user1 = create_user('testuser1', :environment_id => env.id) + user2 = create_user('testuser2', :environment_id => env.id) + user3 = create_user('testuser3', :environment_id => env.id) + p = user3.person; p.public_profile = false; p.save! + + fast_create(Enterprise, :environment_id => env.id) + fast_create(Enterprise, :environment_id => env.id, :public_profile => false) + + fast_create(Community, :environment_id => env.id) + fast_create(Community, :environment_id => env.id, :public_profile => false) + + block = EnvironmentStatisticsBlock.new + env.boxes.first.blocks << block + + content = block.content + + assert_match /2 enterprises/, content + assert_match /3 users/, content + assert_match /2 communities/, content + end + + should 'generate statistics but not for not visible profiles' do + env = create(Environment) + user1 = create_user('testuser1', :environment_id => env.id) + user2 = create_user('testuser2', :environment_id => env.id) + user3 = create_user('testuser3', :environment_id => env.id) + p = user3.person; p.visible = false; p.save! + + fast_create(Enterprise, :environment_id => env.id) + fast_create(Enterprise, :environment_id => env.id, :visible => false) + + fast_create(Community, :environment_id => env.id) + fast_create(Community, :environment_id => env.id, :visible => false) + + block = EnvironmentStatisticsBlock.new + env.boxes.first.blocks << block + + content = block.content + + assert_match /One enterprise/, content + assert_match /2 users/, content + assert_match /One community/, content + end + + should 'not display enterprises if disabled' do + env = fast_create(Environment) + env.enable('disable_asset_enterprises', false) + + block = EnvironmentStatisticsBlock.new + block.stubs(:owner).returns(env) + + assert_no_match /enterprises/i, block.content + end + +end -- libgit2 0.21.2