diff --git a/app/models/blog.rb b/app/models/blog.rb
index 7a17124..c94763d 100644
--- a/app/models/blog.rb
+++ b/app/models/blog.rb
@@ -93,4 +93,20 @@ class Blog < Folder
posts.where("type != 'RssFeed'").order(:updated_at).limit(limit)
end
+ def total_number_of_posts(group_by, year = nil)
+ case group_by
+ when :by_year
+ posts.published.native_translations
+ .except(:order)
+ .count(:all, :group => 'EXTRACT(YEAR FROM published_at)')
+ .sort_by {|year, count| -year.to_i}
+ when :by_month
+ posts.published.native_translations
+ .except(:order)
+ .where('EXTRACT(YEAR FROM published_at)=?', year.to_i)
+ .group('EXTRACT(MONTH FROM published_at)')
+ .count
+ .sort_by {|month, count| -month.to_i}
+ end
+ end
end
diff --git a/app/models/blog_archives_block.rb b/app/models/blog_archives_block.rb
index d36ddad..00713a8 100644
--- a/app/models/blog_archives_block.rb
+++ b/app/models/blog_archives_block.rb
@@ -21,28 +21,12 @@ class BlogArchivesBlock < Block
blog_id && owner.blogs.exists?(blog_id) ? owner.blogs.find(blog_id) : owner.blog
end
- def visible_posts(person)
- #FIXME Performance issues with display_to. Must convert it to a scope.
- # Checkout this page for further information: http://noosfero.org/Development/ActionItem2705
- blog.posts.published.native_translations #.select {|post| post.display_to?(person)}
- end
-
def content(args={})
- owner_blog = self.blog
- return nil unless owner_blog
- results = ''
- posts = visible_posts(args[:person])
- posts.except(:order).count(:all, :group => 'EXTRACT(YEAR FROM published_at)').sort_by {|year, count| -year.to_i}.each do |year, count|
- results << content_tag('li', content_tag('strong', "#{year.to_i} (#{count})"))
- results << "
"
- posts.except(:order).where('EXTRACT(YEAR FROM published_at)=?', year.to_i).group('EXTRACT(MONTH FROM published_at)').count.sort_by {|month, count| -month.to_i}.each do |month, count|
- results << content_tag('li', link_to("#{month_name(month.to_i)} (#{count})", owner_blog.url.merge(year: year.to_i, month: month.to_i)))
- end
- results << "
"
+ return nil unless self.blog
+ block = self
+ proc do
+ render :file => 'blocks/blog_archives', :locals => { :block => block }
end
- block_title(title) +
- content_tag('ul', results, :class => 'blog-archives') +
- content_tag('div', link_to(_('Subscribe RSS Feed'), owner_blog.feed.url), :class => 'subscribe-feed')
end
def self.expire_on
diff --git a/app/views/blocks/blog_archives.html.erb b/app/views/blocks/blog_archives.html.erb
new file mode 100644
index 0000000..342e848
--- /dev/null
+++ b/app/views/blocks/blog_archives.html.erb
@@ -0,0 +1,14 @@
+<%= block_title(block.title) %>
+
+
+ <% block.blog.total_number_of_posts(:by_year).each do |year, count| %>
+ <%= content_tag('li', content_tag('strong', "#{year.to_i} (#{count})")) %>
+
+ <% block.blog.total_number_of_posts(:by_month, year).each do |month, count| %>
+ <%= content_tag('li', link_to("#{month_name(month.to_i)} (#{count})", block.blog.url.merge(year: year.to_i, month: month.to_i))) %>
+ <% end %>
+
+ <% end %>
+
+
+<%= content_tag('div', link_to(_('Subscribe RSS Feed'), block.blog.feed.url), :class => 'subscribe-feed') %>
diff --git a/test/unit/blog_test.rb b/test/unit/blog_test.rb
index 6d14222..779935e 100644
--- a/test/unit/blog_test.rb
+++ b/test/unit/blog_test.rb
@@ -266,4 +266,23 @@ class BlogTest < ActiveSupport::TestCase
assert_equal blog.image(true).filename, 'noosfero-network.png'
end
+
+ should 'count total number of posts by year' do
+ p = create_user('testuser').person
+ blog = fast_create(Blog, :profile_id => p.id, :name => 'Blog test')
+ create(TextileArticle, :name => 'Post 1', :parent => blog, :profile => p, :published_at => DateTime.parse('16-08-2010'))
+ create(TextileArticle, :name => 'Post 2', :parent => blog, :profile => p, :published_at => DateTime.parse('17-08-2010'))
+ create(TextileArticle, :name => 'Post 3', :parent => blog, :profile => p, :published_at => DateTime.parse('10-05-2012'))
+ assert_equal [[2012.0, 1], [2010.0, 2]], blog.total_number_of_posts(:by_year)
+ end
+
+ should 'count total number of posts by month' do
+ p = create_user('testuser').person
+ blog = fast_create(Blog, :profile_id => p.id, :name => 'Blog test')
+ create(TextileArticle, :name => 'Post 1', :parent => blog, :profile => p, :published_at => DateTime.parse('16-08-2010'))
+ create(TextileArticle, :name => 'Post 2', :parent => blog, :profile => p, :published_at => DateTime.parse('17-08-2010'))
+ create(TextileArticle, :name => 'Post 3', :parent => blog, :profile => p, :published_at => DateTime.parse('11-10-2010'))
+ assert_equal [[10.0, 1], [8.0, 2]], blog.total_number_of_posts(:by_month, 2010)
+ end
+
end
--
libgit2 0.21.2