diff --git a/controllers/comment_paragraph_plugin_admin_controller.rb b/controllers/comment_paragraph_plugin_admin_controller.rb index 11e9ada..fd4a85b 100644 --- a/controllers/comment_paragraph_plugin_admin_controller.rb +++ b/controllers/comment_paragraph_plugin_admin_controller.rb @@ -3,27 +3,10 @@ class CommentParagraphPluginAdminController < AdminController def index @settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin, params[:settings]) - @article_types = [] - available_article_types.each do |type| - @article_types.push({ - :class_name => type.name, - :short_description => type.short_description, - :description => type.description - }) - end - if request.post? - @settings.settings[:auto_marking_article_types].reject! { |type| type.blank? } @settings.save! - redirect_to :controller => 'plugins', :action => 'index' + session[:notice] = _('Settings successfuly saved') end end - protected - - def available_article_types - articles = [TinyMceArticle] + @plugins.dispatch(:content_types) - articles - end - end diff --git a/lib/comment_paragraph_plugin.rb b/lib/comment_paragraph_plugin.rb index 6e19e89..4b9c9fc 100644 --- a/lib/comment_paragraph_plugin.rb +++ b/lib/comment_paragraph_plugin.rb @@ -40,6 +40,10 @@ class CommentParagraphPlugin < Noosfero::Plugin true end + def self.activation_mode_default_setting + 'auto' + end + end require_dependency 'comment_paragraph_plugin/macros/allow_comment' diff --git a/lib/comment_paragraph_plugin/comment_paragraph_helper.rb b/lib/comment_paragraph_plugin/comment_paragraph_helper.rb deleted file mode 100644 index 0c3bf9f..0000000 --- a/lib/comment_paragraph_plugin/comment_paragraph_helper.rb +++ /dev/null @@ -1,8 +0,0 @@ -module CommentParagraphPlugin::CommentParagraphHelper - - def auto_marking_enabled?(plugin_settings, article_type) - auto_marking_setting = plugin_settings.get_setting('auto_marking_article_types') - auto_marking_setting && auto_marking_setting.include?(article_type) ? true : false - end - -end diff --git a/lib/ext/article.rb b/lib/ext/article.rb index dd86dd4..128e943 100644 --- a/lib/ext/article.rb +++ b/lib/ext/article.rb @@ -1,20 +1,27 @@ require_dependency 'article' -#FIXME should be specific to TextArticle? class Article has_many :paragraph_comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc', :conditions => [ 'paragraph_uuid IS NOT NULL'] - before_save :parse_html + before_save :comment_paragraph_plugin_parse_html - def parse_paragraph(paragraph_content, paragraph_uuid) - "
" + settings_items :comment_paragraph_plugin_activate, :type => :boolean, :default => false + + def comment_paragraph_plugin_enabled? + environment.plugin_enabled?(CommentParagraphPlugin) && self.kind_of?(TextArticle) end - def parse_html + protected + + def comment_paragraph_plugin_activate? + comment_paragraph_plugin_enabled? && comment_paragraph_plugin_settings.activation_mode == 'auto' + end + + def comment_paragraph_plugin_parse_html + comment_paragraph_plugin_activate = comment_paragraph_plugin_activate? + return unless comment_paragraph_plugin_activate + if body && body_changed? parsed_paragraphs = [] updated = body_change[1] @@ -24,7 +31,7 @@ class Article parsed_paragraphs << paragraph.to_html else if paragraph.to_html =~ /^(
" + end + end diff --git a/public/comment_paragraph_admin.js b/public/comment_paragraph_admin.js deleted file mode 100644 index cca8597..0000000 --- a/public/comment_paragraph_admin.js +++ /dev/null @@ -1,39 +0,0 @@ -function check_fields(check, table_id, start) { - var checkboxes = jQuery("#" + table_id + " tbody tr td input[type='checkbox']"); - for (var i = start; i < checkboxes.length; i++) { - checkboxes[i].checked = check; - } -} - -function verify_checked() { - var checkboxes = jQuery("#auto_marking_article_types_conf tbody tr td input[type='checkbox']"); - var allchecked = true - for (var j = 1; j < checkboxes.length; j++) { - if(!checkboxes[j].checked) { - allchecked = false - break - } - } - - var checkbox = checkboxes.first(); - checkboxes.first().attr('checked', allchecked); -} - -function check_all() { - jQuery("input[type='checkbox']").first().click(function () { - check_fields(this.checked, "auto_marking_article_types_conf", 0) - }); - verify_checked(); -} - -jQuery(document).ready(function() { - check_all(); - jQuery("input[type='checkbox']").click(function () { - var checkbox = jQuery(this).attr("id").split("_"); - verify_checked(); - - if(this.checked == false) { - jQuery("#" + checkbox.first() + "_" + checkbox.last()).attr("checked", false) - } - }); -}); diff --git a/test/functional/comment_paragraph_plugin_admin_controller_test.rb b/test/functional/comment_paragraph_plugin_admin_controller_test.rb index 4bc077c..1429d70 100644 --- a/test/functional/comment_paragraph_plugin_admin_controller_test.rb +++ b/test/functional/comment_paragraph_plugin_admin_controller_test.rb @@ -14,25 +14,25 @@ class CommentParagraphPluginAdminControllerTest < ActionController::TestCase @environment.save! @plugin_settings = Noosfero::Plugin::Settings.new(@environment, CommentParagraphPlugin) end + attr_reader :plugin_settings, :environment should 'access index action' do get :index - assert_template 'index' assert_response :success end should 'update comment paragraph plugin settings' do - assert_nil @plugin_settings.get_setting(:auto_marking_article_types) - post :index, :settings => { :auto_marking_article_types => ['TinyMceArticle'] } - @environment.reload - assert_not_nil @plugin_settings.get_setting(:auto_marking_article_types) + assert_not_equal 'manual', plugin_settings.get_setting(:activation_mode) + post :index, :settings => { :activation_mode => 'manual' } + environment.reload + assert_equal 'manual', plugin_settings.get_setting(:activation_mode) end should 'get article types previously selected' do - post :index, :settings => { :auto_marking_article_types => ['TinyMceArticle', 'TextileArticle'] } + plugin_settings.activation_mode = 'manual' + plugin_settings.save! get :index - assert_tag :input, :attributes => { :value => 'TinyMceArticle' } - assert_tag :input, :attributes => { :value => 'TextileArticle' } + assert_tag :input, :attributes => { :value => 'manual', :checked => 'checked' } end end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index c95d19c..7612373 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -6,9 +6,11 @@ class ArticleTest < ActiveSupport::TestCase def setup profile = fast_create(Community) @article = fast_create(Article, :profile_id => profile.id) + @environment = Environment.default + @environment.enable_plugin(CommentParagraphPlugin) end - attr_reader :article + attr_reader :article, :environment should 'return paragraph comments from article' do comment1 = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id) @@ -22,4 +24,19 @@ class ArticleTest < ActiveSupport::TestCase assert article.save end + should 'not parse html if the plugin is not enabled' do + article.body = "
paragraph 1
paragraph 2
" + environment.disable_plugin(CommentParagraphPlugin) + assert !environment.plugin_enabled?(CommentParagraphPlugin) + article.save! + assert_equal "paragraph 1
paragraph 2
", article.body + end + + should 'parse html if the plugin is not enabled' do + article.body = "paragraph 1
paragraph 2
" + article.expects(:comment_paragraph_plugin_enabled?).returns(true) + article.save! + assert_match /data-macro='comment_paragraph_plugin\/allow_comment'/, article.body + end + end diff --git a/views/comment_paragraph_plugin_admin/index.html.erb b/views/comment_paragraph_plugin_admin/index.html.erb index fa07820..a0caac0 100644 --- a/views/comment_paragraph_plugin_admin/index.html.erb +++ b/views/comment_paragraph_plugin_admin/index.html.erb @@ -1,32 +1,21 @@ -<% extend CommentParagraphPlugin::CommentParagraphHelper %> +
<%= _("Comment Paragraph Plugin Settings") %>
-<%= _("Comment paragraph plugin settings") %>
+ <%= form_for(:settings) do |f| %> -<%= form_for(:settings) do |f| %> +<%= _('Activation Mode') %>
+