require_dependency 'article' 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 :comment_paragraph_plugin_parse_html 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 comment_paragraph_plugin_activated? comment_paragraph_plugin_activate && comment_paragraph_plugin_enabled? end protected def comment_paragraph_plugin_parse_html comment_paragraph_plugin_set_initial_value unless persisted? return unless comment_paragraph_plugin_activated? if body && (body_changed? || setting_changed?(:comment_paragraph_plugin_activate)) parsed_paragraphs = [] updated = body_changed? ? body_change[1] : body doc = Hpricot(updated) doc.search("/*").each do |paragraph| if paragraph.to_html =~ /^\W<\/p>$/ parsed_paragraphs << paragraph.to_html else if paragraph.to_html =~ /^(#{paragraph_content}\r\n" end end