Commit c4c81bc9f4653b4ea5595d611a5b223e3e881e9d

Authored by Aurélio A. Heckert
1 parent 02eedc73

Better support for blogs on ArticleBlocks

This commit has the participation of Lucas Prado Melo

It includes:
* Make blog pagination link to absolute path
* Configure blog display on block view
* List blogs on article block selection
* Auto select the first text article (if there is one)
* Testing!
app/helpers/blog_helper.rb
@@ -18,7 +18,8 @@ module BlogHelper @@ -18,7 +18,8 @@ module BlogHelper
18 pagination = will_paginate(articles, { 18 pagination = will_paginate(articles, {
19 :param_name => 'npage', 19 :param_name => 'npage',
20 :previous_label => _('« Newer posts'), 20 :previous_label => _('« Newer posts'),
21 - :next_label => _('Older posts »') 21 + :next_label => _('Older posts »'),
  22 + :params => {:action=>"view_page", :page=>articles.first.parent.path.split('/'), :controller=>"content_viewer"}
22 }) 23 })
23 content = [] 24 content = []
24 artic_len = articles.length 25 artic_len = articles.length
app/models/article_block.rb
@@ -49,4 +49,14 @@ class ArticleBlock < Block @@ -49,4 +49,14 @@ class ArticleBlock < Block
49 self.box.owner.kind_of?(Environment) ? self.box.owner.portal_community.articles : self.box.owner.articles 49 self.box.owner.kind_of?(Environment) ? self.box.owner.portal_community.articles : self.box.owner.articles
50 end 50 end
51 51
  52 + def posts_per_page
  53 + self.settings[:posts_per_page] or 1
  54 + end
  55 +
  56 + def posts_per_page= value
  57 + value = value.to_i
  58 + self.settings[:posts_per_page] = value if value > 0
  59 + end
  60 +
  61 + settings_items :visualization_format, :type => :string, :default => 'short'
52 end 62 end
app/views/box_organizer/_article_block.rhtml
1 -<div class='article-block-edition'> 1 +<div class="article-block-edition">
2 <% if @block.box.owner.kind_of?(Environment) and @block.box.owner.portal_community.nil? %> 2 <% if @block.box.owner.kind_of?(Environment) and @block.box.owner.portal_community.nil? %>
3 - <p id='no_portal_community'> 3 + <p id="no_portal_community">
4 <%= _("You don't have an community defined as the portal community. Define it before use this block properly.") %> 4 <%= _("You don't have an community defined as the portal community. Define it before use this block properly.") %>
5 </p> 5 </p>
6 <% else %> 6 <% else %>
7 - <% articles = @block.available_articles.select {|article| !article.folder? } %>  
8 - <%= select_tag('block[article_id]', options_for_select_with_title(articles.map {|item| [item.path, item.id]}, @block.article ? @block.article.id : nil)) %> 7 + <%
  8 + articles = @block.available_articles.select {|a| !a.folder? || a.blog? }
  9 + firstText = articles[articles.find_index{|a| a.kind_of?TextArticle}||-1]
  10 + selected = @block.article || firstText
  11 + %>
  12 + <%= select_tag('block[article_id]', options_for_select_with_title(articles.map {|item| [item.path, item.id]}, selected.id), :onchange => 'jQuery("#block_blog_options").toggle(this.blogs.indexOf(this.value) != -1)') %>
  13 + <div id="block_blog_options">
  14 + <%= labelled_form_field( _('Number of posts:'), text_field_tag('block[posts_per_page]', @block.posts_per_page) ) %>
  15 + <%= labelled_form_field( _('How to display posts:'), select_tag('block[visualization_format]', options_for_select([[_('First paragraph'), 'short'], [_('Full post'), 'full']], @block.visualization_format))) %>
  16 + </div>
  17 + <% blogs = @block.available_articles.select{|a|a.blog?} %>
  18 + <script>
  19 + jQuery("#block_article_id")[0].blogs = <%= blogs.map{|b| b.id.to_s }.to_json %>;
  20 + jQuery("#block_blog_options").toggle(<%= blogs.include?(selected) %>);
  21 + </script>
9 <% end %> 22 <% end %>
10 </div> 23 </div>
app/views/content_viewer/blog_page.rhtml
@@ -11,9 +11,11 @@ @@ -11,9 +11,11 @@
11 <div class="blog-posts"> 11 <div class="blog-posts">
12 <%= 12 <%=
13 posts = @posts 13 posts = @posts
  14 + format = blog.visualization_format
14 if inside_block 15 if inside_block
15 - posts = blog.posts.paginate(:page=>1, :per_page=>1) 16 + posts = blog.posts.paginate(:page=>1, :per_page=>inside_block.posts_per_page)
  17 + format = inside_block.visualization_format
16 end 18 end
17 - (blog.empty? ? content_tag('em', _('(no posts)')) : list_posts(posts, blog.visualization_format)) 19 + (blog.empty? ? content_tag('em', _('(no posts)')) : list_posts(posts, format))
18 %> 20 %>
19 </div> 21 </div>
test/integration/blocks_integration_test.rb
1 require "#{File.dirname(__FILE__)}/../test_helper" 1 require "#{File.dirname(__FILE__)}/../test_helper"
2 2
3 class BlocksIntegrationTest < ActionController::IntegrationTest 3 class BlocksIntegrationTest < ActionController::IntegrationTest
4 -  
5 - should "allow blog as block content" do 4 + def blog_on_article_block_bootstrap
6 profile = fast_create(Profile) 5 profile = fast_create(Profile)
7 blog = fast_create(Blog, :name => 'Blog', :profile_id => profile.id) 6 blog = fast_create(Blog, :name => 'Blog', :profile_id => profile.id)
8 - post = fast_create(TinyMceArticle, :name => "A Post", :profile_id => profile.id, :parent_id => blog.id, :body => 'Lorem ipsum dolor sit amet') 7 + fast_create(TinyMceArticle, :name => "First Post", :profile_id => profile.id, :parent_id => blog.id, :body => '<p> Wasserstoffbombe </p>')
  8 + fast_create(TinyMceArticle, :name => "A Post", :profile_id => profile.id, :parent_id => blog.id, :body => '<p>Lorem ipsum dolor sit amet</p> <p>Second paragraph</p>')
9 block = ArticleBlock.new 9 block = ArticleBlock.new
10 block.article = blog 10 block.article = blog
11 profile.boxes << Box.new 11 profile.boxes << Box.new
12 profile.boxes.first.blocks << block 12 profile.boxes.first.blocks << block
13 -  
14 - get "/profile/#{profile.identifier}" 13 + return block
  14 + end
  15 +
  16 + should 'allow blog as article block content' do
  17 + block = blog_on_article_block_bootstrap
  18 + get "/profile/#{block.owner.identifier}"
  19 + assert_match(/Lorem ipsum dolor sit amet/, @response.body)
  20 + end
  21 +
  22 + should 'display short version for block posts on article block' do
  23 + block = blog_on_article_block_bootstrap
  24 + get "/profile/#{block.owner.identifier}"
  25 + assert_no_match(/Second paragraph/, @response.body)
  26 + end
  27 +
  28 + should 'display full version for block posts on article block' do
  29 + block = blog_on_article_block_bootstrap
  30 + block.visualization_format = 'full'
  31 + block.save!
  32 + get "/profile/#{block.owner.identifier}"
  33 + assert_match(/Second paragraph/, @response.body)
  34 + end
  35 +
  36 + should 'display configured number of blog posts on article block' do
  37 + block = blog_on_article_block_bootstrap
  38 + block.posts_per_page = 2
  39 + block.save!
  40 + get "/profile/#{block.owner.identifier}"
15 assert_match(/Lorem ipsum dolor sit amet/, @response.body) 41 assert_match(/Lorem ipsum dolor sit amet/, @response.body)
  42 + assert_match(/Wasserstoffbombe/, @response.body)
  43 + end
  44 +
  45 + should 'link correctly in pagination' do
  46 + block = blog_on_article_block_bootstrap
  47 + p = block.owner
  48 + b = block.article
  49 + f = fast_create(Folder, :name => 'Folder1', :profile_id => p.id)
  50 + b.parent = f
  51 + b.save!
  52 + get "/profile/#{block.owner.identifier}"
  53 + assert_tag :tag => 'a', :attributes => { :href => "/#{p.identifier}/#{f.slug}/#{b.slug}?npage=2" }
16 end 54 end
17 55
18 end 56 end