diff --git a/app/helpers/blog_helper.rb b/app/helpers/blog_helper.rb index 754d385..6d829fe 100644 --- a/app/helpers/blog_helper.rb +++ b/app/helpers/blog_helper.rb @@ -14,4 +14,25 @@ module BlogHelper _('Edit blog') end + def list_posts(user, articles) + pagination = will_paginate(articles, { + :param_name => 'npage', + :prev_label => _('« Newer posts'), + :next_label => _('Older posts »') + }) + content = [] + articles.map{ |i| + css_add = '' + if i.published? || (user==i.profile) + css_add = '-not-published' if !i.published? + content << content_tag('div', display_post(i), :class => 'blog-post' + css_add, :id => "post-#{i.id}") + end + } + content.join("\n") + (pagination or '') + end + + def display_post(article) + article_title(article) + content_tag('p', article.to_html) + + content_tag('p', link_to( number_of_comments(article), article.url.merge(:form => 'opened', :anchor => 'comment_form') ), :class => 'metadata') + end end diff --git a/app/helpers/content_viewer_helper.rb b/app/helpers/content_viewer_helper.rb index 6b3dcbc..7d2155a 100644 --- a/app/helpers/content_viewer_helper.rb +++ b/app/helpers/content_viewer_helper.rb @@ -1,6 +1,7 @@ module ContentViewerHelper include GetText + include BlogHelper def number_of_comments(article) n = article.comments.size @@ -24,37 +25,10 @@ module ContentViewerHelper title end - def list_posts(articles) - pagination = will_paginate(articles, { - :param_name => 'npage', - :page_links => false, - :prev_label => _('Newer posts »'), - :next_label => _('« Older posts') - }) - articles.map{ |i| content_tag('div', display_post(i), :class => 'blog-post', :id => "post-#{i.id}") }.join("\n") + - (pagination or '') - end - - def display_post(article) - article_title(article) + content_tag('p', article.to_html) + - content_tag('p', link_to( number_of_comments(article), article.url.merge(:form => 'opened', :anchor => 'comment_form') ), :class => 'metadata') - end - def article_to_html(article) - content = article.to_html + content = article.to_html(:page => params[:npage]) return self.instance_eval(&content) if content.kind_of?(Proc) - - if article.blog? - children = if article.filter and article.filter[:year] and article.filter[:month] - filter_date = DateTime.parse("#{article.filter[:year]}-#{article.filter[:month]}-01") - article.posts.paginate :page => params[:npage], :per_page => article.posts_per_page, :conditions => [ 'created_at between ? and ?', filter_date, filter_date + 1.month - 1.day ] - else - article.posts.paginate :page => params[:npage], :per_page => article.posts_per_page - end - content + (children.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(children)) - else - content - end + content end end diff --git a/app/models/article.rb b/app/models/article.rb index 9fbf25f..bcddb18 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -127,7 +127,7 @@ class Article < ActiveRecord::Base # The implementation in this class just provides the +body+ attribute as the # HTML. Other article types can override this method to provide customized # views of themselves. - def to_html + def to_html(options = {}) body end diff --git a/app/models/blog.rb b/app/models/blog.rb index 646d334..d0de39b 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -28,8 +28,8 @@ 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 - content_tag('div', body) + tag('hr') + def to_html(options = {}) + posts_list(options[:page]) end def folder? @@ -52,4 +52,16 @@ class Blog < Folder end 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 => [ 'created_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 end diff --git a/app/models/enterprise_homepage.rb b/app/models/enterprise_homepage.rb index 9b68f5e..d6908fc 100644 --- a/app/models/enterprise_homepage.rb +++ b/app/models/enterprise_homepage.rb @@ -20,7 +20,7 @@ class EnterpriseHomepage < Article include EnterpriseHomepageHelper include CatalogHelper - def to_html + def to_html(options ={}) products = self.profile.products display_profile_info(self.profile) + content_tag('div', self.body || '') + (self.profile.environment.enabled?('disable_products_for_enterprises') ? '' : display_products_list(self.profile, products)) diff --git a/app/models/event.rb b/app/models/event.rb index 2b0b8c8..1ee1f79 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -58,7 +58,7 @@ class Event < Article include ActionController::UrlWriter include DatesHelper - def to_html + def to_html(options = {}) result = '' html = Builder::XmlMarkup.new(:target => result) diff --git a/app/models/folder.rb b/app/models/folder.rb index 89e55b8..8198f55 100644 --- a/app/models/folder.rb +++ b/app/models/folder.rb @@ -34,7 +34,7 @@ class Folder < Article include FolderHelper include DatesHelper - def to_html + def to_html(options = {}) send(view_as) end diff --git a/app/models/rss_feed.rb b/app/models/rss_feed.rb index 9936786..67f8342 100644 --- a/app/models/rss_feed.rb +++ b/app/models/rss_feed.rb @@ -50,7 +50,7 @@ class RssFeed < Article validates_inclusion_of :feed_item_description, :in => [ 'body', 'abstract' ], :if => :feed_item_description # TODO - def to_html + def to_html(options = {}) end # RSS feeds have type =text/xml=. @@ -61,7 +61,7 @@ class RssFeed < Article include ActionController::UrlWriter def fetch_articles if parent && parent.blog? - return parent.posts.find(:all, :limit => self.limit, :order => 'id desc') + return parent.posts.find(:all, :conditions => ['published = ?', true], :limit => self.limit, :order => 'id desc') end articles = diff --git a/app/models/textile_article.rb b/app/models/textile_article.rb index 96947e3..895a540 100644 --- a/app/models/textile_article.rb +++ b/app/models/textile_article.rb @@ -8,7 +8,7 @@ class TextileArticle < TextArticle _('Accessible alternative for visually impaired users.') end - def to_html + def to_html(options ={}) RedCloth.new(self.body|| '').to_html end diff --git a/app/models/uploaded_file.rb b/app/models/uploaded_file.rb index 0937d00..bf8ad5f 100644 --- a/app/models/uploaded_file.rb +++ b/app/models/uploaded_file.rb @@ -50,7 +50,7 @@ class UploadedFile < Article # FIXME isn't this too much including just to be able to generate some HTML? include ActionView::Helpers::TagHelper - def to_html + def to_html(options = {}) tag('img', :src => public_filename, :class => css_class_name, :style => 'max-width: 100%') if image? end diff --git a/app/views/content_viewer/blog_page.rhtml b/app/views/content_viewer/blog_page.rhtml new file mode 100644 index 0000000..f1bda69 --- /dev/null +++ b/app/views/content_viewer/blog_page.rhtml @@ -0,0 +1,5 @@ +
<%= article.body %>
+
+<%= (children.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(user, children)) %> + + diff --git a/public/stylesheets/article.css b/public/stylesheets/article.css index e7d1f99..cd60bce 100644 --- a/public/stylesheets/article.css +++ b/public/stylesheets/article.css @@ -202,10 +202,38 @@ #content #article .pagination .prev_page { position: absolute; - right: 0; + left: 0; } #content #article .pagination .next_page { position: absolute; - left: 0; + right: 0; +} +.msie6 #content #article .pagination .prev_page, +.msie6 #content #article .pagination .next_page { + position: relative; + display: inline; +} + +/* NOT PUBLISHED BLOG POSTS */ + +.blog-post-not-published { + background: url(/images/hachure.png); + opacity: 0.25; + filter: alpha(opacity=25); + zoom: 1; +} + +.blog-post-not-published a { + text-decoration: none; +} + +#content .blog-post-not-published .created-at { + text-align: left; +} + +.blog-post-not-published .metadata { + display: block; + text-align: center; + font-size: small; } diff --git a/test/unit/blog_helper_test.rb b/test/unit/blog_helper_test.rb index cab4ec5..ba9111a 100644 --- a/test/unit/blog_helper_test.rb +++ b/test/unit/blog_helper_test.rb @@ -6,12 +6,64 @@ class BlogHelperTest < Test::Unit::TestCase def setup stubs(:show_date).returns('') + @environment = Environment.default @profile = create_user('blog_helper_test').person + @blog = Blog.create!(:profile => profile, :name => 'Blog test') end + attr :profile + attr :blog + + def _(s); s; end + + should 'list published posts with class blog-post' do + blog.children << published_post = TextileArticle.create!(:name => 'Post', :profile => profile, :parent => blog, :published => true) - should 'add real tests' do - assert true + expects(:display_post).with(anything).returns('POST') + expects(:content_tag).with('div', 'POST', :class => 'blog-post', :id => "post-#{published_post.id}").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).returns('POST') + expects(:content_tag).with('div', 'POST', :class => 'blog-post-not-published', :id => "post-#{unpublished_post.id}").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).returns('POST') + expects(:content_tag).with('div', 'POST', :class => 'blog-post', :id => "post-#{published_post.id}").returns('RESULT') + expects(:content_tag).with('div', 'POST', :class => 'blog-post-not-published', :id => "post-#{unpublished_post.id}").never + + assert_equal 'RESULT', list_posts(nil, blog.posts) + end + + 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(:content_tag).with('p', article.to_html).returns(' TO_HTML') + expects(:number_of_comments).with(article).returns('NUMBER OF COMMENTS') + expects(:content_tag).with('p', 'NUMBER OF COMMENTS', anything).returns(' COMMENTS').at_least_once + + assert_equal 'TITLE TO_HTML COMMENTS', display_post(article) + end + + def will_paginate(arg1, arg2) + end + + def link_to(content, url) + content + end + + def content_tag(tag, content, options = {}) + "<#{tag}>#{content}" + end end diff --git a/test/unit/content_viewer_helper_test.rb b/test/unit/content_viewer_helper_test.rb index 34135db..5d5de44 100644 --- a/test/unit/content_viewer_helper_test.rb +++ b/test/unit/content_viewer_helper_test.rb @@ -56,7 +56,7 @@ class ContentViewerHelperTest < Test::Unit::TestCase should 'not list feed article' do profile.articles << Blog.new(:name => 'Blog test') 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 @@ -75,10 +75,11 @@ class ContentViewerHelperTest < Test::Unit::TestCase 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_match /November post/, result - assert_no_match /September post/, result + assert_equal 'BLI', result end end diff --git a/test/unit/rss_feed_test.rb b/test/unit/rss_feed_test.rb index bf45035..a05bc7f 100644 --- a/test/unit/rss_feed_test.rb +++ b/test/unit/rss_feed_test.rb @@ -121,6 +121,20 @@ class RssFeedTest < Test::Unit::TestCase assert_equal [posts[5], posts[4], posts[3], posts[2], posts[1]], feed.fetch_articles end + should 'list only published posts from blog' do + profile = create_user('testuser').person + blog = Blog.create(:name => 'blog', :profile => profile) + posts = [] + 5.times do |i| + posts << TextArticle.create!(:name => "post #{i}", :profile => profile, :parent => blog) + end + posts[0].published = false + posts[0].save! + + assert_equal [posts[4], posts[3], posts[2], posts[1]], blog.feed.fetch_articles + end + + should 'provide link to profile' do profile = create_user('testuser').person feed = RssFeed.new(:name => 'testfeed') -- libgit2 0.21.2