Commit 3fb6b3011044dfee469de42a1f856eed72218203
1 parent
067cd84b
Exists in
master
and in
27 other branches
refactoring display filter
Showing
3 changed files
with
134 additions
and
6 deletions
Show diff stats
app/controllers/public/content_viewer_controller.rb
@@ -221,7 +221,7 @@ class ContentViewerController < ApplicationController | @@ -221,7 +221,7 @@ class ContentViewerController < ApplicationController | ||
221 | # relation. | 221 | # relation. |
222 | posts = posts.native_translations if blog_with_translation?(@page) | 222 | posts = posts.native_translations if blog_with_translation?(@page) |
223 | 223 | ||
224 | - @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))).to_a | 224 | + @posts = posts.display_filter(user, profile).paginate({ :page => params[:npage], :per_page => @page.posts_per_page }).to_a |
225 | 225 | ||
226 | if blog_with_translation?(@page) | 226 | if blog_with_translation?(@page) |
227 | @posts.replace @posts.map{ |p| p.get_translation_to(FastGettext.locale) }.compact | 227 | @posts.replace @posts.map{ |p| p.get_translation_to(FastGettext.locale) }.compact |
app/models/article.rb
@@ -486,15 +486,17 @@ class Article < ActiveRecord::Base | @@ -486,15 +486,17 @@ class Article < ActiveRecord::Base | ||
486 | scope :more_comments, :order => "comments_count DESC" | 486 | scope :more_comments, :order => "comments_count DESC" |
487 | scope :more_recent, :order => "created_at DESC" | 487 | scope :more_recent, :order => "created_at DESC" |
488 | 488 | ||
489 | - def self.display_filter(user, profile) | ||
490 | - return {:conditions => ['articles.published = ?', true]} if !user | 489 | + scope :display_filter, lambda {|user, profile| |
490 | + user.nil? ? | ||
491 | + {:conditions => ['articles.published = ?', true]} : | ||
491 | {:conditions => [" articles.published = ? OR | 492 | {:conditions => [" articles.published = ? OR |
492 | articles.last_changed_by_id = ? OR | 493 | articles.last_changed_by_id = ? OR |
493 | articles.profile_id = ? OR | 494 | articles.profile_id = ? OR |
494 | - ? OR articles.show_to_followers = ? AND ?", | 495 | + ? OR articles.show_to_followers = ? AND ? ", |
495 | true, user.id, user.id, user.has_permission?(:view_private_content, profile), | 496 | true, user.id, user.id, user.has_permission?(:view_private_content, profile), |
496 | - true, user.follows?(profile)]} | ||
497 | - end | 497 | + true, user.follows?(profile)] |
498 | + } | ||
499 | + } | ||
498 | 500 | ||
499 | 501 | ||
500 | def display_unpublished_article_to?(user) | 502 | def display_unpublished_article_to?(user) |
test/unit/article_test.rb
@@ -1892,4 +1892,130 @@ class ArticleTest < ActiveSupport::TestCase | @@ -1892,4 +1892,130 @@ class ArticleTest < ActiveSupport::TestCase | ||
1892 | assert_equal p3, article.author_by_version(3) | 1892 | assert_equal p3, article.author_by_version(3) |
1893 | end | 1893 | end |
1894 | 1894 | ||
1895 | + should 'display_filter display only public articles if there is no user' do | ||
1896 | + p = fast_create(Person) | ||
1897 | + Article.delete_all | ||
1898 | + a = fast_create(Article, :published => true, :profile_id => p.id) | ||
1899 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1900 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1901 | + assert_equal [a], Article.display_filter(nil, p) | ||
1902 | + end | ||
1903 | + | ||
1904 | + should 'display_filter display public articles for users' do | ||
1905 | + user = create_user('someuser').person | ||
1906 | + p = fast_create(Person) | ||
1907 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(false) | ||
1908 | + Article.delete_all | ||
1909 | + a = fast_create(Article, :published => true, :profile_id => p.id) | ||
1910 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1911 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1912 | + assert_equal [a], Article.display_filter(user, p) | ||
1913 | + end | ||
1914 | + | ||
1915 | + should 'display_filter display private article last changed by user' do | ||
1916 | + user = create_user('someuser').person | ||
1917 | + p = fast_create(Person) | ||
1918 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(false) | ||
1919 | + Article.delete_all | ||
1920 | + a = fast_create(Article, :published => false, :last_changed_by_id => user.id, :profile_id => p.id) | ||
1921 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1922 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1923 | + assert_equal [a], Article.display_filter(user, p) | ||
1924 | + end | ||
1925 | + | ||
1926 | + should 'display_filter display user private article of his own profile' do | ||
1927 | + user = create_user('someuser').person | ||
1928 | + user.stubs(:has_permission?).with(:view_private_content, user).returns(false) | ||
1929 | + p = fast_create(Person) | ||
1930 | + Article.delete_all | ||
1931 | + a = fast_create(Article, :published => false, :profile_id => user.id) | ||
1932 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1933 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1934 | + assert_equal [a], Article.display_filter(user, user) | ||
1935 | + end | ||
1936 | + | ||
1937 | + should 'display_filter show profile private content if the user has view_private_content permission' do | ||
1938 | + user = create_user('someuser').person | ||
1939 | + p = fast_create(Person) | ||
1940 | + Article.delete_all | ||
1941 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(false) | ||
1942 | + a = fast_create(Article, :published => false, :profile_id => p.id) | ||
1943 | + assert_equal [], Article.display_filter(user, p) | ||
1944 | + | ||
1945 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(true) | ||
1946 | + assert_equal [a], Article.display_filter(user, p) | ||
1947 | + end | ||
1948 | + | ||
1949 | + should 'display_filter show person private content to friends' do | ||
1950 | + user = create_user('someuser').person | ||
1951 | + p = fast_create(Person) | ||
1952 | + p.add_friend(user) | ||
1953 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(false) | ||
1954 | + Article.delete_all | ||
1955 | + a = fast_create(Article, :published => false, :show_to_followers => true, :profile_id => p.id) | ||
1956 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1957 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1958 | + assert_equal [a], Article.display_filter(user, p) | ||
1959 | + end | ||
1960 | + | ||
1961 | + should 'display_filter show community private content to members' do | ||
1962 | + user = create_user('someuser').person | ||
1963 | + p = fast_create(Community) | ||
1964 | + p.add_member(user) | ||
1965 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(false) | ||
1966 | + Article.delete_all | ||
1967 | + a = fast_create(Article, :published => false, :show_to_followers => true, :profile_id => p.id) | ||
1968 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1969 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1970 | + assert_equal [a], Article.display_filter(user, p) | ||
1971 | + end | ||
1972 | + | ||
1973 | + should 'display_filter do not show person private content to non friends' do | ||
1974 | + user = create_user('someuser').person | ||
1975 | + p = fast_create(Person) | ||
1976 | + assert !p.is_a_friend?(user) | ||
1977 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(false) | ||
1978 | + Article.delete_all | ||
1979 | + a = fast_create(Article, :published => false, :show_to_followers => true, :profile_id => p.id) | ||
1980 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1981 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1982 | + assert_equal [], Article.display_filter(user, p) | ||
1983 | + end | ||
1984 | + | ||
1985 | + should 'display_filter do not show community private content to non members' do | ||
1986 | + user = create_user('someuser').person | ||
1987 | + p = fast_create(Community) | ||
1988 | + assert !user.is_member_of?(p) | ||
1989 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(false) | ||
1990 | + Article.delete_all | ||
1991 | + a = fast_create(Article, :published => false, :show_to_followers => true, :profile_id => p.id) | ||
1992 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1993 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
1994 | + assert_equal [], Article.display_filter(user, p) | ||
1995 | + end | ||
1996 | + | ||
1997 | + should 'display_filter show community public content even it has no followers defined' do | ||
1998 | + user = create_user('someuser').person | ||
1999 | + p = fast_create(Community) | ||
2000 | + assert !user.is_member_of?(p) | ||
2001 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(false) | ||
2002 | + Article.delete_all | ||
2003 | + a = fast_create(Article, :published => true, :show_to_followers => true, :profile_id => p.id) | ||
2004 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
2005 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
2006 | + assert_equal [a], Article.display_filter(user, p) | ||
2007 | + end | ||
2008 | + | ||
2009 | + should 'display_filter show person public content even it has no followers defined' do | ||
2010 | + user = create_user('someuser').person | ||
2011 | + p = fast_create(Community) | ||
2012 | + assert !user.is_a_friend?(p) | ||
2013 | + user.stubs(:has_permission?).with(:view_private_content, p).returns(false) | ||
2014 | + Article.delete_all | ||
2015 | + a = fast_create(Article, :published => true, :show_to_followers => true, :profile_id => p.id) | ||
2016 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
2017 | + fast_create(Article, :published => false, :profile_id => p.id) | ||
2018 | + assert_equal [a], Article.display_filter(user, p) | ||
2019 | + end | ||
2020 | + | ||
1895 | end | 2021 | end |