Permalink
Browse files

initial import

  • Loading branch information...
0 parents commit 97709c127a842f64f5ae07eb9b7952a9908c7cee Andy Delcambre committed Feb 13, 2008
Showing with 51 additions and 0 deletions.
  1. +9 −0 README
  2. +1 −0 init.rb
  3. +41 −0 lib/textilize_fu.rb
9 README
@@ -0,0 +1,9 @@
+TextilizeFu
+
+ class Article < ActiveRecord::Base
+ textilize :body
+ end
+
+This will convert the body from textile into html and store the result in body_html
+
+Based primarily off permalink_fu from Rick Olsen (technoweenie)
@@ -0,0 +1 @@
+ActiveRecord::Base.send :include, TextilizeFu
@@ -0,0 +1,41 @@
+require 'redcloth'
+module TextilizeFu
+ class << self
+ def translate(str)
+ RedCloth.new(str, [:hard_breaks]).to_html
+ end
+ end
+
+ def self.included(base)
+ base.extend ClassMethods
+ class << base
+ attr_accessor :textilize_attribute
+ attr_accessor :html_field
+ end
+ end
+
+ module ClassMethods
+ # Specifies the given field(s) as using textile, meaning it is passed through TextilizeFu.translate and set to the html_field.
+ #
+ # class Foo < ActiveRecord::Base
+ # # stores the html version of body in body_html
+ # textilize :body
+ #
+ # # stores html version of textile_body into converted_body
+ # textilize :textile_body, :converted_body
+ #
+ # end
+ #
+ def textilize(attr_name, html_field_name = nil)
+ self.textilize_attribute = attr_name
+ self.html_field = html_field_name || "#{attr_name.to_s}_html".to_sym
+ before_save :create_textilized_field
+ end
+ end
+
+protected
+ def create_textilized_field
+ send("#{self.class.html_field}=", TextilizeFu.translate(send(self.class.textilize_attribute)))
+ end
+
+end

0 comments on commit 97709c1

Please sign in to comment.