From f7c784e9e6bec5b1a4a4f3490aa578fa448824f5 Mon Sep 17 00:00:00 2001
From: Daniela Soares Feitosa
Date: Fri, 28 May 2010 19:02:57 -0300
Subject: [PATCH] Adding option to choose how posts will be displayed
---
app/helpers/blog_helper.rb | 27 +++++++++++++++++++++++----
app/helpers/content_viewer_helper.rb | 5 +++--
app/models/article.rb | 2 +-
app/models/blog.rb | 4 ++++
app/views/cms/_blog.rhtml | 2 ++
app/views/content_viewer/blog_page.rhtml | 2 +-
public/stylesheets/application.css | 8 ++++++++
test/functional/cms_controller_test.rb | 6 ++++++
test/functional/content_viewer_controller_test.rb | 13 +++++++++++++
test/unit/blog_helper_test.rb | 38 ++++++++++++++++++++++++++++++++------
test/unit/blog_test.rb | 29 +++++++++++++++++++++++++++++
11 files changed, 122 insertions(+), 14 deletions(-)
diff --git a/app/helpers/blog_helper.rb b/app/helpers/blog_helper.rb
index 227340b..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(user, articles)
+ def list_posts(user, articles, format = 'full')
pagination = will_paginate(articles, {
:param_name => 'npage',
:prev_label => _('« Newer posts'),
@@ -32,7 +32,7 @@ module BlogHelper
css_add << position + '-inner'
content << content_tag('div',
content_tag('div',
- display_post(art) + '
',
+ display_post(art, format) + '
',
:class => 'blog-post ' + css_add.join(' '),
:id => "post-#{art.id}"), :class => position
)
@@ -41,10 +41,29 @@ module BlogHelper
content.join("\n
\n") + (pagination or '')
end
- def display_post(article)
+ def display_post(article, format = 'full')
+ no_comments = (format == 'full') ? false : true
+ html = send("display_#{format}_format", article)
+
+ article_title(article, :no_comments => no_comments) + html
+ end
+
+ def display_short_format(article)
+ html = content_tag('div',
+ article.first_paragraph +
+ content_tag('div',
+ link_to_comments(article) +
+ link_to( _('Read more'), article.url),
+ :class => 'read-more'),
+ :class => 'short-post'
+ )
+ html
+ end
+
+ def display_full_format(article)
html = article_to_html(article)
html = content_tag('p', html) if ! html.include?('
')
- article_title(article) + html
+ html
end
end
diff --git a/app/helpers/content_viewer_helper.rb b/app/helpers/content_viewer_helper.rb
index 90f3fe8..c4774d3 100644
--- a/app/helpers/content_viewer_helper.rb
+++ b/app/helpers/content_viewer_helper.rb
@@ -19,12 +19,13 @@ module ContentViewerHelper
unless args[:no_link]
title = content_tag('h1', link_to(article.name, article.url), :class => 'title')
end
- title << content_tag('span', _("%s, by %s - %s") % [show_date(article.published_at), link_to(article.author.name, article.author.url), link_to_comments(article)], :class => 'created-at')
+ comments = args[:no_comments] ? '' : (("- %s") % link_to_comments(article))
+ title << content_tag('span', _("%s, by %s %s") % [show_date(article.published_at), link_to(article.author.name, article.author.url), comments], :class => 'created-at')
end
title
end
- def link_to_comments(article)
+ def link_to_comments(article, args = {})
link_to( number_of_comments(article), article.url.merge(:anchor => 'comments_list') )
end
diff --git a/app/models/article.rb b/app/models/article.rb
index 489c462..28a2ccb 100644
--- a/app/models/article.rb
+++ b/app/models/article.rb
@@ -332,7 +332,7 @@ class Article < ActiveRecord::Base
def first_paragraph
to_html =~ /(.*<\/p>)/
- $1
+ $1 || ''
end
def self.find_tagged_with(tag)
diff --git a/app/models/blog.rb b/app/models/blog.rb
index 35b09f4..ac8b7b2 100644
--- a/app/models/blog.rb
+++ b/app/models/blog.rb
@@ -99,4 +99,8 @@ class Blog < Folder
end
end
+ settings_items :visualization_format, :type => :string, :default => 'full'
+ validates_inclusion_of :visualization_format, :in => [ 'full', 'short' ], :if => :visualization_format
+
+
end
diff --git a/app/views/cms/_blog.rhtml b/app/views/cms/_blog.rhtml
index f234b43..7db52f2 100644
--- a/app/views/cms/_blog.rhtml
+++ b/app/views/cms/_blog.rhtml
@@ -52,6 +52,8 @@
<%= labelled_form_field(_('Description:'), text_area(:article, :body, :cols => 64, :rows => 10)) %>
+<%= labelled_form_field(_('How to display posts:'), f.select(:visualization_format, [ [ _('Full post'), 'full'], [ _('First paragraph'), 'short'] ])) %>
+
<%= labelled_form_field(_('Posts per page:'), f.select(:posts_per_page, [5, 10, 20, 50, 100])) %>
<% f.fields_for 'feed', @article.feed do |feed| %>
diff --git a/app/views/content_viewer/blog_page.rhtml b/app/views/content_viewer/blog_page.rhtml
index 98f4563..4a671b5 100644
--- a/app/views/content_viewer/blog_page.rhtml
+++ b/app/views/content_viewer/blog_page.rhtml
@@ -10,5 +10,5 @@
- <%= (children.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(user, children)) %>
+ <%= (children.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(user, children, article.visualization_format)) %>
diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css
index 3a8df41..6cd20eb 100644
--- a/public/stylesheets/application.css
+++ b/public/stylesheets/application.css
@@ -1171,6 +1171,14 @@ a.comment-picture {
max-width: 100%;
}
+#content .blog-post .read-more {
+ text-align: right;
+ clear: both;
+}
+#content .blog-post .read-more a {
+ margin: 0 10px;
+}
+
/* NOT PUBLISHED BLOG POSTS */
.blog-post.not-published {
diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb
index 7231e09..4b4ec1a 100644
--- a/test/functional/cms_controller_test.rb
+++ b/test/functional/cms_controller_test.rb
@@ -736,6 +736,12 @@ class CmsControllerTest < Test::Unit::TestCase
assert_tag :tag => 'select', :attributes => { :name => 'article[posts_per_page]' }, :child => { :tag => 'option', :attributes => {:value => n, :selected => 'selected'} }
end
+ should 'display options for blog visualization with default value on edit blog' do
+ format = Blog.new.visualization_format
+ get :new, :profile => profile.identifier, :type => 'Blog'
+ assert_tag :tag => 'select', :attributes => { :name => 'article[visualization_format]' }, :child => { :tag => 'option', :attributes => {:value => 'full', :selected => 'selected'} }
+ end
+
should 'not offer to create special article types' do
get :new, :profile => profile.identifier
assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?type=Blog"}
diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb
index f8f8e7b..719aade 100644
--- a/test/functional/content_viewer_controller_test.rb
+++ b/test/functional/content_viewer_controller_test.rb
@@ -857,4 +857,17 @@ class ContentViewerControllerTest < Test::Unit::TestCase
assert_tag :tag => 'div', :attributes => { :class => 'post_comment_box opened' }
end
+ should 'show only first paragraph of blog posts if visualization_format is short' do
+ login_as(profile.identifier)
+
+ blog = Blog.create!(:name => 'A blog test', :profile => profile, :visualization_format => 'short')
+
+ blog.posts << TinyMceArticle.create!(:name => 'first post', :parent => blog, :profile => profile, :body => 'Content to be displayed.
Anything')
+
+ get :view_page, :profile => profile.identifier, :page => blog.explode_path
+
+ assert_tag :tag => 'div', :attributes => { :class => 'short-post'}, :content => /Content to be displayed./
+ assert_no_tag :tag => 'div', :attributes => { :class => 'short-post'}, :content => /Anything/
+ end
+
end
diff --git a/test/unit/blog_helper_test.rb b/test/unit/blog_helper_test.rb
index 203b2ef..7edf7bf 100644
--- a/test/unit/blog_helper_test.rb
+++ b/test/unit/blog_helper_test.rb
@@ -23,7 +23,7 @@ class BlogHelperTest < Test::Unit::TestCase
should 'list published posts with class blog-post' do
blog.children << published_post = TextileArticle.create!(:name => 'Post', :profile => profile, :parent => blog, :published => true)
- expects(:display_post).with(anything).returns('POST')
+ expects(:display_post).with(anything, anything).returns('POST')
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')
@@ -33,7 +33,7 @@ class BlogHelperTest < Test::Unit::TestCase
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).returns('POST')
+ 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)
@@ -44,7 +44,7 @@ class BlogHelperTest < Test::Unit::TestCase
blog.children << published_post = TextileArticle.create!(:name => 'Second post', :profile => profile, :parent => blog, :published => true)
- expects(:display_post).with(anything).returns('POST')
+ 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')
@@ -56,7 +56,7 @@ class BlogHelperTest < Test::Unit::TestCase
blog.children << newer_post = TextileArticle.create!(:name => 'Second post', :profile => profile, :parent => blog, :published => true)
- expects(:display_post).with(anything).returns('POST').times(2)
+ expects(:display_post).with(anything, anything).returns('POST').times(2)
expects(:content_tag).with('div', "POST
", :class => 'blog-post position-1 first odd-post-inner', :id => "post-#{newer_post.id}").returns('POST 1')
expects(:content_tag).with('div', "POST 1", :class => 'odd-post').returns('ODD-POST')
@@ -70,7 +70,7 @@ class BlogHelperTest < Test::Unit::TestCase
should 'display post' do
blog.children << article = TextileArticle.create!(:name => 'Second post', :profile => profile, :parent => blog, :published => true)
- expects(:article_title).with(article).returns('TITLE')
+ expects(:article_title).with(article, anything).returns('TITLE')
expects(:content_tag).with('p', article.to_html).returns(' TO_HTML')
self.stubs(:params).returns({:npage => nil})
@@ -79,13 +79,39 @@ class BlogHelperTest < Test::Unit::TestCase
should 'display empty post if body is nil' do
blog.children << article = fast_create(Article, :profile_id => profile.id, :parent_id => blog.id, :body => nil)
- expects(:article_title).with(article).returns('TITLE')
+ expects(:article_title).with(article, anything).returns('TITLE')
expects(:content_tag).with('p', '').returns('')
self.stubs(:params).returns({:npage => nil})
assert_equal 'TITLE', display_post(article)
end
+ should 'display full post by default' do
+ blog.children << article = fast_create(Article, :profile_id => profile.id, :parent_id => blog.id, :body => nil)
+ expects(:article_title).with(article, anything).returns('')
+ expects(:display_full_format).with(article).returns('FULL POST')
+
+ assert_equal 'FULL POST', display_post(article)
+ end
+
+ should 'no_comments is false if blog displays full post' do
+ blog.children << article = fast_create(Article, :profile_id => profile.id, :parent_id => blog.id, :body => nil)
+ expects(:article_title).with(article, :no_comments => false).returns('')
+ expects(:display_full_format).with(article).returns('FULL POST')
+
+ assert_equal 'FULL POST', display_post(article, 'full')
+ end
+
+ should 'no_comments is true if blog displays short post' do
+ blog.update_attribute(:visualization_format, 'short')
+ blog.children << article = fast_create(Article, :profile_id => profile.id, :parent_id => blog.id, :body => nil)
+ expects(:article_title).with(article, :no_comments => true).returns('')
+ expects(:display_short_format).with(article).returns('SHORT POST')
+
+ assert_equal 'SHORT POST', display_post(article, 'short')
+ end
+
+
should 'display link to file if post is an uploaded_file' do
file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => profile, :published => true, :parent => blog)
diff --git a/test/unit/blog_test.rb b/test/unit/blog_test.rb
index b055d07..7f73a31 100644
--- a/test/unit/blog_test.rb
+++ b/test/unit/blog_test.rb
@@ -149,4 +149,33 @@ class BlogTest < ActiveSupport::TestCase
assert_not_equal 'changed-name', blog.slug
end
+ should 'display full posts by default' do
+ blog = Blog.new
+ assert_equal 'full', blog.visualization_format
+ end
+
+ should 'update visualization_format setting' do
+ p = create_user('testuser').person
+ p.articles << Blog.new(:profile => p, :name => 'Blog test')
+ blog = p.blog
+ blog.visualization_format = 'short'
+ assert blog.save!
+ assert_equal 'short', p.blog.visualization_format
+ end
+
+ should 'allow only full and short as visualization_format' do
+ blog = Blog.new(:name => 'blog')
+ blog.visualization_format = 'wrong_format'
+ blog.valid?
+ assert blog.errors.invalid?(:visualization_format)
+
+ blog.visualization_format = 'short'
+ blog.valid?
+ assert !blog.errors.invalid?(:visualization_format)
+
+ blog.visualization_format = 'full'
+ blog.valid?
+ assert !blog.errors.invalid?(:visualization_format)
+ end
+
end
--
libgit2 0.21.2