Commit 1f1e521053a2d37e094ce799798fca0067ecdc1a

Authored by Victor Costa
1 parent bcd0d440

comment_paragraph: option to apply to all text articles

plugins/comment_paragraph/controllers/comment_paragraph_plugin_admin_controller.rb
@@ -3,27 +3,10 @@ class CommentParagraphPluginAdminController < AdminController @@ -3,27 +3,10 @@ class CommentParagraphPluginAdminController < AdminController
3 3
4 def index 4 def index
5 @settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin, params[:settings]) 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? 6 if request.post?
16 - @settings.settings[:auto_marking_article_types].reject! { |type| type.blank? }  
17 @settings.save! 7 @settings.save!
18 - redirect_to :controller => 'plugins', :action => 'index' 8 + session[:notice] = _('Settings successfuly saved')
19 end 9 end
20 end 10 end
21 11
22 - protected  
23 -  
24 - def available_article_types  
25 - articles = [TinyMceArticle] + @plugins.dispatch(:content_types)  
26 - articles  
27 - end  
28 -  
29 end 12 end
plugins/comment_paragraph/lib/comment_paragraph_plugin.rb
@@ -40,6 +40,10 @@ class CommentParagraphPlugin < Noosfero::Plugin @@ -40,6 +40,10 @@ class CommentParagraphPlugin < Noosfero::Plugin
40 true 40 true
41 end 41 end
42 42
  43 + def self.activation_mode_default_setting
  44 + 'auto'
  45 + end
  46 +
43 end 47 end
44 48
45 require_dependency 'comment_paragraph_plugin/macros/allow_comment' 49 require_dependency 'comment_paragraph_plugin/macros/allow_comment'
plugins/comment_paragraph/lib/comment_paragraph_plugin/comment_paragraph_helper.rb
@@ -1,8 +0,0 @@ @@ -1,8 +0,0 @@
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/lib/ext/article.rb
1 require_dependency 'article' 1 require_dependency 'article'
2 2
3 -#FIXME should be specific to TextArticle?  
4 class Article 3 class Article
5 4
6 has_many :paragraph_comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc', :conditions => [ 'paragraph_uuid IS NOT NULL'] 5 has_many :paragraph_comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc', :conditions => [ 'paragraph_uuid IS NOT NULL']
7 6
8 - before_save :parse_html 7 + before_save :comment_paragraph_plugin_parse_html
9 8
10 - def parse_paragraph(paragraph_content, paragraph_uuid)  
11 - "<div class='macro article_comments paragraph_comment' " +  
12 - "data-macro='comment_paragraph_plugin/allow_comment' " +  
13 - "data-macro-paragraph_uuid='#{paragraph_uuid}'>#{paragraph_content}</div>\r\n" +  
14 - "<p>&nbsp;</p>" 9 + settings_items :comment_paragraph_plugin_activate, :type => :boolean, :default => false
  10 +
  11 + def comment_paragraph_plugin_enabled?
  12 + environment.plugin_enabled?(CommentParagraphPlugin) && self.kind_of?(TextArticle)
15 end 13 end
16 14
17 - def parse_html 15 + protected
  16 +
  17 + def comment_paragraph_plugin_activate?
  18 + comment_paragraph_plugin_enabled? && comment_paragraph_plugin_settings.activation_mode == 'auto'
  19 + end
  20 +
  21 + def comment_paragraph_plugin_parse_html
  22 + comment_paragraph_plugin_activate = comment_paragraph_plugin_activate?
  23 + return unless comment_paragraph_plugin_activate
  24 +
18 if body && body_changed? 25 if body && body_changed?
19 parsed_paragraphs = [] 26 parsed_paragraphs = []
20 updated = body_change[1] 27 updated = body_change[1]
@@ -24,7 +31,7 @@ class Article @@ -24,7 +31,7 @@ class Article
24 parsed_paragraphs << paragraph.to_html 31 parsed_paragraphs << paragraph.to_html
25 else 32 else
26 if paragraph.to_html =~ /^(<div|<table|<p|<ul).*/ 33 if paragraph.to_html =~ /^(<div|<table|<p|<ul).*/
27 - parsed_paragraphs << parse_paragraph(paragraph.to_html, SecureRandom.uuid) 34 + parsed_paragraphs << comment_paragraph_plugin_parse_paragraph(paragraph.to_html, SecureRandom.uuid)
28 else 35 else
29 parsed_paragraphs << paragraph.to_html 36 parsed_paragraphs << paragraph.to_html
30 end 37 end
@@ -34,4 +41,15 @@ class Article @@ -34,4 +41,15 @@ class Article
34 end 41 end
35 end 42 end
36 43
  44 + def comment_paragraph_plugin_settings
  45 + @comment_paragraph_plugin_settings ||= Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin)
  46 + end
  47 +
  48 + def comment_paragraph_plugin_parse_paragraph(paragraph_content, paragraph_uuid)
  49 + "<div class='macro article_comments paragraph_comment' " +
  50 + "data-macro='comment_paragraph_plugin/allow_comment' " +
  51 + "data-macro-paragraph_uuid='#{paragraph_uuid}'>#{paragraph_content}</div>\r\n" +
  52 + "<p>&nbsp;</p>"
  53 + end
  54 +
37 end 55 end
plugins/comment_paragraph/public/comment_paragraph_admin.js
@@ -1,39 +0,0 @@ @@ -1,39 +0,0 @@
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_admin_controller_test.rb
@@ -14,25 +14,25 @@ class CommentParagraphPluginAdminControllerTest &lt; ActionController::TestCase @@ -14,25 +14,25 @@ class CommentParagraphPluginAdminControllerTest &lt; ActionController::TestCase
14 @environment.save! 14 @environment.save!
15 @plugin_settings = Noosfero::Plugin::Settings.new(@environment, CommentParagraphPlugin) 15 @plugin_settings = Noosfero::Plugin::Settings.new(@environment, CommentParagraphPlugin)
16 end 16 end
  17 + attr_reader :plugin_settings, :environment
17 18
18 should 'access index action' do 19 should 'access index action' do
19 get :index 20 get :index
20 - assert_template 'index'  
21 assert_response :success 21 assert_response :success
22 end 22 end
23 23
24 should 'update comment paragraph plugin settings' do 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) 25 + assert_not_equal 'manual', plugin_settings.get_setting(:activation_mode)
  26 + post :index, :settings => { :activation_mode => 'manual' }
  27 + environment.reload
  28 + assert_equal 'manual', plugin_settings.get_setting(:activation_mode)
29 end 29 end
30 30
31 should 'get article types previously selected' do 31 should 'get article types previously selected' do
32 - post :index, :settings => { :auto_marking_article_types => ['TinyMceArticle', 'TextileArticle'] } 32 + plugin_settings.activation_mode = 'manual'
  33 + plugin_settings.save!
33 get :index 34 get :index
34 - assert_tag :input, :attributes => { :value => 'TinyMceArticle' }  
35 - assert_tag :input, :attributes => { :value => 'TextileArticle' } 35 + assert_tag :input, :attributes => { :value => 'manual', :checked => 'checked' }
36 end 36 end
37 37
38 end 38 end
plugins/comment_paragraph/test/unit/article_test.rb
@@ -6,9 +6,11 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -6,9 +6,11 @@ class ArticleTest &lt; ActiveSupport::TestCase
6 def setup 6 def setup
7 profile = fast_create(Community) 7 profile = fast_create(Community)
8 @article = fast_create(Article, :profile_id => profile.id) 8 @article = fast_create(Article, :profile_id => profile.id)
  9 + @environment = Environment.default
  10 + @environment.enable_plugin(CommentParagraphPlugin)
9 end 11 end
10 12
11 - attr_reader :article 13 + attr_reader :article, :environment
12 14
13 should 'return paragraph comments from article' do 15 should 'return paragraph comments from article' do
14 comment1 = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id) 16 comment1 = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id)
@@ -22,4 +24,19 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -22,4 +24,19 @@ class ArticleTest &lt; ActiveSupport::TestCase
22 assert article.save 24 assert article.save
23 end 25 end
24 26
  27 + should 'not parse html if the plugin is not enabled' do
  28 + article.body = "<p>paragraph 1</p><p>paragraph 2</p>"
  29 + environment.disable_plugin(CommentParagraphPlugin)
  30 + assert !environment.plugin_enabled?(CommentParagraphPlugin)
  31 + article.save!
  32 + assert_equal "<p>paragraph 1</p><p>paragraph 2</p>", article.body
  33 + end
  34 +
  35 + should 'parse html if the plugin is not enabled' do
  36 + article.body = "<p>paragraph 1</p><p>paragraph 2</p>"
  37 + article.expects(:comment_paragraph_plugin_enabled?).returns(true)
  38 + article.save!
  39 + assert_match /data-macro='comment_paragraph_plugin\/allow_comment'/, article.body
  40 + end
  41 +
25 end 42 end
plugins/comment_paragraph/views/comment_paragraph_plugin_admin/index.html.erb
1 -<% extend CommentParagraphPlugin::CommentParagraphHelper %> 1 +<div class="comment-paragraph-plugin-settings">
  2 + <h1><%= _("Comment Paragraph Plugin Settings") %></h1>
2 3
3 -<h1><%= _("Comment paragraph plugin settings") %></h1> 4 + <%= form_for(:settings) do |f| %>
4 5
5 -<%= form_for(:settings) do |f| %> 6 + <div class="activation-mode">
  7 + <h4><%= _('Activation Mode') %></h4>
  8 + <div class="auto">
  9 + <%= f.radio_button(:activation_mode, 'auto') %> <%= _('Auto') %>
  10 + </div>
  11 + <div>
  12 + <%= f.radio_button(:activation_mode, 'manual') %> <%= _('Manual') %>
  13 + </div>
  14 + </div>
6 15
7 -<%= hidden_field_tag 'settings[auto_marking_article_types][]' %> 16 + <% button_bar do %>
  17 + <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
  18 + <% end %>
8 19
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 %> 20 <% 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' %> 21 +</div>