Commit 40c1e5e71897fa6f32b1233b7462784ccd757164

Authored by Daniela Feitosa
1 parent 2015618d

Display only translated posts on blog if configured

(ActionItem1961)
app/controllers/public/content_viewer_controller.rb
@@ -87,11 +87,11 @@ class ContentViewerController < ApplicationController @@ -87,11 +87,11 @@ class ContentViewerController < ApplicationController
87 @page.posts 87 @page.posts
88 end 88 end
89 89
90 - posts = posts.native_translations if @page.blog? && @page.display_posts_in_current_language? 90 + if @page.blog? && @page.display_posts_in_current_language?
  91 + posts = posts.native_translations.all(Article.display_filter(user, profile)).map{ |p| p.get_translation_to(FastGettext.locale) }.compact
  92 + end
91 93
92 @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))) 94 @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile)))
93 -  
94 - @posts.map!{ |p| p.get_translation_to(FastGettext.locale) } if @page.blog? && @page.display_posts_in_current_language?  
95 end 95 end
96 96
97 if @page.folder? && @page.gallery? 97 if @page.folder? && @page.gallery?
app/models/article.rb
@@ -319,12 +319,12 @@ class Article < ActiveRecord::Base @@ -319,12 +319,12 @@ class Article < ActiveRecord::Base
319 end 319 end
320 320
321 def get_translation_to(locale) 321 def get_translation_to(locale)
322 - if self.language.nil? || self.language == locale 322 + if self.language.nil? || self.language.blank? || self.language == locale
323 self 323 self
324 elsif self.native_translation.language == locale 324 elsif self.native_translation.language == locale
325 self.native_translation 325 self.native_translation
326 else 326 else
327 - self.native_translation.translations.first(:conditions => { :language => locale }) || self 327 + self.native_translation.translations.first(:conditions => { :language => locale })
328 end 328 end
329 end 329 end
330 330
app/views/cms/_blog.rhtml
@@ -56,7 +56,7 @@ @@ -56,7 +56,7 @@
56 56
57 <%= labelled_form_field(_('Posts per page:'), f.select(:posts_per_page, [5, 10, 20, 50, 100])) %> 57 <%= labelled_form_field(_('Posts per page:'), f.select(:posts_per_page, [5, 10, 20, 50, 100])) %>
58 58
59 -<%= labelled_check_box(_("Try listing only translated posts"), 'article[display_posts_in_current_language]', '1', @article.display_posts_in_current_language?) %> 59 +<%= labelled_check_box(_("List only translated posts"), 'article[display_posts_in_current_language]', '1', @article.display_posts_in_current_language?) %>
60 60
61 <% f.fields_for 'feed', @article.feed do |feed| %> 61 <% f.fields_for 'feed', @article.feed do |feed| %>
62 <%= labelled_form_field(_('Limit of posts in RSS Feed'), feed.select(:limit, [5, 10, 20, 50])) %> 62 <%= labelled_form_field(_('Limit of posts in RSS Feed'), feed.select(:limit, [5, 10, 20, 50])) %>
test/functional/content_viewer_controller_test.rb
@@ -1223,6 +1223,23 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase @@ -1223,6 +1223,23 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
1223 assert_no_tag :div, :attributes => { :id => "post-#{en_article.id}" } 1223 assert_no_tag :div, :attributes => { :id => "post-#{en_article.id}" }
1224 end 1224 end
1225 1225
  1226 + should 'not display article at blog listing if blog option is enabled and there is no translation for the language' do
  1227 + FastGettext.stubs(:locale).returns('pt')
  1228 + blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog')
  1229 + blog.stubs(:display_posts_in_current_language).returns(true)
  1230 + en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id)
  1231 + es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article)
  1232 + pt_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'pt', :parent_id => blog.id, :translation_of_id => en_article)
  1233 +
  1234 + en_article2 = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id)
  1235 + es_article2 = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article2)
  1236 +
  1237 +
  1238 + get :view_page, :profile => @profile.identifier, :page => blog.explode_path
  1239 +
  1240 + assert_equal [pt_article], assigns(:posts)
  1241 + end
  1242 +
1226 should 'list all posts at blog listing if blog option is disabled' do 1243 should 'list all posts at blog listing if blog option is disabled' do
1227 FastGettext.stubs(:locale).returns('es') 1244 FastGettext.stubs(:locale).returns('es')
1228 blog = Blog.create!(:name => 'A blog test', :profile => profile, :display_posts_in_current_language => false) 1245 blog = Blog.create!(:name => 'A blog test', :profile => profile, :display_posts_in_current_language => false)
test/unit/article_test.rb
@@ -1367,6 +1367,11 @@ class ArticleTest &lt; Test::Unit::TestCase @@ -1367,6 +1367,11 @@ class ArticleTest &lt; Test::Unit::TestCase
1367 assert_equal article, article.get_translation_to('en') 1367 assert_equal article, article.get_translation_to('en')
1368 end 1368 end
1369 1369
  1370 + should 'get self if language article is blank' do
  1371 + article = fast_create(Article, :language => '', :profile_id => @profile.id)
  1372 + assert_equal article, article.get_translation_to('en')
  1373 + end
  1374 +
1370 should 'get self if article is the translation' do 1375 should 'get self if article is the translation' do
1371 article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) 1376 article = fast_create(Article, :language => 'pt', :profile_id => @profile.id)
1372 assert_equal article, article.get_translation_to('pt') 1377 assert_equal article, article.get_translation_to('pt')
@@ -1386,7 +1391,7 @@ class ArticleTest &lt; Test::Unit::TestCase @@ -1386,7 +1391,7 @@ class ArticleTest &lt; Test::Unit::TestCase
1386 1391
1387 should 'get self if article does not has a translation' do 1392 should 'get self if article does not has a translation' do
1388 native_article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) 1393 native_article = fast_create(Article, :language => 'pt', :profile_id => @profile.id)
1389 - assert_equal native_article, native_article.get_translation_to('en') 1394 + assert_nil native_article.get_translation_to('en')
1390 end 1395 end
1391 1396
1392 should 'get only non translated articles' do 1397 should 'get only non translated articles' do