diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb
index 3f8c5ce..abcc58e 100644
--- a/app/controllers/public/content_viewer_controller.rb
+++ b/app/controllers/public/content_viewer_controller.rb
@@ -81,7 +81,13 @@ 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..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
end
if @page.folder? && @page.view_as == 'image_gallery'
diff --git a/app/helpers/blog_helper.rb b/app/helpers/blog_helper.rb
index ecab000..a10f1b0 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(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 ccf496e..f1d3ab1 100644
--- a/app/models/article.rb
+++ b/app/models/article.rb
@@ -32,6 +32,23 @@ 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 ac8b7b2..4b15ff1 100644
--- a/app/models/blog.rb
+++ b/app/models/blog.rb
@@ -3,7 +3,6 @@ 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)
@@ -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 f33fdc1..c3264de 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -56,17 +56,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 4a671b5..b9164a8 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(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? %>
- <%= link_to(image_tag('icons-mime/rss-feed.png'), article.feed.url, :class => 'blog-feed-link') if article.blog? && article.feed %>
+ <%= link_to(image_tag('icons-mime/rss-feed.png'), @page.feed.url, :class => 'blog-feed-link') if @page.blog? && @page.feed %>
- <%= 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 1602194..ea9ce3c 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
@@ -614,11 +631,17 @@ class ContentViewerControllerTest < Test::Unit::TestCase
assert_tag :tag => 'a', :attributes => { :href => "/#{profile.identifier}/#{blog.path}?npage=2", :rel => 'next' }
end
- 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)
+ 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"
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 9ed5c29..80df6f0 100644
--- a/test/unit/article_test.rb
+++ b/test/unit/article_test.rb
@@ -873,4 +873,22 @@ 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 7edf7bf..b21f7f0 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 7f73a31..bf4bd19 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 = Blog.create!(:profile => p, :name => 'Blog test')
diff --git a/test/unit/content_viewer_helper_test.rb b/test/unit/content_viewer_helper_test.rb
index a675c09..a66bcf1 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