Commit c97d1f564dde3e91e6181c6b48c8741aaf9b718c

Authored by Joenio Costa
Committed by Daniela Feitosa
1 parent 14532049

Expires blog cache after organization changes its URL address

(ActionItem1941)
app/models/blog.rb
... ... @@ -72,4 +72,8 @@ class Blog < Folder
72 72  
73 73 alias :display_posts_in_current_language? :display_posts_in_current_language
74 74  
  75 + def empty?
  76 + posts.compact.empty?
  77 + end
  78 +
75 79 end
... ...
app/sweepers/profile_sweeper.rb
... ... @@ -25,10 +25,23 @@ protected
25 25 profile.blocks.each do |block|
26 26 expire_timeout_fragment(block.cache_key)
27 27 end
  28 +
  29 + expire_blogs(profile) if profile.organization?
28 30 end
29 31  
30 32 def expire_statistics_block_cache(profile)
31 33 blocks = profile.environment.blocks.select { |b| b.kind_of?(EnvironmentStatisticsBlock) }
32 34 blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)}
33 35 end
  36 +
  37 + def expire_blogs(profile)
  38 + profile.blogs.select{|b| !b.empty?}.each do |blog|
  39 + pages = blog.posts.count / blog.posts_per_page + 1
  40 + ([nil] + (1..pages).to_a).each do |i|
  41 + expire_timeout_fragment(blog.cache_key({:npage => i}))
  42 + expire_timeout_fragment(blog.cache_key({:npage => i}, profile))
  43 + end
  44 + end
  45 + end
  46 +
34 47 end
... ...
app/views/content_viewer/blog_page.rhtml
... ... @@ -9,5 +9,5 @@
9 9 </div>
10 10 <hr class="pre-posts"/>
11 11 <div class="blog-posts">
12   - <%= (@posts.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(@posts, @page.visualization_format)) %>
  12 + <%= (@page.empty? ? content_tag('em', _('(no posts)')) : list_posts(@posts, @page.visualization_format)) %>
13 13 </div>
... ...
test/unit/blog_test.rb
... ... @@ -211,4 +211,11 @@ class BlogTest &lt; ActiveSupport::TestCase
211 211 assert !folder.accept_uploads?
212 212 end
213 213  
  214 + should 'know when blog has or when has no posts' do
  215 + blog = fast_create(Blog)
  216 + assert blog.empty?
  217 + fast_create(TextileArticle, :parent_id => blog.id)
  218 + assert ! blog.empty?
  219 + end
  220 +
214 221 end
... ...