Commit d0f8d245c7304e20f86be76e84de59e62f30c889

Authored by Rodrigo Souto
Committed by Antonio Terceiro
1 parent d2879e8d

Listing folder items

* Checking if each children of the folder should be displayed to the
	  current user by using the 'display_to?' method.
	* Moving some test from the folder test to the folder_helper test.

(ActionItem1396)
app/helpers/folder_helper.rb
1 1 module FolderHelper
2 2  
3   - def list_articles(articles, recursive = false)
4   - content_tag(
5   - 'table',
6   - content_tag('tr', content_tag('th', _('Title')) + content_tag('th', _('Last update'))) +
7   - articles.select { |item| item.public? }.map {|item| display_article_in_listing(item, recursive, 0)}.join('')
8   - )
  3 + def list_articles(articles, user, recursive = false)
  4 + if !articles.blank?
  5 + content_tag(
  6 + 'table',
  7 + content_tag('tr', content_tag('th', _('Title')) + content_tag('th', _('Last update'))) +
  8 + articles.select { |item| item.display_to?(user)}.map {|item| display_article_in_listing(item, recursive, 0)}.join('')
  9 + )
  10 + else
  11 + content_tag('em', _('(empty folder)'))
  12 + end
9 13 end
10 14  
11 15 def display_article_in_listing(article, recursive = false, level = 0)
... ...
app/models/folder.rb
... ... @@ -39,7 +39,7 @@ class Folder < Article
39 39 end
40 40  
41 41 def folder
42   - content_tag('div', body) + tag('hr') + (children.empty? ? content_tag('em', _('(empty folder)')) : list_articles(children))
  42 + content_tag('div', body) + tag('hr')
43 43 end
44 44  
45 45 def image_gallery
... ...
app/views/content_viewer/view_page.rhtml
... ... @@ -85,6 +85,9 @@
85 85 <% cache(@page.cache_key(params, user)) do %>
86 86 <div class="<%="article-body article-body-" + @page.css_class_name %>">
87 87 <%= article_to_html(@page) %>
  88 + <% if @page.folder? %>
  89 + <%= list_articles(@page.children, user)%>
  90 + <% end %>
88 91 <br style="clear:both" />
89 92 </div> <!-- end class="article-body" -->
90 93 <% end %>
... ...
app/views/profile/sitemap.rhtml
1 1 <h1><%= _("%s: site map") % profile.name %></h1>
2 2  
3   -<%= list_articles(@articles, false) %>
  3 +<%= list_articles(@articles, user) %>
... ...
test/unit/folder_helper_test.rb
... ... @@ -15,4 +15,82 @@ class FolderHelperTest &lt; Test::Unit::TestCase
15 15 assert_equal 'icons-mime/unknown.png', icon_for_article(art2)
16 16 end
17 17  
  18 + should 'list all the folder\'s children to the owner' do
  19 + profile = create_user('Folder Owner').person
  20 + folder = fast_create(Folder, {:name => 'Parent Folder', :profile_id => profile.id})
  21 + sub_folder = fast_create(Folder, {:name => 'Child Folder', :parent_id => folder.id,
  22 + :profile_id => profile.id})
  23 + sub_blog = fast_create(Blog, {:name => 'Child Blog', :parent_id => folder.id,
  24 + :profile_id => profile.id})
  25 + sub_article = fast_create(Article, {:name => 'Not Public Child Article', :parent_id =>
  26 + folder.id, :profile_id => profile.id, :published => false})
  27 +
  28 + result = folder.list_articles(folder.children, profile)
  29 +
  30 + assert_match 'Child Folder', result
  31 + assert_match 'Not Public Child Article', result
  32 + assert_match 'Child Blog', result
  33 + end
  34 +
  35 + should 'list the folder\'s children that are public to the user' do
  36 + profile = create_user('Folder Owner').person
  37 + profile2 = create_user('Folder Viwer').person
  38 + folder = fast_create(Folder, {:name => 'Parent Folder', :profile_id => profile.id})
  39 + public_article = fast_create(Article, {:name => 'Public Article', :parent_id =>
  40 + folder.id, :profile_id => profile.id, :published => true})
  41 + not_public_article = fast_create(Article, {:name => 'Not Public Article', :parent_id =>
  42 + folder.id, :profile_id => profile.id, :published => false})
  43 +
  44 + result = folder.list_articles(folder.children, profile2)
  45 +
  46 + assert_match 'Public Article', result
  47 + assert_no_match /Not Public Article/, result
  48 + end
  49 +
  50 + should ' not list the folder\'s children to the user because the owner\'s profile is not public' do
  51 + profile = create_user('folder-owner').person
  52 + profile.public_profile = false
  53 + profile.save!
  54 + profile2 = create_user('Folder Viwer').person
  55 + folder = fast_create(Folder, {:name => 'Parent Folder', :profile_id => profile.id})
  56 + article = fast_create(Article, {:name => 'Article', :parent_id => folder.id, :profile_id => profile.id})
  57 +
  58 + result = folder.list_articles(folder.children, profile2)
  59 +
  60 + assert_no_match /Article/, result
  61 + end
  62 +
  63 + should ' not list the folder\'s children to the user because the owner\'s profile is not visible' do
  64 + profile = create_user('folder-owner').person
  65 + profile.visible = false
  66 + profile.save!
  67 + profile2 = create_user('Folder Viwer').person
  68 + folder = fast_create(Folder, {:name => 'Parent Folder', :profile_id => profile.id})
  69 + article = fast_create(Article, {:name => 'Article', :parent_id => folder.id, :profile_id => profile.id})
  70 +
  71 + result = folder.list_articles(folder.children, profile2)
  72 +
  73 + assert_no_match /Article/, result
  74 + end
  75 +
  76 + should 'list subitems as HTML content' do
  77 + profile = create_user('folder-owner').person
  78 + folder = fast_create(Folder, {:name => 'Parent Folder', :profile_id => profile.id})
  79 + article = fast_create(Article, {:name => 'Article1', :parent_id => folder.id, :profile_id => profile.id})
  80 + article = fast_create(Article, {:name => 'Article2', :parent_id => folder.id, :profile_id => profile.id})
  81 +
  82 + result = folder.list_articles(folder.children, profile)
  83 +
  84 + assert_tag_in_string result, :tag => 'td', :descendant => { :tag => 'a', :attributes => { :href => /.*\/folder-owner\/my-article-[0-9]*(\?|$)/ } }, :content => /Article1/
  85 + assert_tag_in_string result, :tag => 'td', :descendant => { :tag => 'a', :attributes => { :href => /.*\/folder-owner\/my-article-[0-9]*(\?|$)/ } }, :content => /Article2/
  86 + end
  87 +
  88 + should 'explictly advise if empty' do
  89 + profile = create_user('folder-owner').person
  90 + folder = fast_create(Folder, {:name => 'Parent Folder', :profile_id => profile.id})
  91 + result = folder.list_articles(folder.children, profile)
  92 +
  93 + assert_match '(empty folder)', result
  94 + end
  95 +
18 96 end
... ...
test/unit/folder_test.rb
... ... @@ -18,23 +18,6 @@ class FolderTest &lt; ActiveSupport::TestCase
18 18 assert_not_equal Article.new.icon_name, Folder.new.icon_name
19 19 end
20 20  
21   - should 'list subitems as HTML content' do
22   - p = create_user('testuser').person
23   - f = Folder.create!(:profile => p, :name => 'f')
24   - f.children.create!(:profile => p, :name => 'onearticle')
25   - f.children.create!(:profile => p, :name => 'otherarticle')
26   - f.reload
27   -
28   - assert_tag_in_string f.to_html, :tag => 'td', :descendant => { :tag => 'a', :attributes => { :href => /.*\/testuser\/f\/onearticle(\?|$)/ } }, :content => /onearticle/
29   - assert_tag_in_string f.to_html, :tag => 'td', :descendant => { :tag => 'a', :attributes => { :href => /.*\/testuser\/f\/otherarticle(\?|$)/ } }, :content => /otherarticle/
30   - end
31   -
32   - should 'explictly advise if empty' do
33   - p = create_user('testuser').person
34   - f = Folder.create!(:profile => p, :name => 'f')
35   - assert_tag_in_string f.to_html, :content => '(empty folder)'
36   - end
37   -
38 21 should 'show text body in HTML content' do
39 22 p = create_user('testuser').person
40 23 f = Folder.create!(:name => 'f', :profile => p, :body => 'this-is-the-text')
... ...