diff --git a/lib/software_communities_plugin.rb b/lib/software_communities_plugin.rb index 9340535..07ad801 100644 --- a/lib/software_communities_plugin.rb +++ b/lib/software_communities_plugin.rb @@ -40,7 +40,8 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin CategoriesAndTagsBlock => { :type => [Community] }, CategoriesSoftwareBlock => { :type => [Environment] }, SearchCatalogBlock => { :type => [Environment] }, - SoftwareHighlightsBlock => { :type => [Environment] } + SoftwareHighlightsBlock => { :type => [Environment] }, + SoftwareTabDataBlock => {:type => [Community], :position => 1} } end diff --git a/lib/software_tab_data_block.rb b/lib/software_tab_data_block.rb new file mode 100644 index 0000000..b32eeb5 --- /dev/null +++ b/lib/software_tab_data_block.rb @@ -0,0 +1,48 @@ +class SoftwareTabDataBlock < Block + attr_accessible :show_name, :displayed_blog + + settings_items :show_name, :type => :boolean, :default => false + settings_items :displayed_blog, :type => :integer, :default => 0 + + TOTAL_POSTS_DYSPLAYED = 5 + + def self.description + _('Software Tab Data') + end + + def help + _('This block is used by colab to insert data into Noosfero') + end + + def content(args={}) + block = self + + lambda do |object| + render( + :file => 'blocks/software_tab_data', + :locals => { + :block => block + } + ) + end + end + + def blogs + self.owner.blogs + end + + def actual_blog + # As :displayed_blog default value is 0, it falls to the first one + blogs.find_by_id(self.displayed_blog) || blogs.first + end + + def posts + blog = actual_blog + + if blog and (not blog.posts.empty?) + blog.posts.limit(TOTAL_POSTS_DYSPLAYED) + else + [] + end + end +end diff --git a/test/unit/software_tab_data_block_test.rb b/test/unit/software_tab_data_block_test.rb new file mode 100644 index 0000000..aad8c8c --- /dev/null +++ b/test/unit/software_tab_data_block_test.rb @@ -0,0 +1,66 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../helpers/plugin_test_helper' + +class SoftwareTabDataBlockTest < ActiveSupport::TestCase + include PluginTestHelper + + def setup + @software_info = create_software_info("A new Software") + @software_info.save! + + @soft_community = @software_info.community + + @soft_community.blogs << Blog.new(:name=>"First blog") + @soft_community.blogs << Blog.new(:name=>"Second blog") + @soft_community.save! + + SoftwareTabDataBlock.any_instance.stubs(:owner).returns(@soft_community) + end + + should "get its owner blogs" do + assert_equal @soft_community.blogs, SoftwareTabDataBlock.new.blogs + end + + should "actual_blog get the first blog if it is not defined" do + assert_equal @soft_community.blogs.first, SoftwareTabDataBlock.new.actual_blog + end + + should "actual_blog get the defined community blog" do + last_blog = @soft_community.blogs.last + soft_tab_data = create_software_tab_data_block(last_blog) + + assert_equal last_blog, soft_tab_data.actual_blog + end + + should "get the actual_blog posts" do + last_blog = @soft_community.blogs.last + soft_tab_data = create_software_tab_data_block(last_blog) + craete_sample_posts(last_blog, 2) + + assert_equal last_blog.posts.first.id, soft_tab_data.posts.first.id + assert_equal last_blog.posts.last.id, soft_tab_data.posts.last.id + end + + should "limit the number of posts" do + last_blog = @soft_community.blogs.last + soft_tab_data = create_software_tab_data_block(last_blog) + craete_sample_posts(last_blog, 6) + + assert_equal SoftwareTabDataBlock::TOTAL_POSTS_DYSPLAYED, soft_tab_data.posts.count + end + + private + + def create_software_tab_data_block blog + soft_tab_data = SoftwareTabDataBlock.new + soft_tab_data.displayed_blog = blog.id + soft_tab_data + end + + def craete_sample_posts blog, quantity=1 + quantity.times do |number| + TinyMceArticle.create! :name=>"Simple post #{number}", :body=>"Simple post #{number}", + :parent=> blog, :profile=>@soft_community + end + end +end diff --git a/views/blocks/_software_tab_blog.html.erb b/views/blocks/_software_tab_blog.html.erb new file mode 100644 index 0000000..cee9f19 --- /dev/null +++ b/views/blocks/_software_tab_blog.html.erb @@ -0,0 +1,17 @@ +