From 40c1e5e71897fa6f32b1233b7462784ccd757164 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Fri, 25 Mar 2011 16:23:50 -0300 Subject: [PATCH] Display only translated posts on blog if configured --- app/controllers/public/content_viewer_controller.rb | 6 +++--- app/models/article.rb | 4 ++-- app/views/cms/_blog.rhtml | 2 +- test/functional/content_viewer_controller_test.rb | 17 +++++++++++++++++ test/unit/article_test.rb | 7 ++++++- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 89041bd..47cba3d 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -87,11 +87,11 @@ class ContentViewerController < ApplicationController @page.posts end - posts = posts.native_translations if @page.blog? && @page.display_posts_in_current_language? + if @page.blog? && @page.display_posts_in_current_language? + posts = posts.native_translations.all(Article.display_filter(user, profile)).map{ |p| p.get_translation_to(FastGettext.locale) }.compact + end @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))) - - @posts.map!{ |p| p.get_translation_to(FastGettext.locale) } if @page.blog? && @page.display_posts_in_current_language? end if @page.folder? && @page.gallery? diff --git a/app/models/article.rb b/app/models/article.rb index 94d5fef..5e3e324 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -319,12 +319,12 @@ class Article < ActiveRecord::Base end def get_translation_to(locale) - if self.language.nil? || self.language == locale + if self.language.nil? || self.language.blank? || self.language == locale self elsif self.native_translation.language == locale self.native_translation else - self.native_translation.translations.first(:conditions => { :language => locale }) || self + self.native_translation.translations.first(:conditions => { :language => locale }) end end diff --git a/app/views/cms/_blog.rhtml b/app/views/cms/_blog.rhtml index 73769e1..86ebcbf 100644 --- a/app/views/cms/_blog.rhtml +++ b/app/views/cms/_blog.rhtml @@ -56,7 +56,7 @@ <%= labelled_form_field(_('Posts per page:'), f.select(:posts_per_page, [5, 10, 20, 50, 100])) %> -<%= labelled_check_box(_("Try listing only translated posts"), 'article[display_posts_in_current_language]', '1', @article.display_posts_in_current_language?) %> +<%= labelled_check_box(_("List only translated posts"), 'article[display_posts_in_current_language]', '1', @article.display_posts_in_current_language?) %> <% f.fields_for 'feed', @article.feed do |feed| %> <%= labelled_form_field(_('Limit of posts in RSS Feed'), feed.select(:limit, [5, 10, 20, 50])) %> diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 463e4ca..e54524d 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1223,6 +1223,23 @@ class ContentViewerControllerTest < Test::Unit::TestCase assert_no_tag :div, :attributes => { :id => "post-#{en_article.id}" } end + should 'not display article at blog listing if blog option is enabled and there is no translation for the language' do + FastGettext.stubs(:locale).returns('pt') + blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog') + blog.stubs(:display_posts_in_current_language).returns(true) + en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id) + es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article) + pt_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'pt', :parent_id => blog.id, :translation_of_id => en_article) + + en_article2 = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id) + es_article2 = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article2) + + + get :view_page, :profile => @profile.identifier, :page => blog.explode_path + + assert_equal [pt_article], assigns(:posts) + end + should 'list all posts at blog listing if blog option is disabled' do FastGettext.stubs(:locale).returns('es') blog = Blog.create!(:name => 'A blog test', :profile => profile, :display_posts_in_current_language => false) diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 4a21bfc..d531fb3 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1367,6 +1367,11 @@ class ArticleTest < Test::Unit::TestCase assert_equal article, article.get_translation_to('en') end + should 'get self if language article is blank' do + article = fast_create(Article, :language => '', :profile_id => @profile.id) + assert_equal article, article.get_translation_to('en') + end + should 'get self if article is the translation' do article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) assert_equal article, article.get_translation_to('pt') @@ -1386,7 +1391,7 @@ class ArticleTest < Test::Unit::TestCase should 'get self if article does not has a translation' do native_article = fast_create(Article, :language => 'pt', :profile_id => @profile.id) - assert_equal native_article, native_article.get_translation_to('en') + assert_nil native_article.get_translation_to('en') end should 'get only non translated articles' do -- libgit2 0.21.2