diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 600cb7b..9cfc424 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -61,17 +61,24 @@ class CmsController < MyProfileController def view @article = profile.articles.find(params[:id]) - @subitems = @article.children.reject {|item| item.folder? } + conditions = [] if @article.blog? - @subitems.reject! {|item| item.class == RssFeed } + conditions = ['type != ?', 'RssFeed'] end - @folders = @article.children.select {|item| item.folder? } + + @articles = @article.children.find( + :all, + :order => "case when type = 'Folder' then 0 when type ='Blog' then 1 else 2 end, updated_at DESC", + :conditions => conditions + ).paginate(:per_page => per_page, :page => params[:npage]) end def index @article = nil - @subitems = profile.top_level_articles.reject {|item| item.folder? } - @folders = profile.top_level_articles.select {|item| item.folder?} + @articles = profile.top_level_articles.find( + :all, + :order => "case when type = 'Folder' then 0 when type ='Blog' then 1 else 2 end, updated_at DESC" + ).paginate(:per_page => per_page, :page => params[:npage]) render :action => 'view' end diff --git a/app/helpers/cms_helper.rb b/app/helpers/cms_helper.rb index 52a0708..22c9b2a 100644 --- a/app/helpers/cms_helper.rb +++ b/app/helpers/cms_helper.rb @@ -27,4 +27,30 @@ module CmsHelper article_helper.custom_options_for_article(article) end + def link_to_article(article) + article_name = short_filename(article.name, 30) + if article.folder? + link_to article_name, :action => 'view', :id => article.id + else + link_to article_name, article.url + end + end + + def display_spread_button(profile, article) + if profile.person? + button_without_text :spread, _('Spread this'), :action => 'publish', :id => article.id + elsif profile.community? && environment.portal_community + button_without_text :spread, _('Spread this'), :action => 'publish_on_portal_community', :id => article.id + end + end + + def display_delete_button(article) + confirm_message = if article.folder? + _('Are you sure that you want to remove this folder? Note that all the items inside it will also be removed!') + else + _('Are you sure that you want to remove this item?') + end + + button_without_text :delete, _('Delete'), { :action => 'destroy', :id => article.id }, :method => :post, :confirm => confirm_message + end end diff --git a/app/helpers/folder_helper.rb b/app/helpers/folder_helper.rb index 331d489..4ca8edd 100644 --- a/app/helpers/folder_helper.rb +++ b/app/helpers/folder_helper.rb @@ -4,11 +4,12 @@ module FolderHelper def list_articles(articles, recursive = false) if !articles.blank? - content_tag( - 'table', - content_tag('tr', content_tag('th', _('Title')) + content_tag('th', _('Last update'))) + - articles.sort_by { |article| article.updated_at }.reverse.map {|item| display_article_in_listing(item, recursive, 0)}.join('') - ) + articles = articles.find( + :all, + :order => "updated_at DESC" + ).paginate(:per_page => 10, :page => params[:npage]) + + render :file => 'shared/articles_list', :locals => {:articles => articles, :recursive => recursive} else content_tag('em', _('(empty folder)')) end diff --git a/app/models/article.rb b/app/models/article.rb index 1c6c0fb..d573961 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -110,9 +110,9 @@ class Article < ActiveRecord::Base # retrieves all articles belonging to the given +profile+ that are not # sub-articles of any other article. - def self.top_level_for(profile) - self.find(:all, :conditions => [ 'parent_id is null and profile_id = ?', profile.id ]) - end + named_scope :top_level_for, lambda { |profile| + {:conditions => [ 'parent_id is null and profile_id = ?', profile.id ]} + } # retrieves the latest +limit+ articles, sorted from the most recent to the # oldest. diff --git a/app/views/cms/view.rhtml b/app/views/cms/view.rhtml index 94a63bc..a0e3e0f 100644 --- a/app/views/cms/view.rhtml +++ b/app/views/cms/view.rhtml @@ -46,51 +46,27 @@ <% end %> - <%# folders %> - <% for folder in @folders %> + <% @articles.each do |article| %> - <%= image_tag(icon_for_article(folder)) %> - <%= link_to folder.name, :action => 'view', :id => folder.id %> + <%= image_tag(icon_for_article(article)) %> + <%= link_to_article(article) %> - <%= folder.class.short_description %> + <%= article.class.short_description %> - <%= button_without_text :edit, _('Properties'), :action => 'edit', :id => folder.id %> - <%= button_without_text :eyes, _('Public view'), folder.url %> + <%= button_without_text :edit, _('Edit'), :action => 'edit', :id => article.id %> + <%= button_without_text :eyes, _('Public view'), article.url %> + <%= display_spread_button(profile, article) unless article.folder? %> <% if !environment.enabled?('cant_change_homepage') %> - <%= button_without_text :home, _('Use as homepage'), { :action => 'set_home_page', :id => folder.id }, :method => :post %> + <%= button_without_text :home, _('Use as homepage'), { :action => 'set_home_page', :id => article.id }, :method => :post %> <% end %> - <%= button_without_text :delete, _('Delete'), { :action => 'destroy', :id => folder.id }, :method => :post, :confirm => _('Are you sure that you want to remove this folder? Note that all the items inside it will also be removed!') %> - - - <% end %> - - <%# non-folder subitems %> - <% for item in @subitems %> - - - <%= image_tag(icon_for_article(item)) %> - <%= link_to short_filename(item.name,30), item.url %> - - - <%= item.class.short_description %> - - - <%= button_without_text :edit, _('Edit'), :action => 'edit', :id => item.id %> - <%= button_without_text :eyes, _('Public view'), item.url %> - <% if profile.person? %> - <%= button_without_text :spread, _('Spread this'), :action => 'publish', :id => item.id %> - <% elsif profile.community? && environment.portal_community %> - <%= button_without_text :spread, _('Spread this'), :action => 'publish_on_portal_community', :id => item.id %> - <% end %> - <% if !environment.enabled?('cant_change_homepage') %> - <%= button_without_text :home, _('Use as homepage'), { :action => 'set_home_page', :id => item.id }, :method => :post %> - <% end %> - <%= button_without_text :delete, _('Delete'), { :action => 'destroy', :id => item.id }, :method => :post, :confirm => _('Are you sure that you want to remove this item?') %> + <%= display_delete_button(article) %> <% end %> + +<%= pagination_links @articles, {:param_name => 'npage', :prev_label => '« ' + _('Previous'), :next_label => _('Next') + ' »', :page_links => true} %> diff --git a/app/views/content_viewer/folder.rhtml b/app/views/content_viewer/folder.rhtml index 19b70d9..a670b39 100644 --- a/app/views/content_viewer/folder.rhtml +++ b/app/views/content_viewer/folder.rhtml @@ -8,5 +8,5 @@ <% if folder.children.empty? %> <%= _('(empty folder)') %> <% else %> - <%= list_articles(available_articles(folder.children, user)) %> + <%= list_articles(folder.children) %> <% end %> diff --git a/app/views/shared/articles_list.rhtml b/app/views/shared/articles_list.rhtml new file mode 100644 index 0000000..f47b4bf --- /dev/null +++ b/app/views/shared/articles_list.rhtml @@ -0,0 +1,14 @@ + + + + + + + <% articles.each do |article| %> + <% if article.display_to?(user) %> + <%= display_article_in_listing(article, recursive, 0) %> + <% end %> + <% end %> +
<%= _('Title') %><%= _('Last update') %>
+ +

<%= pagination_links(articles, {:param_name => 'npage', :prev_label => '« ' + _('Previous'), :next_label => _('Next') + ' »', :page_links => true}) %>

diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index c7ab56d..a3f4968 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -34,7 +34,7 @@ class CmsControllerTest < Test::Unit::TestCase assert_template 'view' assert_equal profile, assigns(:profile) assert_nil assigns(:article) - assert_kind_of Array, assigns(:subitems) + assert_kind_of Array, assigns(:articles) end should 'be able to view a particular document' do @@ -46,9 +46,9 @@ class CmsControllerTest < Test::Unit::TestCase assert_template 'view' assert_equal a, assigns(:article) - assert_equal [], assigns(:subitems) + assert_equal [], assigns(:articles) - assert_kind_of Array, assigns(:subitems) + assert_kind_of Array, assigns(:articles) end should 'be able to edit a document' do @@ -84,7 +84,7 @@ class CmsControllerTest < Test::Unit::TestCase end should 'display set as home page link to non folder' do - a = profile.articles.create!(:name => 'my new home page') + a = fast_create(TextileArticle, :profile_id => profile.id, :updated_at => DateTime.now) Article.stubs(:short_description).returns('bli') get :index, :profile => profile.identifier assert_tag :tag => 'a', :content => 'Use as homepage', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/set_home_page/#{a.id}" } @@ -452,26 +452,27 @@ class CmsControllerTest < Test::Unit::TestCase assert_tag :tag => 'input', :attributes => { :name => 'parent_id', :value => profile.home_page.id } end - should 'list folders at top level' do - Folder.destroy_all - f1 = Folder.new(:name => 'f1'); profile.articles << f1; f1.save! - f2 = Folder.new(:name => 'f2'); profile.articles << f2; f2.save! + should 'list folders before others' do + profile.articles.destroy_all + + folder1 = fast_create(Folder, :profile_id => profile.id, :updated_at => DateTime.now - 1.hour) + article = fast_create(TextileArticle, :profile_id => profile.id, :updated_at => DateTime.now) + folder2 = fast_create(Folder, :profile_id => profile.id, :updated_at => DateTime.now + 1.hour) get :index, :profile => profile.identifier - assert_equal [f1, f2], assigns(:folders) - assert_not_includes assigns(:subitems), f1 - assert_not_includes assigns(:subitems), f2 + assert_equal [folder2, folder1, article], assigns(:articles) end should 'list folders inside another folder' do - parent = Folder.new(:name => 'parent'); profile.articles << parent; parent.save! - f1 = Folder.new(:name => 'f1', :parent => parent); profile.articles << f1; f1.save! - f2 = Folder.new(:name => 'f2', :parent => parent); profile.articles << f2; f2.save! + profile.articles.destroy_all + + parent = fast_create(Folder, :profile_id => profile.id) + folder1 = fast_create(Folder, :parent_id => parent.id, :profile_id => profile.id, :updated_at => DateTime.now - 1.hour) + article = fast_create(TextileArticle, :parent_id => parent.id, :profile_id => profile.id, :updated_at => DateTime.now) + folder2 = fast_create(Folder, :parent_id => parent.id, :profile_id => profile.id, :updated_at => DateTime.now + 1.hour) get :view, :profile => profile.identifier, :id => parent.id - assert_equal [f1, f2], assigns(:folders) - assert_not_includes assigns(:subitems), f1 - assert_not_includes assigns(:subitems), f2 + assert_equal [folder2, folder1, article], assigns(:articles) end should 'offer to create new top-level folder' do @@ -1190,7 +1191,7 @@ class CmsControllerTest < Test::Unit::TestCase end should "display 'Publish' when profile is a person" do - a = profile.articles.create!(:name => 'my new home page') + a = fast_create(TextileArticle, :profile_id => profile.id, :updated_at => DateTime.now) Article.stubs(:short_description).returns('bli') get :index, :profile => profile.identifier assert_tag :tag => 'a', :attributes => {:href => "/myprofile/#{profile.identifier}/cms/publish/#{a.id}"} @@ -1200,7 +1201,7 @@ class CmsControllerTest < Test::Unit::TestCase community = fast_create(Community) community.add_member(profile) Environment.any_instance.stubs(:portal_community).returns(community) - a = community.articles.create!(:name => 'my new home page') + a = fast_create(TextileArticle, :profile_id => community.id, :updated_at => DateTime.now) Article.stubs(:short_description).returns('bli') get :index, :profile => community.identifier assert_tag :tag => 'a', :attributes => {:href => "/myprofile/#{community.identifier}/cms/publish_on_portal_community/#{a.id}"} diff --git a/test/unit/cms_helper_test.rb b/test/unit/cms_helper_test.rb index 162c658..58192b7 100644 --- a/test/unit/cms_helper_test.rb +++ b/test/unit/cms_helper_test.rb @@ -18,6 +18,74 @@ class CmsHelperTest < Test::Unit::TestCase assert_match /id="article\[accept_comments\]" name="article\[accept_comments\]" type="hidden" value="0"/, result end + should 'display link to folder content if article is folder' do + profile = fast_create(Profile) + folder = fast_create(Folder, :name => 'My folder', :profile_id => profile.id) + expects(:link_to).with('My folder', :action => 'view', :id => folder.id) + + result = link_to_article(folder) + end + + should 'display link to article if article is not folder' do + profile = fast_create(Profile) + article = fast_create(TinyMceArticle, :name => 'My article', :profile_id => profile.id) + expects(:link_to).with('My article', article.url) + + result = link_to_article(article) + end + + should 'display spread button when profile is a person' do + profile = fast_create(Person) + article = fast_create(TinyMceArticle, :name => 'My article', :profile_id => profile.id) + expects(:button_without_text).with(:spread, 'Spread this', :action => 'publish', :id => article.id) + + result = display_spread_button(profile, article) + end + + should 'display spread button when profile is a community and env has portal_community' do + env = fast_create(Environment) + env.expects(:portal_community).returns(true) + profile = fast_create(Community, :environment_id => env.id) + expects(:environment).returns(env) + + article = fast_create(TinyMceArticle, :name => 'My article', :profile_id => profile.id) + + expects(:button_without_text).with(:spread, 'Spread this', :action => 'publish_on_portal_community', :id => article.id) + + result = display_spread_button(profile, article) + end + + should 'not display spread button when profile is a community and env has not portal_community' do + env = fast_create(Environment) + env.expects(:portal_community).returns(nil) + profile = fast_create(Community, :environment_id => env.id) + expects(:environment).returns(env) + + article = fast_create(TinyMceArticle, :name => 'My article', :profile_id => profile.id) + + expects(:button_without_text).with(:spread, 'Spread this', :action => 'publish_on_portal_community', :id => article.id).never + + result = display_spread_button(profile, article) + end + + should 'display delete_button to folder' do + profile = fast_create(Profile) + folder = fast_create(Folder, :name => 'My folder', :profile_id => profile.id) + confirm_message = 'Are you sure that you want to remove this folder? Note that all the items inside it will also be removed!' + expects(:button_without_text).with(:delete, 'Delete', {:action => 'destroy', :id => folder.id}, :method => :post, :confirm => confirm_message) + + result = display_delete_button(folder) + end + + should 'display delete_button to article' do + profile = fast_create(Profile) + article = fast_create(TinyMceArticle, :name => 'My article', :profile_id => profile.id) + confirm_message = 'Are you sure that you want to remove this item?' + expects(:button_without_text).with(:delete, 'Delete', {:action => 'destroy', :id => article.id}, :method => :post, :confirm => confirm_message) + + result = display_delete_button(article) + end + end module RssFeedHelper diff --git a/test/unit/folder_helper_test.rb b/test/unit/folder_helper_test.rb index fa767f5..6ff653b 100644 --- a/test/unit/folder_helper_test.rb +++ b/test/unit/folder_helper_test.rb @@ -77,8 +77,16 @@ class FolderHelperTest < Test::Unit::TestCase should 'list subitems as HTML content' do profile = create_user('folder-owner').person folder = fast_create(Folder, {:name => 'Parent Folder', :profile_id => profile.id}) - article = fast_create(Article, {:name => 'Article1', :parent_id => folder.id, :profile_id => profile.id}) - article = fast_create(Article, {:name => 'Article2', :parent_id => folder.id, :profile_id => profile.id}) + article1 = fast_create(Article, {:name => 'Article1', :parent_id => folder.id, :profile_id => profile.id, :updated_at => DateTime.now }) + article2 = fast_create(Article, {:name => 'Article2', :parent_id => folder.id, :profile_id => profile.id, :updated_at => DateTime.now }) + self.stubs(:params).returns({:npage => nil}) + + articles = folder.children.find(:all, :order => 'updated_at DESC').paginate(:per_page => 10, :page => params[:npage]) + expects(:user).returns(profile).at_least_once + expects(:recursive).returns(false).at_least_once + expects(:pagination_links).with(anything, anything).returns('') + list = render 'shared/articles_list', binding + expects(:render).with(:file => 'shared/articles_list', :locals => { :articles => articles, :recursive => false}).returns(list) result = list_articles(folder.children) diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 80f2f8b..e3e6200 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -193,10 +193,10 @@ class ProfileTest < Test::Unit::TestCase list = profile.top_level_articles same_list = profile.top_level_articles - assert_same list, same_list + assert_equal list.object_id, same_list.object_id other_list = profile.top_level_articles(true) - assert_not_same list, other_list + assert_not_equal list.object_id, other_list.object_id end should 'be able to find profiles by their names with ferret' do -- libgit2 0.21.2