diff --git a/app/controllers/public/browse_controller.rb b/app/controllers/public/browse_controller.rb index 17f85a5..f37c68f 100644 --- a/app/controllers/public/browse_controller.rb +++ b/app/controllers/public/browse_controller.rb @@ -6,6 +6,8 @@ class BrowseController < PublicController more_recent more_active more_popular + more_comments + more_views ) def per_page @@ -36,6 +38,18 @@ class BrowseController < PublicController @results = @results.compact.paginate(:per_page => per_page, :page => params[:page]) end + def contents + @filter = filter + @title = self.filter_description(params[:action] + '_' + @filter ) + + @results = @environment.articles.published.text_articles.send(@filter) + + if !params[:query].blank? + @results = @results.find_by_contents(params[:query]) + end + @results = @results.compact.paginate(:per_page => per_page, :page => params[:page]) + end + protected def filter @@ -54,6 +68,9 @@ class BrowseController < PublicController 'communities_more_recent' => _('More recent communities'), 'communities_more_active' => _('More active communities'), 'communities_more_popular' => _('More popular communities'), + 'contents_more_recent' => _('More recent contents'), + 'contents_more_views' => _('Most viewed contents'), + 'contents_more_comments' => _('Most commented contents'), }[str] || str end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 62685ec..3dbf313 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1134,6 +1134,17 @@ module ApplicationHelper link_to(content_tag(:span, _('Communities Menu')), '#', :onclick => "toggleSubmenu(this,'',#{links.to_json}); return false", :class => 'menu-submenu-trigger up', :id => 'submenu-communities-trigger') end + def browse_contents_menu + links = [ + {s_('contents|More Comments') => {:href => url_for({:controller => 'browse', :action => 'contents', :filter => 'more_comments'})}}, + {s_('contents|More Views') => {:href => url_for({:controller => 'browse', :action => 'contents', :filter => 'more_views'})}}, + {s_('contents|More Recent') => {:href => url_for({:controller => 'browse', :action => 'contents', :filter => 'more_recent'})}} + ] + + link_to(content_tag(:span, _('Contents'), :class => 'icon-blog'), {:controller => "browse", :action => 'contents'}, :id => 'submenu-contents') + + link_to(content_tag(:span, _('Contents Menu')), '#', :onclick => "toggleSubmenu(this,'',#{links.to_json}); return false", :class => 'menu-submenu-trigger up', :id => 'submenu-contents-trigger') + end + def pagination_links(collection, options={}) options = {:prev_label => '« ' + _('Previous'), :next_label => _('Next') + ' »'}.merge(options) will_paginate(collection, options) diff --git a/app/models/article.rb b/app/models/article.rb index b8f136a..f5956a2 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -355,11 +355,20 @@ class Article < ActiveRecord::Base ['Folder', 'Blog', 'Forum', 'Gallery'] end + def self.text_article_types + ['TextArticle', 'TextileArticle', 'TinyMceArticle'] + end + named_scope :published, :conditions => { :published => true } named_scope :folders, :conditions => { :type => folder_types} named_scope :no_folders, :conditions => ['type NOT IN (?)', folder_types] named_scope :galleries, :conditions => { :type => 'Gallery' } named_scope :images, :conditions => { :is_image => true } + named_scope :text_articles, :conditions => [ 'articles.type IN (?)', text_article_types ] + + named_scope :more_comments, :order => "comments_count DESC" + named_scope :more_views, :order => "hits DESC" + named_scope :more_recent, :order => "created_at DESC" def self.display_filter(user, profile) return {:conditions => ['published = ?', true]} if !user @@ -528,6 +537,28 @@ class Article < ActiveRecord::Base end end + def more_comments_label + amount = self.comments_count + { + 0 => _('no comments'), + 1 => _('one comment') + }[amount] || _("%s comments") % amount + + end + + def more_views_label + amount = self.hits + { + 0 => _('no views'), + 1 => _('one view') + }[amount] || _("%s views") % amount + + end + + def more_recent_label + _('Created at: ') + end + private def sanitize_tag_list diff --git a/app/views/browse/_article.rhtml b/app/views/browse/_article.rhtml new file mode 100644 index 0000000..e36e667 --- /dev/null +++ b/app/views/browse/_article.rhtml @@ -0,0 +1,11 @@ +
Article to test browse contents', :profile_id => profile.id, :comments_count => 5) + article2 = fast_create(TinyMceArticle, :body => '
Another article to test browse contents
', :profile_id => profile.id, :comments_count => 10) + article3 = fast_create(TinyMceArticle, :body => 'Another article to test browse contents
', :profile_id => profile.id, :comments_count => 1) + + get :contents, :filter => 'more_comments' + assert_equal [article2,article1,article3] , assigns(:results) + end + + should 'list all contents filter by more views' do + article1 = fast_create(TinyMceArticle, :body => 'Article to test browse contents', :profile_id => profile.id, :hits => 5) + article2 = fast_create(TinyMceArticle, :body => '
Another article to test browse contents
', :profile_id => profile.id, :hits => 10) + article3 = fast_create(TinyMceArticle, :body => 'Another article to test browse contents
', :profile_id => profile.id, :hits => 1) + + get :contents, :filter => 'more_views' + assert_equal [article2,article1,article3], assigns(:results) + end + + should 'have the more_recent filter by default' do + get :contents, :filter => 'more_recent' + assert_equal 'more_recent' , assigns(:filter) + + get :contents, :filter => 'more_comments' + assert_equal 'more_comments' , assigns(:filter) + + get :contents, :filter => 'more_views' + assert_equal 'more_views' , assigns(:filter) + + get :contents, :filter => 'more_anything' + assert_equal 'more_recent' , assigns(:filter) + end + + should 'the contents filter define the title' do + get :contents, :filter => 'more_recent' + assert_equal 'More recent contents' , assigns(:title) + assert_tag :h1, :content => 'More recent contents' + + get :contents, :filter => 'more_views' + assert_equal 'Most viewed contents' , assigns(:title) + assert_tag :h1, :content => 'Most viewed contents' + + get :contents, :filter => 'more_comments' + assert_equal 'Most commented contents' , assigns(:title) + assert_tag :h1, :content => 'Most commented contents' + + get :contents, :filter => 'more_anything' + assert_equal 'More recent contents' , assigns(:title) + assert_tag :h1, :content => 'More recent contents' + end + + should "only include published contents in more_recent filter" do + # assuming that all filters behave the same! + article = fast_create(TinyMceArticle, :body => 'Article to test browse contents', :profile_id => profile.id, :published => false) + get :contents, :filter => 'more_recent' + assert_not_includes assigns(:results), article + end + end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index a4cbf20..7ff89dd 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1562,4 +1562,78 @@ class ArticleTest < Test::Unit::TestCase end end + should 'find more recent contents' do + Article.delete_all + + c1 = fast_create(TinyMceArticle, :name => 'Testing article 1', :body => 'Article body 1', :profile_id => profile.id, :created_at => DateTime.now - 4) + c2 = fast_create(TinyMceArticle, :name => 'Testing article 2', :body => 'Article body 2', :profile_id => profile.id, :created_at => DateTime.now - 1) + c3 = fast_create(TinyMceArticle, :name => 'Testing article 3', :body => 'Article body 3', :profile_id => profile.id, :created_at => DateTime.now - 3) + + assert_equal [c2,c3,c1] , Article.more_recent + + c4 = fast_create(TinyMceArticle, :name => 'Testing article 4', :body => 'Article body 4', :profile_id => profile.id, :created_at => DateTime.now - 2) + assert_equal [c2,c4,c3,c1] , Article.more_recent + end + + should 'respond to more comments' do + assert_respond_to Article, :more_comments + end + + should 'respond to more views' do + assert_respond_to Article, :more_views + end + + should "return the more recent label" do + a = Article.new + assert_equal "Created at: ", a.more_recent_label + end + + should "return no comments if profile has 0 comments" do + a = Article.new + assert_equal 0, a.comments_count + assert_equal "no comments", a.more_comments_label + end + + should "return 1 comment on label if the content has 1 comment" do + a = Article.new(:comments_count => 1) + assert_equal 1, a.comments_count + assert_equal "one comment", a.more_comments_label + end + + should "return number of comments on label if the content has more than one comment" do + a = Article.new(:comments_count => 4) + assert_equal 4, a.comments_count + assert_equal "4 comments", a.more_comments_label + end + + should "return no views if profile has 0 views" do + a = Article.new + assert_equal 0, a.hits + assert_equal "no views", a.more_views_label + end + + should "return 1 view on label if the content has 1 view" do + a = Article.new(:hits => 1) + assert_equal 1, a.hits + assert_equal "one view", a.more_views_label + end + + should "return number of views on label if the content has more than one view" do + a = Article.new(:hits => 4) + assert_equal 4, a.hits + assert_equal "4 views", a.more_views_label + end + + should 'return only text articles' do + Article.delete_all + + c1 = fast_create(TinyMceArticle, :name => 'Testing article 1', :body => 'Article body 1', :profile_id => profile.id) + c2 = fast_create(TextArticle, :name => 'Testing article 2', :body => 'Article body 2', :profile_id => profile.id) + c3 = fast_create(Event, :name => 'Testing article 3', :body => 'Article body 3', :profile_id => profile.id) + c4 = fast_create(RssFeed, :name => 'Testing article 4', :body => 'Article body 4', :profile_id => profile.id) + c5 = fast_create(TextileArticle, :name => 'Testing article 5', :body => 'Article body 5', :profile_id => profile.id) + + assert_equal [c1,c2,c5], Article.text_articles + end + end -- libgit2 0.21.2