From 301ce771cc271c7fd9bbd147f17e1cfcfda48e72 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Mon, 9 Aug 2010 18:13:06 -0300 Subject: [PATCH] Revert "Blog paginate only with the available post for the user" --- app/controllers/public/content_viewer_controller.rb | 8 +------- app/helpers/blog_helper.rb | 24 +++++++++++++----------- app/models/article.rb | 17 ----------------- app/models/blog.rb | 18 +++++++++++++++--- app/models/event.rb | 11 +++++++++++ app/views/content_viewer/blog_page.rhtml | 10 +++++----- test/functional/content_viewer_controller_test.rb | 35 ++++++----------------------------- test/unit/article_test.rb | 18 ------------------ test/unit/blog_helper_test.rb | 25 +++++++++++++++++++++++-- test/unit/blog_test.rb | 7 +++++++ test/unit/content_viewer_helper_test.rb | 24 +++++++++++++++++++++++- 11 files changed, 104 insertions(+), 93 deletions(-) diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 0c1ab29..dcf55cc 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -78,13 +78,7 @@ class ContentViewerController < ApplicationController end if @page.blog? - posts = if params[:year] and params[:month] - filter_date = DateTime.parse("#{params[:year]}-#{params[:month]}-01") - @page.posts.by_range(filter_date..Article.last_day_of_month(filter_date)) - else - @page.posts - end - @posts = available_articles(posts, user).paginate :page => params[:npage], :per_page => @page.posts_per_page + @page.filter = {:year => params[:year], :month => params[:month]} end if @page.folder? && @page.view_as == 'image_gallery' diff --git a/app/helpers/blog_helper.rb b/app/helpers/blog_helper.rb index a10f1b0..ecab000 100644 --- a/app/helpers/blog_helper.rb +++ b/app/helpers/blog_helper.rb @@ -14,7 +14,7 @@ module BlogHelper _('Edit blog') end - def list_posts(articles, format = 'full') + def list_posts(user, articles, format = 'full') pagination = will_paginate(articles, { :param_name => 'npage', :prev_label => _('« Newer posts'), @@ -25,16 +25,18 @@ module BlogHelper articles.each_with_index{ |art,i| css_add = [ 'position-'+(i+1).to_s() ] position = (i%2 == 0) ? 'odd-post' : 'even-post' - css_add << 'first' if i == 0 - css_add << 'last' if i == (artic_len-1) - css_add << 'not-published' if !art.published? - css_add << position + '-inner' - content << content_tag('div', - content_tag('div', - display_post(art, format) + '
', - :class => 'blog-post ' + css_add.join(' '), - :id => "post-#{art.id}"), :class => position - ) + if art.published? || (user==art.profile) + css_add << 'first' if i == 0 + css_add << 'last' if i == (artic_len-1) + css_add << 'not-published' if !art.published? + css_add << position + '-inner' + content << content_tag('div', + content_tag('div', + display_post(art, format) + '
', + :class => 'blog-post ' + css_add.join(' '), + :id => "post-#{art.id}"), :class => position + ) + end } content.join("\n
\n") + (pagination or '') end diff --git a/app/models/article.rb b/app/models/article.rb index f1d3ab1..ccf496e 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -32,23 +32,6 @@ class Article < ActiveRecord::Base {:include => 'categories', :conditions => { 'categories.id' => category.id }} } - named_scope :by_range, lambda { |range| { - :conditions => [ - 'published_at BETWEEN :start_date AND :end_date', { :start_date => range.first, :end_date => range.last } - ] - }} - - def self.first_day_of_month(date) - date ||= Date.today - Date.new(date.year, date.month, 1) - end - - def self.last_day_of_month(date) - date ||= Date.today - date >>= 1 - Date.new(date.year, date.month, 1) - 1.day - end - URL_FORMAT = /\A(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?\Z/ix validates_format_of :external_link, :with => URL_FORMAT, :if => lambda { |article| !article.external_link.blank? } diff --git a/app/models/blog.rb b/app/models/blog.rb index 4b15ff1..ac8b7b2 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -3,6 +3,7 @@ class Blog < Folder has_many :posts, :class_name => 'Article', :foreign_key => 'parent_id', :source => :children, :conditions => [ 'type != ?', 'RssFeed' ], :order => 'published_at DESC, id DESC' attr_accessor :feed_attrs + attr_accessor :filter after_create do |blog| blog.children << RssFeed.new(:name => 'feed', :profile => blog.profile) @@ -22,9 +23,7 @@ class Blog < Folder # FIXME isn't this too much including just to be able to generate some HTML? include ActionView::Helpers::TagHelper def to_html(options = {}) - lambda do - render :file => 'content_viewer/blog_page' - end + posts_list(options[:page]) end def folder? @@ -50,6 +49,19 @@ class Blog < Folder self.feed end + def posts_list(npage) + article = self + children = if filter and filter[:year] and filter[:month] + filter_date = DateTime.parse("#{filter[:year]}-#{filter[:month]}-01") + posts.paginate :page => npage, :per_page => posts_per_page, :conditions => [ 'published_at between ? and ?', filter_date, filter_date + 1.month - 1.day ] + else + posts.paginate :page => npage, :per_page => posts_per_page + end + lambda do + render :file => 'content_viewer/blog_page', :locals => {:article => article, :children => children} + end + end + has_one :external_feed, :foreign_key => 'blog_id', :dependent => :destroy attr_accessor :external_feed_data diff --git a/app/models/event.rb b/app/models/event.rb index c3264de..f33fdc1 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -56,6 +56,17 @@ class Event < Article first_day..last_day end + def self.first_day_of_month(date) + date ||= Date.today + Date.new(date.year, date.month, 1) + end + + def self.last_day_of_month(date) + date ||= Date.today + date >>= 1 + Date.new(date.year, date.month, 1) - 1.day + end + def date_range start_date..(end_date||start_date) end diff --git a/app/views/content_viewer/blog_page.rhtml b/app/views/content_viewer/blog_page.rhtml index b9164a8..4a671b5 100644 --- a/app/views/content_viewer/blog_page.rhtml +++ b/app/views/content_viewer/blog_page.rhtml @@ -1,14 +1,14 @@ -<% add_rss_feed_to_head(@page.name, @page.feed.url) if @page.blog? && @page.feed %> +<% add_rss_feed_to_head(article.name, article.feed.url) if article.blog? && article.feed %> -<%= content_tag('em', _('(external feed was not loaded yet)'), :id => 'external-feed-info', :class => 'metadata') if @page.blog? && @page.external_feed && @page.external_feed.enabled && @page.external_feed.fetched_at.nil? %> +<%= content_tag('em', _('(external feed was not loaded yet)'), :id => 'external-feed-info', :class => 'metadata') if article.blog? && article.external_feed && article.external_feed.enabled && article.external_feed.fetched_at.nil? %>
- <%= link_to(image_tag('icons-mime/rss-feed.png'), @page.feed.url, :class => 'blog-feed-link') if @page.blog? && @page.feed %> + <%= link_to(image_tag('icons-mime/rss-feed.png'), article.feed.url, :class => 'blog-feed-link') if article.blog? && article.feed %>
- <%= @page.body %> + <%= article.body %>

- <%= (@posts.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(@posts, @page.visualization_format)) %> + <%= (children.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(user, children, article.visualization_format)) %>
diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index ee606d7..607f6a6 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -603,27 +603,10 @@ class ContentViewerControllerTest < Test::Unit::TestCase assert_response :missing end - should 'list unpublished posts to owner with a different class' do - login_as('testinguser') - blog = Blog.create!(:name => 'A blog test', :profile => profile) - blog.posts << TextileArticle.create!(:name => 'Post', :profile => profile, :parent => blog, :published => false) - - get :view_page, :profile => profile.identifier, :page => [blog.path] - assert_tag :tag => 'div', :attributes => {:class => /not-published/} - end - - should 'not list unpublished posts to a not logged person' do - blog = Blog.create!(:name => 'A blog test', :profile => profile) - blog.posts << TextileArticle.create!(:name => 'Post', :profile => profile, :parent => blog, :published => false) - - get :view_page, :profile => profile.identifier, :page => [blog.path] - assert_no_tag :tag => 'a', :content => "Post" - end - should 'display pagination links of blog' do blog = Blog.create!(:name => 'A blog test', :profile => profile, :posts_per_page => 5) for n in 1..10 - blog.posts << TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => blog) + blog.children << TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => blog) end assert_equal 10, blog.posts.size @@ -631,17 +614,11 @@ class ContentViewerControllerTest < Test::Unit::TestCase assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{blog.path}?npage=2", :rel => 'next' } end - should 'display filtered posts' do - blog = Blog.create!(:name => 'A blog test', :profile => profile) - not_display_post = TextileArticle.new(:name => "Post 1", :profile => profile, :parent => blog) - display_post = TextileArticle.new(:name => "Post 2", :profile => profile, :parent => blog) - - not_display_post.update_attribute(:published_at, DateTime.parse('2009-09-10')) - display_post.update_attribute(:published_at, DateTime.parse('2010-09-10')) - - get :view_page, :profile => profile.identifier, :page => [blog.path], :year => 2010, :month => 9 - assert_no_tag :tag => 'a', :content => "Post 1" - assert_tag :tag => 'a', :content => "Post 2" + should 'set year and month filter from URL params' do + profile.articles << Blog.new(:name => 'A blog test', :profile => profile) + year, month = profile.blog.created_at.year.to_s, '%02d' % profile.blog.created_at.month + get :view_page, :profile => profile.identifier, :page => [profile.blog.path], :year => year, :month => month + assert_equal({ :year => year.to_s, :month => month.to_s }, assigns(:page).filter) end should 'give link to create new article inside folder when view child of folder' do diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 1fc64a7..588a83e 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -873,22 +873,4 @@ class ArticleTest < Test::Unit::TestCase assert_no_match /[<>]/, article.name end - should 'found articles with published date between a range' do - start_date = DateTime.parse('2010-07-06') - end_date = DateTime.parse('2010-08-02') - - article_found1 = fast_create(Article, :published_at => start_date) - article_found2 = fast_create(Article, :published_at => end_date) - article_not_found = fast_create(Article, :published_at => end_date + 1.month) - - assert_includes Article.by_range(start_date..end_date), article_found1 - assert_includes Article.by_range(start_date..end_date), article_found2 - assert_not_includes Article.by_range(start_date..end_date), article_not_found - end - - should 'calculate first/end day of a month' do - assert_equal 1, Article.first_day_of_month(DateTime.parse('2010-07-06')).day - assert_equal 31, Article.last_day_of_month(DateTime.parse('2010-07-06')).day - end - end diff --git a/test/unit/blog_helper_test.rb b/test/unit/blog_helper_test.rb index 4cbbf66..a87d71a 100644 --- a/test/unit/blog_helper_test.rb +++ b/test/unit/blog_helper_test.rb @@ -27,7 +27,28 @@ class BlogHelperTest < Test::Unit::TestCase expects(:content_tag).with('div', "POST
", :class => 'blog-post position-1 first last odd-post-inner', :id => "post-#{published_post.id}").returns('POST') expects(:content_tag).with('div', 'POST', {:class => 'odd-post'}).returns('RESULT') - assert_equal 'RESULT', list_posts(blog.posts) + assert_equal 'RESULT', list_posts(profile, blog.posts) + end + + should 'list unpublished posts to owner with a different class' do + blog.children << unpublished_post = TextileArticle.create!(:name => 'Post', :profile => profile, :parent => blog, :published => false) + + expects(:display_post).with(anything, anything).returns('POST') + expects(:content_tag).with('div', "POST
", :class => 'blog-post position-1 first last not-published odd-post-inner', :id => "post-#{unpublished_post.id}").returns('POST') + expects(:content_tag).with('div', 'POST', {:class => 'odd-post'}).returns('RESULT') + assert_equal 'RESULT', list_posts(profile, blog.posts) + end + + should 'not list unpublished posts to not owner' do + blog.children << unpublished_post = TextileArticle.create!(:name => 'First post', :profile => profile, :parent => blog, :published => false) + + blog.children << published_post = TextileArticle.create!(:name => 'Second post', :profile => profile, :parent => blog, :published => true) + + expects(:display_post).with(anything, anything).returns('POST') + expects(:content_tag).with('div', "POST
", has_entries(:id => "post-#{unpublished_post.id}")).never + expects(:content_tag).with('div', "POST
", has_entries(:id => "post-#{published_post.id}")).returns('POST') + expects(:content_tag).with('div', 'POST', {:class => 'odd-post'}).returns('RESULT') + assert_equal 'RESULT', list_posts(nil, blog.posts) end should 'list even/odd posts with a different class' do @@ -43,7 +64,7 @@ class BlogHelperTest < Test::Unit::TestCase expects(:content_tag).with('div', "POST
", :class => 'blog-post position-2 last even-post-inner', :id => "post-#{older_post.id}").returns('POST 2') expects(:content_tag).with('div', "POST 2", :class => 'even-post').returns('EVEN-POST') - assert_equal "ODD-POST\n
\nEVEN-POST", list_posts(blog.posts) + assert_equal "ODD-POST\n
\nEVEN-POST", list_posts(nil, blog.posts) end diff --git a/test/unit/blog_test.rb b/test/unit/blog_test.rb index 6f69311..7422ab0 100644 --- a/test/unit/blog_test.rb +++ b/test/unit/blog_test.rb @@ -82,6 +82,13 @@ class BlogTest < ActiveSupport::TestCase assert_equal [newer, older], blog.posts end + should 'has filter' do + p = create_user('testuser').person + blog = Blog.create!(:profile => p, :name => 'Blog test') + blog.filter = {:param => 'value'} + assert_equal 'value', blog.filter[:param] + end + should 'has one external feed' do p = create_user('testuser').person blog = fast_create(Blog, :profile_id => p.id, :name => 'Blog test') diff --git a/test/unit/content_viewer_helper_test.rb b/test/unit/content_viewer_helper_test.rb index b04d27e..a1bb321 100644 --- a/test/unit/content_viewer_helper_test.rb +++ b/test/unit/content_viewer_helper_test.rb @@ -57,10 +57,32 @@ class ContentViewerHelperTest < Test::Unit::TestCase should 'not list feed article' do profile.articles << Blog.new(:name => 'Blog test', :profile => profile) assert_includes profile.blog.children.map{|i| i.class}, RssFeed - result = list_posts(profile.blog.posts) + result = list_posts(nil, profile.blog.posts) assert_no_match /feed/, result end + should 'filter blog posts by date' do + blog = Blog.create!(:name => 'Blog test', :profile => profile) + + nov = TextileArticle.create!(:name => 'November post', :parent => blog, :profile => profile) + nov.update_attributes!(:published_at => DateTime.parse('2008-11-15')) + + sep = TextileArticle.create!(:name => 'September post', :parent => blog, :profile => profile) + sep.update_attribute(:published_at, DateTime.parse('2008-09-10')) + + blog.reload + blog.filter = {:year => 2008, :month => 11} + assert blog.save! + + self.stubs(:params).returns({:npage => nil}) + + expects(:render).with(:file => 'content_viewer/blog_page', :locals => {:article => blog, :children => [nov]}).returns("BLI") + + result = article_to_html(blog) + + assert_equal 'BLI', result + end + end def show_date(date) -- libgit2 0.21.2