From 482044f63e3700d314c5911771f90354a1348991 Mon Sep 17 00:00:00 2001 From: Evandro Junior Date: Tue, 23 Dec 2014 19:33:30 -0300 Subject: [PATCH] before rename paragraph_id paragraph_uuid --- db/migrate/20140715201649_add_paragraph_id_to_comment.rb | 9 --------- db/migrate/20141223184902_add_paragraph_uuid_to_comments.rb | 6 ++++++ lib/comment_paragraph_plugin.rb | 81 ++++++++++++++++++++++++++++++++++++++++----------------------------------------- lib/ext/article.rb | 32 ++++++++++++++++++++++++++++---- public/comment_paragraph.js | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------- 5 files changed, 131 insertions(+), 111 deletions(-) delete mode 100644 db/migrate/20140715201649_add_paragraph_id_to_comment.rb create mode 100644 db/migrate/20141223184902_add_paragraph_uuid_to_comments.rb diff --git a/db/migrate/20140715201649_add_paragraph_id_to_comment.rb b/db/migrate/20140715201649_add_paragraph_id_to_comment.rb deleted file mode 100644 index 639f4ca..0000000 --- a/db/migrate/20140715201649_add_paragraph_id_to_comment.rb +++ /dev/null @@ -1,9 +0,0 @@ -class AddParagraphIdToComment < ActiveRecord::Migration - def self.up - add_column :comments, :paragraph_id, :integer unless column_exists?(:comments, :paragraph_id) - end - - def self.down - remove_column :comments, :paragraph_id - end -end diff --git a/db/migrate/20141223184902_add_paragraph_uuid_to_comments.rb b/db/migrate/20141223184902_add_paragraph_uuid_to_comments.rb new file mode 100644 index 0000000..1bc166d --- /dev/null +++ b/db/migrate/20141223184902_add_paragraph_uuid_to_comments.rb @@ -0,0 +1,6 @@ +class AddParagraphUuidToComments < ActiveRecord::Migration + def change + add_column :comments, :paragraph_uuid, :string unless column_exists?(:comments, :paragraph_uuid) + add_index :comments, :paragraph_uuid + end +end diff --git a/lib/comment_paragraph_plugin.rb b/lib/comment_paragraph_plugin.rb index 701e944..b9d7ca7 100644 --- a/lib/comment_paragraph_plugin.rb +++ b/lib/comment_paragraph_plugin.rb @@ -40,48 +40,47 @@ class CommentParagraphPlugin < Noosfero::Plugin true end - def cms_controller_filters - block = proc do - if params['commit'] == 'Save' +# def cms_controller_filters +# block = proc do +# if params['commit'] == 'Save' +# +# settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin, params[:settings]) +# +# extend CommentParagraphPlugin::CommentParagraphHelper +# if !@article.id.blank? && self.auto_marking_enabled?(settings, @article.class.name) +# +# parsed_paragraphs = [] +# paragraph_id = 0 +# +# doc = Hpricot(@article.body) +# paragraphs = doc.search("/*").each do |paragraph| +# +# if paragraph.to_html =~ /^\W<\/p>$/ +# parsed_paragraphs << paragraph.to_html +# else +# if paragraph.to_html =~ /^( 'after_filter', +# :method_name => 'new', +# :block => block } +# end - settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin, params[:settings]) - - extend CommentParagraphPlugin::CommentParagraphHelper - if !@article.id.blank? && self.auto_marking_enabled?(settings, @article.class.name) - - parsed_paragraphs = [] - paragraph_id = 0 - - doc = Hpricot(@article.body) - paragraphs = doc.search("/*").each do |paragraph| - - if paragraph.to_html =~ /^\W<\/p>$/ - parsed_paragraphs << paragraph.to_html - else - if paragraph.to_html =~ /^( 'after_filter', - :method_name => 'new', - :block => block } - end - - private def self.parse_paragraph( paragraph_content, paragraph_id ) "
'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc', :conditions => [ 'paragraph_id IS NOT NULL'] + has_many :paragraph_comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc', :conditions => [ 'paragraph_uuid IS NOT NULL'] - validate :body_change_with_comments + # validate :body_change_with_comments + + before_save :parse_html def body_change_with_comments if body && body_changed? && !self.comments.empty? - paragraphs_with_comments = self.comments.where("paragraph_id IS NOT NULL") + paragraphs_with_comments = self.comments.where("paragraph_uuid IS NOT NULL") errors[:base] << (N_('You are unable to change the body of the article when paragraphs are commented')) unless (paragraphs_with_comments).empty? end end + + def parse_html + if body && body_changed? + parsed_paragraphs = [] + updated = body_change[1] + doc = Hpricot(updated) + paragraphs = doc.search("/*").each do |paragraph| + uuid = SecureRandom.uuid + if paragraph.to_html =~ /^\W<\/p>$/ + parsed_paragraphs << paragraph.to_html + else + if paragraph.to_html =~ /^(

') && ! element.startsWith('

 

') ){ - notSelectedTextCount++; - } - } - }); - - if(selectedTextCount > 0 && notSelectedTextCount>0){ - jQuery('#article_body_ifr').contents().find('body').children('.paragraph_comment').contents().unwrap(); - //Workaround necessary to post the body of the article - tinymce.activeEditor.execCommand('mceInsertContent', false, " "); - return; - } - - //Add p tag, when opening the editor. For some season it appear at the end without p tag - foundCommentableParagraph = false; - jQuery('#article_body_ifr').contents().find('body').children('div.article_comments').each(function( index ) { - if(jQuery(this).html()!="" && jQuery(this).html()!=" " && jQuery(this).html()!="
"){ - paragraphsTxt+="

" + jQuery(this).html() + "

"; - } - foundCommentableParagraph = true; - }); - - //undo the paragraph comment tags - if(foundCommentableParagraph === true){ - tinyMCE.activeEditor.setContent(paragraphsTxt); - return; - } - - //Wraps the paragraph using the chosen class - jQuery('#article_body_ifr').contents().find('body').children('p,table,img').each(function( index ) { - text=jQuery(this).prop('outerHTML'); - if(text!="" && text!=" " && text!="
"){ - paragraphsTxt+='
' + text + '

' - } - }); - tinyMCE.activeEditor.setContent(paragraphsTxt); - - //Workaround necessary to post the body of the article - tinymce.activeEditor.execCommand('mceInsertContent', false, " "); -} +//String.prototype.startsWith = function(needle){ +// return(this.indexOf(needle) == 0); +//}; +// +//function makeAllCommentable() { +// var paragraphsTxt=""; +// var selectedTextCount=0; +// var notSelectedTextCount=0; +// var text; +// +// //Search for text that is not selected in the middle of selected text, in this case unselect everything +// jQuery('#article_body_ifr').contents().find('body').children().each(function( index ) { +// //Check if there are other texts not selected +// var element=jQuery(this).prop('outerHTML'); +// if(element.startsWith('

') && ! element.startsWith('

 

') ){ +// notSelectedTextCount++; +// } +// } +// }); +// +// if(selectedTextCount > 0 && notSelectedTextCount>0){ +// jQuery('#article_body_ifr').contents().find('body').children('.paragraph_comment').contents().unwrap(); +// //Workaround necessary to post the body of the article +// tinymce.activeEditor.execCommand('mceInsertContent', false, " "); +// return; +// } +// +// //Add p tag, when opening the editor. For some season it appear at the end without p tag +// foundCommentableParagraph = false; +// jQuery('#article_body_ifr').contents().find('body').children('div.article_comments').each(function( index ) { +// if(jQuery(this).html()!="" && jQuery(this).html()!=" " && jQuery(this).html()!="
"){ +// paragraphsTxt+="

" + jQuery(this).html() + "

"; +// } +// foundCommentableParagraph = true; +// }); +// +// //undo the paragraph comment tags +// if(foundCommentableParagraph === true){ +// tinyMCE.activeEditor.setContent(paragraphsTxt); +// return; +// } +// +// //Wraps the paragraph using the chosen class +// jQuery('#article_body_ifr').contents().find('body').children('p,table,img').each(function( index ) { +// text=jQuery(this).prop('outerHTML'); +// if(text!="" && text!=" " && text!="
"){ +// paragraphsTxt+='
' + text + '

' +// } +// }); +// tinyMCE.activeEditor.setContent(paragraphsTxt); +// +// //Workaround necessary to post the body of the article +// tinymce.activeEditor.execCommand('mceInsertContent', false, " "); +//} -- libgit2 0.21.2