From ecf5200db83ee308fd64823c2964fbb202c7854e Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Tue, 12 Nov 2013 16:34:05 -0300 Subject: [PATCH] adding pagination in comment group plugin --- app/helpers/application_helper.rb | 2 +- plugins/comment_group/controllers/profile/comment_group_plugin_profile_controller.rb | 22 ++++++++++++++-------- plugins/comment_group/lib/comment_group_plugin.rb | 5 +++++ plugins/comment_group/public/style.css | 7 +++++++ plugins/comment_group/test/functional/comment_group_plugin_profile_controller_test.rb | 34 ++++++++++++++++++++++++++++++++++ plugins/comment_group/views/_comment_group.rhtml | 10 ++++++++-- plugins/comment_group/views/comment_group_plugin_profile/view_comments.rjs | 12 ++++++++++++ test/unit/application_helper_test.rb | 13 +++++++++++++ 8 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 plugins/comment_group/public/style.css create mode 100644 plugins/comment_group/views/comment_group_plugin_profile/view_comments.rjs diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 07e41ad..f4a0465 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1402,7 +1402,7 @@ module ApplicationHelper def filter_html(html, source) if @plugins && source.has_macro? - html = convert_macro(html, source) + html = convert_macro(html, source) unless @plugins.enabled_macros.blank? #TODO This parse should be done through the macro infra, but since there # are old things that do not support it we are keeping this hot spot. html = @plugins.pipeline(:parse_content, html, source).first diff --git a/plugins/comment_group/controllers/profile/comment_group_plugin_profile_controller.rb b/plugins/comment_group/controllers/profile/comment_group_plugin_profile_controller.rb index 9f765a2..685107c 100644 --- a/plugins/comment_group/controllers/profile/comment_group_plugin_profile_controller.rb +++ b/plugins/comment_group/controllers/profile/comment_group_plugin_profile_controller.rb @@ -2,15 +2,21 @@ class CommentGroupPluginProfileController < ProfileController append_view_path File.join(File.dirname(__FILE__) + '/../views') def view_comments - article_id = params[:article_id] - group_id = params[:group_id] + @article_id = params[:article_id] + @group_id = params[:group_id] - article = profile.articles.find(article_id) - comments = article.group_comments.without_spam.in_group(group_id) - render :update do |page| - page.replace_html "comments_list_group_#{group_id}", :partial => 'comment/comment.rhtml', :collection => comments - page.replace_html "comment-count-#{group_id}", comments.count - end + article = profile.articles.find(@article_id) + @group_comment_page = (params[:group_comment_page] || 1).to_i + + @comments = article.comments.without_spam.in_group(@group_id) + @comments_count = @comments.count + @comments = @comments.without_reply.paginate(:per_page => per_page, :page => @group_comment_page ) + + @no_more_pages = @comments_count <= @group_comment_page * per_page + end + + def per_page + 3 end end diff --git a/plugins/comment_group/lib/comment_group_plugin.rb b/plugins/comment_group/lib/comment_group_plugin.rb index a2f21b8..6cd23d2 100644 --- a/plugins/comment_group/lib/comment_group_plugin.rb +++ b/plugins/comment_group/lib/comment_group_plugin.rb @@ -24,6 +24,11 @@ class CommentGroupPlugin < Noosfero::Plugin 'comment_group_macro.js' end + def stylesheet? + true + end + + end require_dependency 'comment_group_plugin/macros/allow_comment' diff --git a/plugins/comment_group/public/style.css b/plugins/comment_group/public/style.css new file mode 100644 index 0000000..d8f9e6f --- /dev/null +++ b/plugins/comment_group/public/style.css @@ -0,0 +1,7 @@ +div.article-comments-list-more{ + width: 100%; + height: 30px; + text-align: center; + font-size: 20px; + margin-bottom: 5px; +} diff --git a/plugins/comment_group/test/functional/comment_group_plugin_profile_controller_test.rb b/plugins/comment_group/test/functional/comment_group_plugin_profile_controller_test.rb index 69d31ab..9c5f555 100644 --- a/plugins/comment_group/test/functional/comment_group_plugin_profile_controller_test.rb +++ b/plugins/comment_group/test/functional/comment_group_plugin_profile_controller_test.rb @@ -35,4 +35,38 @@ class CommentGroupPluginProfileControllerTest < ActionController::TestCase assert_match /\"comment-count-0\", \"1\"/, @response.body end + should 'show first page comments only' do + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 1', :group_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 2', :group_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 3', :group_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'secondpage', :body => 'secondpage', :group_id => 0) + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :group_id => 0 + assert_match /firstpage 1/, @response.body + assert_match /firstpage 2/, @response.body + assert_match /firstpage 3/, @response.body + assert_no_match /secondpage/, @response.body + end + + should 'show link to display more comments' do + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :group_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :group_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :group_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'secondpage', :body => 'secondpage', :group_id => 0) + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :group_id => 0 + assert_match /group_comment_page=2/, @response.body + end + + should 'do not show link to display more comments if do not have more pages' do + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :group_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :group_id => 0) + comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :group_id => 0) + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :group_id => 0 + assert_no_match /group_comment_page/, @response.body + end + + should 'do not show link to display more comments if do not have any comments' do + xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :group_id => 0 + assert_no_match /group_comment_page/, @response.body + end + end diff --git a/plugins/comment_group/views/_comment_group.rhtml b/plugins/comment_group/views/_comment_group.rhtml index 91f816f..c7c7644 100644 --- a/plugins/comment_group/views/_comment_group.rhtml +++ b/plugins/comment_group/views/_comment_group.rhtml @@ -20,7 +20,13 @@
diff --git a/plugins/comment_group/views/comment_group_plugin_profile/view_comments.rjs b/plugins/comment_group/views/comment_group_plugin_profile/view_comments.rjs new file mode 100644 index 0000000..90640ec --- /dev/null +++ b/plugins/comment_group/views/comment_group_plugin_profile/view_comments.rjs @@ -0,0 +1,12 @@ +if @group_comment_page == 1 + page.replace_html "comments_list_group_#{@group_id}", :partial => 'comment/comment.rhtml', :collection => @comments +else + page.insert_html :bottom, "comments_list_group_#{@group_id}", :partial => 'comment/comment.rhtml', :collection => @comments +end +page.replace_html "comment-count-#{@group_id}", @comments_count + +if @no_more_pages + page.replace_html "comments_list_group_#{@group_id}_more", "" +else + page.replace_html "comments_list_group_#{@group_id}_more", link_to_remote(_('More'), :url => { :profile => profile.identifier, :controller => 'comment_group_plugin_profile', :action => 'view_comments', :group_id => @group_id, :article_id => @article_id, :group_comment_page => @group_comment_page + 1}, :loaded => visual_effect(:highlight, "comments_list_group_#{@group_id}"), :method => :post, :complete => "loadCompleted(#{@group_id})") +end diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index 379244a..004fe16 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -814,6 +814,19 @@ class ApplicationHelperTest < ActiveSupport::TestCase assert_no_match /Test1/, parsed_html end + should 'not convert macro if there is no macro plugin active' do + profile = create_user('testuser').person + article = fast_create(Article, :profile_id => profile.id) + class Plugin1 < Noosfero::Plugin; end + + environment = Environment.default + environment.enable_plugin(Plugin1) + @plugins = Noosfero::Plugin::Manager.new(environment, self) + + expects(:convert_macro).never + filter_html(article.body, nil) + end + protected include NoosferoTestHelper -- libgit2 0.21.2