diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 02a0946..483442a 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -78,7 +78,14 @@ class ContentViewerController < ApplicationController end if @page.blog? - @page.filter = {:year => params[:year], :month => params[:month]} + posts = if params[:year] and params[:month] + filter_date = DateTime.parse("#{params[:year]}-#{params[:month]}-01") + @page.posts.by_range(filter_date..filter_date.at_end_of_month) + else + @page.posts + end + + @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))) end if @page.folder? && @page.view_as == 'image_gallery' diff --git a/app/controllers/public/events_controller.rb b/app/controllers/public/events_controller.rb index 9cadf1c..5139a19 100644 --- a/app/controllers/public/events_controller.rb +++ b/app/controllers/public/events_controller.rb @@ -13,7 +13,7 @@ class EventsController < PublicController @events_of_the_day = profile.events.by_day(@selected_day) end - events = profile.events.by_range(Event.first_day_of_month(date - 1.month)..Event.last_day_of_month(date + 1.month)) + events = profile.events.by_range((date - 1.month).at_beginning_of_month..(date + 1.month).at_end_of_month) @calendar = populate_calendar(date, events) @previous_calendar = populate_calendar(date - 1.month, events) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index b4da66d..dfd2981 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -102,7 +102,7 @@ class SearchController < PublicController if month || year date = Date.new(year.to_i, month.to_i, 1) - result[:date_range] = (date - 1.month)..Event.last_day_of_month(date + 1.month) + result[:date_range] = (date - 1.month)..(date + 1.month).at_end_of_month end result diff --git a/app/helpers/blog_helper.rb b/app/helpers/blog_helper.rb index 4dd6519..97e4003 100644 --- a/app/helpers/blog_helper.rb +++ b/app/helpers/blog_helper.rb @@ -14,7 +14,7 @@ module BlogHelper _('Configure blog') end - def list_posts(user, articles, format = 'full') + def list_posts(articles, format = 'full') pagination = will_paginate(articles, { :param_name => 'npage', :prev_label => _('« Newer posts'), @@ -25,18 +25,16 @@ module BlogHelper articles.each_with_index{ |art,i| css_add = [ 'position-'+(i+1).to_s() ] position = (i%2 == 0) ? 'odd-post' : 'even-post' - 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 + 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 + ) } content.join("\n
\n") + (pagination or '') end diff --git a/app/models/article.rb b/app/models/article.rb index 8ca5a44..f61cfc1 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -38,6 +38,12 @@ 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 } + ] + }} + 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? } @@ -246,19 +252,28 @@ class Article < ActiveRecord::Base named_scope :folders, :conditions => { :type => ['Folder', 'Blog'] } named_scope :images, :conditions => { :is_image => true } + def self.display_filter(user, profile) + return {:conditions => ['published = ?', true]} if !user + {:conditions => [" articles.published = ? OR + articles.last_changed_by_id = ? OR + articles.profile_id = ? OR + ?", + true, user.id, user.id, user.has_permission?(:view_private_content, profile)] } + end + def display_unpublished_article_to?(user) - self.author == user || allow_view_private_content?(user) || user == self.profile || - user.is_admin?(self.profile.environment) || user.is_admin?(self.profile) + user == author || allow_view_private_content?(user) || user == profile || + user.is_admin?(profile.environment) || user.is_admin?(profile) end - def display_to?(user) - if self.published? - self.profile.display_info_to?(user) + def display_to?(user = nil) + if published? + profile.display_info_to?(user) else - if user.nil? + if !user false else - self.display_unpublished_article_to?(user) + display_unpublished_article_to?(user) end end end diff --git a/app/models/blog.rb b/app/models/blog.rb index ac8b7b2..09e6747 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -1,9 +1,8 @@ class Blog < Folder - has_many :posts, :class_name => 'Article', :foreign_key => 'parent_id', :source => :children, :conditions => [ 'type != ?', 'RssFeed' ], :order => 'published_at DESC, id DESC' + has_many :posts, :class_name => 'Article', :foreign_key => 'parent_id', :source => :children, :conditions => [ 'articles.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) @@ -23,7 +22,9 @@ 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 = {}) - posts_list(options[:page]) + lambda do + render :file => 'content_viewer/blog_page' + end end def folder? @@ -49,19 +50,6 @@ 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 a578fa5..6f103c0 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -64,17 +64,6 @@ 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 1ee345c..a18022c 100644 --- a/app/views/content_viewer/blog_page.rhtml +++ b/app/views/content_viewer/blog_page.rhtml @@ -1,13 +1,13 @@ -<% add_rss_feed_to_head(article.name, article.feed.url) if article.blog? && article.feed %> +<% add_rss_feed_to_head(@page.name, @page.feed.url) if @page.blog? && @page.feed %> -<%= 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? %> +<%= 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? %>
- <%= article.body %> + <%= @page.body %>

- <%= (children.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(user, children, article.visualization_format)) %> + <%= (@posts.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(@posts, @page.visualization_format)) %>
diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 803cb40..cfc9d15 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -603,10 +603,27 @@ 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.children << TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => blog) + blog.posts << TextileArticle.create!(:name => "Post #{n}", :profile => profile, :parent => blog) end assert_equal 10, blog.posts.size @@ -647,10 +664,20 @@ class ContentViewerControllerTest < Test::Unit::TestCase end should 'set year and month filter from URL params' do - profile.articles << Blog.new(:name => 'A blog test', :profile => profile) + blog = Blog.create!(:name => "blog", :profile => profile) + profile.articles << blog + + past_post = TextileArticle.create!(:name => "past post", :profile => profile, :parent => blog, :created_at => blog.created_at - 1.year) + actual_post = TextileArticle.create!(:name => "actual post", :profile => profile, :parent => blog) + blog.children << past_post + blog.children << actual_post + 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) + + assert_no_tag :tag => 'a', :content => past_post.title + assert_tag :tag => 'a', :content => actual_post.title end should 'give link to create new article inside folder when view child of folder' do diff --git a/test/integration/performance_test.rb b/test/integration/performance_test.rb index 221c1a9..176cf6b 100644 --- a/test/integration/performance_test.rb +++ b/test/integration/performance_test.rb @@ -28,6 +28,10 @@ class PerformanceTest < ActionController::IntegrationTest person2 = create_profile('person2') create_posts(person2, 100) + get '/person0/blog' + get '/person1/blog' + get '/person2/blog' + # no posts time0 = (Benchmark.measure { 10.times { get '/person0/blog' } }) diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 6ed2869..33e39db 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1193,4 +1193,22 @@ class ArticleTest < Test::Unit::TestCase assert_equal 6, ActionTrackerNotification.count 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, (DateTime.parse('2010-07-06')).at_beginning_of_month.day + assert_equal 31, (DateTime.parse('2010-07-06')).at_end_of_month.day + end + end diff --git a/test/unit/blog_helper_test.rb b/test/unit/blog_helper_test.rb index a87d71a..4cbbf66 100644 --- a/test/unit/blog_helper_test.rb +++ b/test/unit/blog_helper_test.rb @@ -27,28 +27,7 @@ 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(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) + assert_equal 'RESULT', list_posts(blog.posts) end should 'list even/odd posts with a different class' do @@ -64,7 +43,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(nil, blog.posts) + assert_equal "ODD-POST\n
\nEVEN-POST", list_posts(blog.posts) end diff --git a/test/unit/blog_test.rb b/test/unit/blog_test.rb index 7422ab0..6f69311 100644 --- a/test/unit/blog_test.rb +++ b/test/unit/blog_test.rb @@ -82,13 +82,6 @@ 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 a1bb321..b04d27e 100644 --- a/test/unit/content_viewer_helper_test.rb +++ b/test/unit/content_viewer_helper_test.rb @@ -57,32 +57,10 @@ 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(nil, profile.blog.posts) + result = list_posts(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