Commit 60f17b72e70a105017f58ff689496ca1bbde9f54

Authored by Victor Costa
1 parent 5d7c6de0

Add pagination to context content block

plugins/context_content/controllers/profile/context_content_plugin_profile_controller.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +class ContextContentPluginProfileController < ProfileController
  2 + append_view_path File.join(File.dirname(__FILE__) + '/../../views')
  3 +
  4 + def view_content
  5 + block = Block.find(params[:id])
  6 + p = params[:page].to_i
  7 + contents = block.contents(profile.articles.find(params[:article_id]), p)
  8 +
  9 + render :update do |page|
  10 + page.replace_html "context_content_#{block.id}", :file => "blocks/context_content", :locals => {:block => block, :contents => contents}
  11 + page.replace_html "context_content_more_#{block.id}", :partial => 'blocks/more', :locals => {:block => block, :contents => contents, :article_id => params[:article_id] }
  12 + end
  13 + end
  14 +
  15 +end
... ...
plugins/context_content/lib/context_content_block.rb
... ... @@ -37,28 +37,36 @@ class ContextContentBlock &lt; Block
37 37 end
38 38 end
39 39  
40   - def contents(page)
  40 + def contents(page, p=1)
  41 + return @children unless @children.blank?
41 42 if page
42   - children = page.children.with_types(types).limit(limit)
43   - (children.blank? && show_parent_content) ? contents(page.parent) : children
  43 + @children = page.children.with_types(types).paginate(:per_page => limit, :page => p)
  44 + (@children.blank? && show_parent_content) ? contents(page.parent) : @children
44 45 else
45 46 nil
46 47 end
47 48 end
48 49  
49   -# FIXME
50   -# def footer
51   -# lambda do
52   -# link_to(_('View all'), '')
53   -# end
54   -# end
  50 + def footer
  51 + block = self
  52 + lambda do
  53 + if @page
  54 + contents = block.contents(@page)
  55 + content_tag('div',
  56 + render(:partial => 'blocks/more', :locals => {:block => block, :contents => contents, :article_id => @page.id}), :id => "context_content_more_#{block.id}", :class => "more_button")
  57 + else
  58 + ''
  59 + end
  60 + end
  61 + end
55 62  
56 63 def content(args={})
57 64 block = self
58 65 lambda do
59 66 contents = block.contents(@page)
60 67 if !contents.blank?
61   - render :file => 'blocks/context_content', :locals => {:block => block, :contents => contents}
  68 + block_title(block.title) + content_tag('div',
  69 + render(:file => 'blocks/context_content', :locals => {:block => block, :contents => contents}), :class => 'contents', :id => "context_content_#{block.id}")
62 70 else
63 71 ''
64 72 end
... ...
plugins/context_content/test/unit/context_content_block_test.rb
... ... @@ -27,6 +27,8 @@ class ContextContentBlockTest &lt; ActiveSupport::TestCase
27 27 should 'render context content block view' do
28 28 @page = fast_create(Folder)
29 29 article = fast_create(TinyMceArticle, :parent_id => @page.id)
  30 + expects(:block_title).with(@block.title).returns('').once
  31 + expects(:content_tag).returns('').once
30 32 expects(:render).with(:file => 'blocks/context_content', :locals => {:block => @block, :contents => [article]})
31 33 instance_eval(&@block.content)
32 34 end
... ... @@ -43,7 +45,7 @@ class ContextContentBlockTest &lt; ActiveSupport::TestCase
43 45 article1 = fast_create(TinyMceArticle, :parent_id => folder.id)
44 46 article2 = fast_create(TinyMceArticle, :parent_id => folder.id)
45 47 article3 = fast_create(TinyMceArticle, :parent_id => folder.id)
46   - assert_equivalent [article1, article2], @block.contents(folder)
  48 + assert_equal 2, @block.contents(folder).length
47 49 end
48 50  
49 51 should 'return parent children if page has no children' do
... ... @@ -94,4 +96,24 @@ class ContextContentBlockTest &lt; ActiveSupport::TestCase
94 96 instance_eval(&@block.content_image(content))
95 97 end
96 98  
  99 + should 'do not display pagination links if page is nil' do
  100 + @page = nil
  101 + assert_equal '', instance_eval(&@block.footer)
  102 + end
  103 +
  104 + should 'do not display pagination links if it has until one page' do
  105 + assert_equal '', instance_eval(&@block.footer)
  106 + end
  107 +
  108 + should 'display pagination links if it has more than one page' do
  109 + @block.limit = 2
  110 + @page = fast_create(Folder)
  111 + article1 = fast_create(TinyMceArticle, :parent_id => @page.id)
  112 + article2 = fast_create(TinyMceArticle, :parent_id => @page.id)
  113 + article3 = fast_create(TinyMceArticle, :parent_id => @page.id)
  114 + expects(:content_tag).once
  115 + expects(:render).with(:partial => 'blocks/more', :locals => {:block => @block, :contents => [article1, article2], :article_id => @page.id}).once
  116 + instance_eval(&@block.footer)
  117 + end
  118 +
97 119 end
... ...
plugins/context_content/views/blocks/_more.rhtml 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +<% if contents.total_pages > 1 %>
  2 + <%= link_to_remote(nil, :url => {:id => block.id, :controller => 'context_content_plugin_profile', :action => 'view_content', :page => contents.previous_page, :article_id => article_id }, :html => {:class => "button icon-button icon-left #{contents.previous_page ? '':'disabled'}"}, :condition => "#{!contents.previous_page.nil?}", :success => "jQuery('#context_content_#{block.id}').effect('slide', {direction: 'left'});" )%>
  3 + <%= link_to_remote(nil, :url => {:id => block.id, :controller => 'context_content_plugin_profile', :action => 'view_content', :page => contents.next_page, :article_id => article_id }, :html => {:class => "button icon-button icon-right #{contents.next_page ? '':'disabled'}"}, :condition => "#{!contents.next_page.nil?}", :success => "jQuery('#context_content_#{block.id}').effect('slide', {direction: 'right'});" )%>
  4 +<% end %>
... ...
plugins/context_content/views/blocks/context_content.rhtml
1   -<%= block_title(block.title) %>
2   -
3   -<div class="contents">
4 1 <% contents.each do |content| %>
5 2 <span class="item">
6 3 <a href="<%= url_for(content.view_url) %>">
... ... @@ -13,4 +10,3 @@
13 10 </a>
14 11 </span>
15 12 <% end %>
16   -</div>
... ...