Commit 1f1e521053a2d37e094ce799798fca0067ecdc1a
1 parent
bcd0d440
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
comment_paragraph: option to apply to all text articles
Showing
8 changed files
with
74 additions
and
110 deletions
Show diff stats
plugins/comment_paragraph/controllers/comment_paragraph_plugin_admin_controller.rb
... | ... | @@ -3,27 +3,10 @@ class CommentParagraphPluginAdminController < AdminController |
3 | 3 | |
4 | 4 | def index |
5 | 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 | 6 | if request.post? |
16 | - @settings.settings[:auto_marking_article_types].reject! { |type| type.blank? } | |
17 | 7 | @settings.save! |
18 | - redirect_to :controller => 'plugins', :action => 'index' | |
8 | + session[:notice] = _('Settings successfuly saved') | |
19 | 9 | end |
20 | 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 | 12 | end | ... | ... |
plugins/comment_paragraph/lib/comment_paragraph_plugin.rb
plugins/comment_paragraph/lib/comment_paragraph_plugin/comment_paragraph_helper.rb
... | ... | @@ -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 | 1 | require_dependency 'article' |
2 | 2 | |
3 | -#FIXME should be specific to TextArticle? | |
4 | 3 | class Article |
5 | 4 | |
6 | 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> </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 | 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 | 25 | if body && body_changed? |
19 | 26 | parsed_paragraphs = [] |
20 | 27 | updated = body_change[1] |
... | ... | @@ -24,7 +31,7 @@ class Article |
24 | 31 | parsed_paragraphs << paragraph.to_html |
25 | 32 | else |
26 | 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 | 35 | else |
29 | 36 | parsed_paragraphs << paragraph.to_html |
30 | 37 | end |
... | ... | @@ -34,4 +41,15 @@ class Article |
34 | 41 | end |
35 | 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> </p>" | |
53 | + end | |
54 | + | |
37 | 55 | end | ... | ... |
plugins/comment_paragraph/public/comment_paragraph_admin.js
... | ... | @@ -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 < ActionController::TestCase |
14 | 14 | @environment.save! |
15 | 15 | @plugin_settings = Noosfero::Plugin::Settings.new(@environment, CommentParagraphPlugin) |
16 | 16 | end |
17 | + attr_reader :plugin_settings, :environment | |
17 | 18 | |
18 | 19 | should 'access index action' do |
19 | 20 | get :index |
20 | - assert_template 'index' | |
21 | 21 | assert_response :success |
22 | 22 | end |
23 | 23 | |
24 | 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 | 29 | end |
30 | 30 | |
31 | 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 | 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 | 36 | end |
37 | 37 | |
38 | 38 | end | ... | ... |
plugins/comment_paragraph/test/unit/article_test.rb
... | ... | @@ -6,9 +6,11 @@ class ArticleTest < ActiveSupport::TestCase |
6 | 6 | def setup |
7 | 7 | profile = fast_create(Community) |
8 | 8 | @article = fast_create(Article, :profile_id => profile.id) |
9 | + @environment = Environment.default | |
10 | + @environment.enable_plugin(CommentParagraphPlugin) | |
9 | 11 | end |
10 | 12 | |
11 | - attr_reader :article | |
13 | + attr_reader :article, :environment | |
12 | 14 | |
13 | 15 | should 'return paragraph comments from article' do |
14 | 16 | comment1 = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id) |
... | ... | @@ -22,4 +24,19 @@ class ArticleTest < ActiveSupport::TestCase |
22 | 24 | assert article.save |
23 | 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 | 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 | 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> | ... | ... |