Commit bd1e21fc81d69b779c3db142732866bcf710fca3

Authored by Francisco Júnior
2 parents 435c6e9a 3f112b4b

Merge branch 'AI3205-comment-paragraph' into stable

plugins/comment_paragraph/controllers/comment_paragraph_plugin_admin_controller.rb 0 → 100644
@@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
  1 +class CommentParagraphPluginAdminController < AdminController
  2 + append_view_path File.join(File.dirname(__FILE__) + '/../views')
  3 +
  4 + def index
  5 + @settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin, params[:settings])
  6 + @article_types = []
  7 + available_article_types.each do |type|
  8 + @article_types.push({
  9 + :class_name => type.name,
  10 + :short_description => type.short_description,
  11 + :description => type.description
  12 + })
  13 + end
  14 +
  15 + if request.post?
  16 + @settings.settings[:auto_marking_article_types].reject! { |type| type.blank? }
  17 + @settings.save!
  18 + redirect_to :controller => 'plugins', :action => 'index'
  19 + end
  20 + end
  21 +
  22 + protected
  23 +
  24 + def available_article_types
  25 + articles = [TinyMceArticle, TextileArticle] + @plugins.dispatch(:content_types)
  26 + articles
  27 + end
  28 +
  29 +end
plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb
@@ -4,13 +4,10 @@ class CommentParagraphPluginProfileController &lt; ProfileController @@ -4,13 +4,10 @@ class CommentParagraphPluginProfileController &lt; ProfileController
4 def view_comments 4 def view_comments
5 @article_id = params[:article_id] 5 @article_id = params[:article_id]
6 @paragraph_id = params[:paragraph_id] 6 @paragraph_id = params[:paragraph_id]
7 -  
8 article = profile.articles.find(@article_id) 7 article = profile.articles.find(@article_id)
9 - @paragraph_comment_page = (params[:paragraph_comment_page] || 1).to_i  
10 -  
11 @comments = article.comments.without_spam.in_paragraph(@paragraph_id) 8 @comments = article.comments.without_spam.in_paragraph(@paragraph_id)
12 @comments_count = @comments.count 9 @comments_count = @comments.count
13 @comments = @comments.without_reply 10 @comments = @comments.without_reply
14 end 11 end
15 12
16 -end  
17 \ No newline at end of file 13 \ No newline at end of file
  14 +end
plugins/comment_paragraph/lib/comment_paragraph_plugin.rb
@@ -42,15 +42,30 @@ class CommentParagraphPlugin &lt; Noosfero::Plugin @@ -42,15 +42,30 @@ class CommentParagraphPlugin &lt; Noosfero::Plugin
42 def cms_controller_filters 42 def cms_controller_filters
43 block = proc do 43 block = proc do
44 if params['commit'] == 'Save' 44 if params['commit'] == 'Save'
45 - unless @article.id.blank? 45 +
  46 + settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin, params[:settings])
  47 +
  48 + extend CommentParagraphPlugin::CommentParagraphHelper
  49 + if !@article.id.blank? && self.auto_marking_enabled?(settings, @article.class.name)
46 50
47 parsed_paragraphs = [] 51 parsed_paragraphs = []
48 paragraph_id = 0 52 paragraph_id = 0
49 53
50 doc = Hpricot(@article.body) 54 doc = Hpricot(@article.body)
51 - paragraphs = doc.search("/[\r\n]").each do |paragraph|  
52 - parsed_paragraphs << (paragraph.to_html =~ /(.*)paragraph_comment_spacer(.*)|<div(.*)paragraph_comment(.*)/ ? paragraph.to_html : CommentParagraphPlugin.parse_paragraph(paragraph.to_html, paragraph_id)) 55 + paragraphs = doc.search("/*").each do |paragraph|
  56 +
  57 + if paragraph.to_html =~ /^<div(.*)paragraph_comment(.*)$/ || paragraph.to_html =~ /^<p>\W<\/p>$/
  58 + parsed_paragraphs << paragraph.to_html
  59 + else
  60 + if paragraph.to_html =~ /^(<div|<table|<p|<ul).*/
  61 + parsed_paragraphs << CommentParagraphPlugin.parse_paragraph(paragraph.to_html, paragraph_id)
  62 + else
  63 + parsed_paragraphs << paragraph.to_html
  64 + end
  65 + end
  66 +
53 paragraph_id += 1 67 paragraph_id += 1
  68 +
54 end 69 end
55 70
56 @article.body = parsed_paragraphs.join() 71 @article.body = parsed_paragraphs.join()
@@ -71,7 +86,7 @@ class CommentParagraphPlugin &lt; Noosfero::Plugin @@ -71,7 +86,7 @@ class CommentParagraphPlugin &lt; Noosfero::Plugin
71 "<div class='macro article_comments paragraph_comment' " + 86 "<div class='macro article_comments paragraph_comment' " +
72 "data-macro='comment_paragraph_plugin/allow_comment' " + 87 "data-macro='comment_paragraph_plugin/allow_comment' " +
73 "data-macro-paragraph_id='#{paragraph_id}'>#{paragraph_content}</div>\r\n" + 88 "data-macro-paragraph_id='#{paragraph_id}'>#{paragraph_content}</div>\r\n" +
74 - "<p class='paragraph_comment_spacer'></p>\r\n" 89 + "<p>&nbsp;</p>"
75 end 90 end
76 91
77 end 92 end
plugins/comment_paragraph/lib/comment_paragraph_plugin/comment_paragraph_helper.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +module CommentParagraphPlugin::CommentParagraphHelper
  2 +
  3 + def auto_marking_enabled?(plugin_settings, article_type)
  4 + auto_marking_setting = plugin_settings.get_setting('auto_marking_article_types')
  5 + auto_marking_setting && auto_marking_setting.include?(article_type) ? true : false
  6 + end
  7 +
  8 +end
plugins/comment_paragraph/public/comment_paragraph_admin.js 0 → 100644
@@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
  1 +function check_fields(check, table_id, start) {
  2 + var checkboxes = jQuery("#" + table_id + " tbody tr td input[type='checkbox']");
  3 + for (var i = start; i < checkboxes.length; i++) {
  4 + checkboxes[i].checked = check;
  5 + }
  6 +}
  7 +
  8 +function verify_checked() {
  9 + var checkboxes = jQuery("#auto_marking_article_types_conf tbody tr td input[type='checkbox']");
  10 + var allchecked = true
  11 + for (var j = 1; j < checkboxes.length; j++) {
  12 + if(!checkboxes[j].checked) {
  13 + allchecked = false
  14 + break
  15 + }
  16 + }
  17 +
  18 + var checkbox = checkboxes.first();
  19 + checkboxes.first().attr('checked', allchecked);
  20 +}
  21 +
  22 +function check_all() {
  23 + jQuery("input[type='checkbox']").first().click(function () {
  24 + check_fields(this.checked, "auto_marking_article_types_conf", 0)
  25 + });
  26 + verify_checked();
  27 +}
  28 +
  29 +jQuery(document).ready(function() {
  30 + check_all();
  31 + jQuery("input[type='checkbox']").click(function () {
  32 + var checkbox = jQuery(this).attr("id").split("_");
  33 + verify_checked();
  34 +
  35 + if(this.checked == false) {
  36 + jQuery("#" + checkbox.first() + "_" + checkbox.last()).attr("checked", false)
  37 + }
  38 + });
  39 +});
plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb
@@ -35,38 +35,16 @@ class CommentParagraphPluginProfileControllerTest &lt; ActionController::TestCase @@ -35,38 +35,16 @@ class CommentParagraphPluginProfileControllerTest &lt; ActionController::TestCase
35 assert_match /\"comment-count-0\", \"1\"/, @response.body 35 assert_match /\"comment-count-0\", \"1\"/, @response.body
36 end 36 end
37 37
38 - should 'show first page comments only' do  
39 - comment1 = fast_create(Comment, :created_at => Time.now - 1.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'secondpage', :paragraph_id => 0)  
40 - comment2 = fast_create(Comment, :created_at => Time.now - 2.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 1', :paragraph_id => 0)  
41 - comment3 = fast_create(Comment, :created_at => Time.now - 3.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 2', :paragraph_id => 0)  
42 - comment4 = fast_create(Comment, :created_at => Time.now - 4.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'firstpage 3', :paragraph_id => 0) 38 + should 'be able to show all comments of a paragraph' do
  39 + comment1 = fast_create(Comment, :created_at => Time.now - 1.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'a comment', :paragraph_id => 0)
  40 + comment2 = fast_create(Comment, :created_at => Time.now - 2.days, :source_id => article, :author_id => profile, :title => 'b comment', :body => 'b comment', :paragraph_id => 0)
  41 + comment3 = fast_create(Comment, :created_at => Time.now - 3.days, :source_id => article, :author_id => profile, :title => 'c comment', :body => 'c comment', :paragraph_id => 0)
  42 + comment4 = fast_create(Comment, :created_at => Time.now - 4.days, :source_id => article, :author_id => profile, :title => 'd comment', :body => 'd comment', :paragraph_id => 0)
43 xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0 43 xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0
44 - assert_match /firstpage 1/, @response.body  
45 - assert_match /firstpage 2/, @response.body  
46 - assert_match /firstpage 3/, @response.body  
47 - assert_no_match /secondpage/, @response.body  
48 - end  
49 -  
50 - should 'show link to display more comments' do  
51 - comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)  
52 - comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)  
53 - comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)  
54 - comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'secondpage', :body => 'secondpage', :paragraph_id => 0)  
55 - xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0  
56 - assert_match /paragraph_comment_page=2/, @response.body  
57 - end  
58 -  
59 - should 'do not show link to display more comments if do not have more pages' do  
60 - comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)  
61 - comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)  
62 - comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala', :paragraph_id => 0)  
63 - xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0  
64 - assert_no_match /paragraph_comment_page/, @response.body  
65 - end  
66 -  
67 - should 'do not show link to display more comments if do not have any comments' do  
68 - xhr :get, :view_comments, :profile => @profile.identifier, :article_id => article.id, :paragraph_id => 0  
69 - assert_no_match /paragraph_comment_page/, @response.body 44 + assert_match /a comment/, @response.body
  45 + assert_match /b comment/, @response.body
  46 + assert_match /c comment/, @response.body
  47 + assert_match /d comment/, @response.body
70 end 48 end
71 49
72 end 50 end
plugins/comment_paragraph/test/functional/comment_paragraph_plugin_test.rb 0 → 100644
@@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require File.dirname(__FILE__) + '/../../controllers/comment_paragraph_plugin_admin_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class CommentParagraphPluginAdminController; def rescue_action(e) raise e end; end
  6 +
  7 +class CommentParagraphPluginAdminControllerTest < ActionController::TestCase
  8 +
  9 + def setup
  10 + @environment = Environment.default
  11 + user_login = create_admin_user(@environment)
  12 + login_as(user_login)
  13 + @environment.enabled_plugins = ['CommentParagraphPlugin']
  14 + @environment.save!
  15 + @plugin_settings = Noosfero::Plugin::Settings.new(@environment, CommentParagraphPlugin)
  16 + end
  17 +
  18 + should 'access index action' do
  19 + get :index
  20 + assert_template 'index'
  21 + assert_response :success
  22 + end
  23 +
  24 + should 'update comment paragraph plugin settings' do
  25 + assert_nil @plugin_settings.get_setting(:auto_marking_article_types)
  26 + post :index, :settings => { :auto_marking_article_types => ['TinyMceArticle'] }
  27 + @environment.reload
  28 + assert_not_nil @plugin_settings.get_setting(:auto_marking_article_types)
  29 + end
  30 +
  31 + should 'get article types previously selected' do
  32 + post :index, :settings => { :auto_marking_article_types => ['TinyMceArticle', 'TextileArticle'] }
  33 + get :index
  34 + assert_tag :input, :attributes => { :value => 'TinyMceArticle' }
  35 + assert_tag :input, :attributes => { :value => 'TextileArticle' }
  36 + end
  37 +
  38 +end
plugins/comment_paragraph/views/comment_paragraph_plugin_admin/index.html.erb 0 → 100644
@@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
  1 +<% extend CommentParagraphPlugin::CommentParagraphHelper %>
  2 +
  3 +<h1><%= _("Comment paragraph plugin settings") %></h1>
  4 +
  5 +<%= form_for(:settings) do |f| %>
  6 +
  7 +<%= hidden_field_tag 'settings[auto_marking_article_types][]' %>
  8 +
  9 +<table id="auto_marking_article_types_conf" border="0">
  10 + <tr>
  11 + <th align="left"><%= _('Article type') %></th>
  12 + <th><%= _('Automatic marking active') %></th>
  13 + </tr>
  14 + <tr style="background-color: #EEE; border-bottom: 2px solid #000;">
  15 + <td><span style="font-style: italic;"><%= _('Check/Uncheck All') %></span></td>
  16 + <td align="center"><%= check_box_tag 'article_active', '' %></td>
  17 + </tr>
  18 + <% @article_types.each do |type| %>
  19 + <tr>
  20 + <td><%= _(type[:short_description]) %></td>
  21 + <td align="center"><%= check_box_tag 'settings[auto_marking_article_types][]', type[:class_name], auto_marking_enabled?(@settings, type[:class_name]) %></td>
  22 + </tr>
  23 + <% end %>
  24 +</table>
  25 +
  26 + <% button_bar do %>
  27 + <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
  28 + <% end %>
  29 +
  30 +<% end %>
  31 +
  32 +<%= javascript_include_tag 'plugins/comment_paragraph/comment_paragraph_admin' %>
plugins/comment_paragraph/views/comment_paragraph_plugin_profile/view_comments.rjs
1 -if @paragraph_comment_page == 1  
2 - page.replace_html "comments_list_paragraph_#{@paragraph_id}", :partial => 'comment/comment.html.erb', :collection => @comments  
3 -else  
4 - page.insert_html :bottom, "comments_list_paragraph_#{@paragraph_id}", :partial => 'comment/comment.html.erb', :collection => @comments  
5 -end 1 +page.replace_html "comments_list_paragraph_#{@paragraph_id}", :partial => 'comment/comment.html.erb', :collection => @comments
6 page.replace_html "comment-count-#{@paragraph_id}", @comments_count 2 page.replace_html "comment-count-#{@paragraph_id}", @comments_count
7 -  
8 -if @no_more_pages  
9 - page.replace_html "comments_list_paragraph_#{@paragraph_id}_more", ""  
10 -else  
11 - page.replace_html "comments_list_paragraph_#{@paragraph_id}_more", link_to_remote(_('More'), :url => { :profile => profile.identifier, :controller => 'comment_paragraph_plugin_profile', :action => 'view_comments', :paragraph_id => @paragraph_id, :article_id => @article_id, :paragraph_comment_page => @paragraph_comment_page + 1}, :loaded => visual_effect(:highlight, "comments_list_paragraph_#{@paragraph_id}"), :method => :post, :complete => "loadCompleted(#{@paragraph_id})")  
12 -end