Commit bc4a8cfeda8513c5b326b3ec74b25d48321ab42c
Committed by
Rafael Reggiani Manzo
1 parent
a73a2a75
Exists in
staging
and in
7 other branches
moving HTML out of BlogArchivesBlock
Showing
4 changed files
with
53 additions
and
20 deletions
Show diff stats
app/models/blog.rb
| ... | ... | @@ -93,4 +93,20 @@ class Blog < Folder |
| 93 | 93 | posts.where("type != 'RssFeed'").order(:updated_at).limit(limit) |
| 94 | 94 | end |
| 95 | 95 | |
| 96 | + def total_number_of_posts(group_by, year = nil) | |
| 97 | + case group_by | |
| 98 | + when :by_year | |
| 99 | + posts.published.native_translations | |
| 100 | + .except(:order) | |
| 101 | + .count(:all, :group => 'EXTRACT(YEAR FROM published_at)') | |
| 102 | + .sort_by {|year, count| -year.to_i} | |
| 103 | + when :by_month | |
| 104 | + posts.published.native_translations | |
| 105 | + .except(:order) | |
| 106 | + .where('EXTRACT(YEAR FROM published_at)=?', year.to_i) | |
| 107 | + .group('EXTRACT(MONTH FROM published_at)') | |
| 108 | + .count | |
| 109 | + .sort_by {|month, count| -month.to_i} | |
| 110 | + end | |
| 111 | + end | |
| 96 | 112 | end | ... | ... |
app/models/blog_archives_block.rb
| ... | ... | @@ -21,28 +21,12 @@ class BlogArchivesBlock < Block |
| 21 | 21 | blog_id && owner.blogs.exists?(blog_id) ? owner.blogs.find(blog_id) : owner.blog |
| 22 | 22 | end |
| 23 | 23 | |
| 24 | - def visible_posts(person) | |
| 25 | - #FIXME Performance issues with display_to. Must convert it to a scope. | |
| 26 | - # Checkout this page for further information: http://noosfero.org/Development/ActionItem2705 | |
| 27 | - blog.posts.published.native_translations #.select {|post| post.display_to?(person)} | |
| 28 | - end | |
| 29 | - | |
| 30 | 24 | def content(args={}) |
| 31 | - owner_blog = self.blog | |
| 32 | - return nil unless owner_blog | |
| 33 | - results = '' | |
| 34 | - posts = visible_posts(args[:person]) | |
| 35 | - posts.except(:order).count(:all, :group => 'EXTRACT(YEAR FROM published_at)').sort_by {|year, count| -year.to_i}.each do |year, count| | |
| 36 | - results << content_tag('li', content_tag('strong', "#{year.to_i} (#{count})")) | |
| 37 | - results << "<ul class='#{year.to_i}-archive'>" | |
| 38 | - 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| | |
| 39 | - results << content_tag('li', link_to("#{month_name(month.to_i)} (#{count})", owner_blog.url.merge(year: year.to_i, month: month.to_i))) | |
| 40 | - end | |
| 41 | - results << "</ul>" | |
| 25 | + return nil unless self.blog | |
| 26 | + block = self | |
| 27 | + proc do | |
| 28 | + render :file => 'blocks/blog_archives', :locals => { :block => block } | |
| 42 | 29 | end |
| 43 | - block_title(title) + | |
| 44 | - content_tag('ul', results, :class => 'blog-archives') + | |
| 45 | - content_tag('div', link_to(_('Subscribe RSS Feed'), owner_blog.feed.url), :class => 'subscribe-feed') | |
| 46 | 30 | end |
| 47 | 31 | |
| 48 | 32 | def self.expire_on | ... | ... |
| ... | ... | @@ -0,0 +1,14 @@ |
| 1 | +<%= block_title(block.title) %> | |
| 2 | + | |
| 3 | +<ul class='blog-archives'> | |
| 4 | + <% block.blog.total_number_of_posts(:by_year).each do |year, count| %> | |
| 5 | + <%= content_tag('li', content_tag('strong', "#{year.to_i} (#{count})")) %> | |
| 6 | + <ul class='<%= year.to_i %>-archive'> | |
| 7 | + <% block.blog.total_number_of_posts(:by_month, year).each do |month, count| %> | |
| 8 | + <%= content_tag('li', link_to("#{month_name(month.to_i)} (#{count})", block.blog.url.merge(year: year.to_i, month: month.to_i))) %> | |
| 9 | + <% end %> | |
| 10 | + </ul> | |
| 11 | + <% end %> | |
| 12 | +</ul> | |
| 13 | + | |
| 14 | +<%= content_tag('div', link_to(_('Subscribe RSS Feed'), block.blog.feed.url), :class => 'subscribe-feed') %> | ... | ... |
test/unit/blog_test.rb
| ... | ... | @@ -266,4 +266,23 @@ class BlogTest < ActiveSupport::TestCase |
| 266 | 266 | |
| 267 | 267 | assert_equal blog.image(true).filename, 'noosfero-network.png' |
| 268 | 268 | end |
| 269 | + | |
| 270 | + should 'count total number of posts by year' do | |
| 271 | + p = create_user('testuser').person | |
| 272 | + blog = fast_create(Blog, :profile_id => p.id, :name => 'Blog test') | |
| 273 | + create(TextileArticle, :name => 'Post 1', :parent => blog, :profile => p, :published_at => DateTime.parse('16-08-2010')) | |
| 274 | + create(TextileArticle, :name => 'Post 2', :parent => blog, :profile => p, :published_at => DateTime.parse('17-08-2010')) | |
| 275 | + create(TextileArticle, :name => 'Post 3', :parent => blog, :profile => p, :published_at => DateTime.parse('10-05-2012')) | |
| 276 | + assert_equal [[2012.0, 1], [2010.0, 2]], blog.total_number_of_posts(:by_year) | |
| 277 | + end | |
| 278 | + | |
| 279 | + should 'count total number of posts by month' do | |
| 280 | + p = create_user('testuser').person | |
| 281 | + blog = fast_create(Blog, :profile_id => p.id, :name => 'Blog test') | |
| 282 | + create(TextileArticle, :name => 'Post 1', :parent => blog, :profile => p, :published_at => DateTime.parse('16-08-2010')) | |
| 283 | + create(TextileArticle, :name => 'Post 2', :parent => blog, :profile => p, :published_at => DateTime.parse('17-08-2010')) | |
| 284 | + create(TextileArticle, :name => 'Post 3', :parent => blog, :profile => p, :published_at => DateTime.parse('11-10-2010')) | |
| 285 | + assert_equal [[10.0, 1], [8.0, 2]], blog.total_number_of_posts(:by_month, 2010) | |
| 286 | + end | |
| 287 | + | |
| 269 | 288 | end | ... | ... |