beast rev 2066
[beast-modified.git] / vendor / plugins / white_list_formatted_content / init.rb
blob73b0e086d1196cdd3286b3e2a3c60059f4ccd065
1 ActiveRecord::Base.class_eval do
2   include ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, WhiteListHelper
3   def self.format_attribute(attr_name)
4     class << self; include ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, WhiteListHelper; end
5     define_method(:body)       { read_attribute attr_name }
6     define_method(:body_html)  { read_attribute "#{attr_name}_html" }
7     define_method(:body_html=) { |value| write_attribute "#{attr_name}_html", value }
8     before_save :format_content
9   end
11   def dom_id
12     [self.class.name.downcase.pluralize.dasherize, id] * '-'
13   end
15   protected
16     def format_content
17       body.strip! if body.respond_to?(:strip!)
18       self.body_html = body.blank? ? '' : body_html_with_formatting
19     end
20     
21     def body_html_with_formatting
22       body_html = auto_link(body) { |text| truncate(text, 50) }
23       textilized = RedCloth.new(body_html, [ :hard_breaks ])
24       textilized.hard_breaks = true if textilized.respond_to?("hard_breaks=")
25       white_list(textilized.to_html)
26     end
27 end