Module: ActiveRecord::Acts::AsMarkup::ClassMethods

Defined in:
lib/acts/as_markup.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_markdown(*columns) ⇒ Object

This is a convenience method for ‘acts_as_markup :language => :markdown, :columns => [:body]`



123
124
125
# File 'lib/acts/as_markup.rb', line 123

def acts_as_markdown(*columns)
  acts_as_markup :language => :markdown, :columns => columns
end

#acts_as_markup(options) ⇒ Object

This allows you to specify columns you want to define as containing Markdown, Textile, Wikitext or RDoc content. Then you can simply call .to_html method on the attribute.

You can also specify the language as :variable you will then need to add an additional option of :language_column. When a value is accessed it will create the correct object (Markdown, Textile, Wikitext or RDoc) based on the value of the language column. If any value besides markdown, textile, wikitext, or RDoc is supplied for the markup language the text will pass through as a string.

Examples

Using Markdown language
class Post < ActiveRecrod
  acts_as_markup :language => :markdown, :columns => [:body]
end

@post = Post.find(:first)
@post.body.to_s            # => "## Markdown Headline"
@post.body.to_html         # => "<h2> Markdown Headline</h2>"
Using variable language
class Post < ActiveRecrod
  acts_as_markup :language => :variable, :columns => [:body], :language_column => 'markup_language'
end

@post = Post.find(:first)
@post.markup_language      # => "markdown"
@post.body.to_s            # => "## Markdown Headline"
@post.body.to_html         # => "<h2> Markdown Headline</h2>"


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/acts/as_markup.rb', line 50

def acts_as_markup(options)
  case options[:language].to_sym
  when :markdown
    klass = get_markdown_class
  when :textile
    require 'redcloth'
    klass = 'RedCloth'
  when :wikitext
    require 'wikitext'
    require_extensions 'wikitext'
    klass = 'WikitextString'
  when :rdoc
    require 'rdoc/markup/simple_markup'
    require 'rdoc/markup/simple_markup/to_html'
    require_extensions 'rdoc'
    klass = 'RDocText'
  when :variable
    markdown_klass = get_markdown_class
    require 'redcloth'
    require 'wikitext'
    require 'rdoc/markup/simple_markup'
    require 'rdoc/markup/simple_markup/to_html'
    require_extensions 'wikitext'
    require_extensions 'rdoc'
    textile_klass = 'RedCloth'
    wiki_klass = 'WikitextString'
    rdoc_klass = 'RDocText'
  else
    raise ActsAsMarkup::UnsportedMarkupLanguage, "#{options[:langauge]} is not a currently supported markup language."
  end
  
  options[:columns].each do |col|
    unless options[:language].to_sym == :variable
      class_eval <<-EOV
        def #{col.to_s}
          if @#{col.to_s}
            unless self.#{col.to_s}_changed?
              return @#{col.to_s}
            end
          end
          @#{col.to_s} = #{klass}.new(self['#{col.to_s}'].to_s)
        end
      EOV
    else
      class_eval <<-EOV
        def #{col.to_s}
          if @#{col.to_s}
            unless self.#{col.to_s}_changed? || self.#{options[:language_column].to_s}_changed?
              return @#{col.to_s}
            end
          end
          case self.#{options[:language_column].to_s}
          when /markdown/i
            @#{col.to_s} = #{markdown_klass}.new(self['#{col.to_s}'].to_s)
          when /textile/i
            @#{col.to_s} = #{textile_klass}.new(self['#{col.to_s}'].to_s)
          when /wikitext/i
            @#{col.to_s} = #{wiki_klass}.new(self['#{col.to_s}'].to_s)
          when /rdoc/i
            @#{col.to_s} = #{rdoc_klass}.new(self['#{col.to_s}'].to_s)
          else
            @#{col.to_s} = self['#{col.to_s}']
          end
        end
      EOV
    end
  end
end

#acts_as_rdoc(*columns) ⇒ Object

This is a convenience method for ‘acts_as_markup :language => :rdoc, :columns => [:body]`



147
148
149
# File 'lib/acts/as_markup.rb', line 147

def acts_as_rdoc(*columns)
  acts_as_markup :language => :rdoc, :columns => columns
end

#acts_as_textile(*columns) ⇒ Object

This is a convenience method for ‘acts_as_markup :language => :textile, :columns => [:body]`



131
132
133
# File 'lib/acts/as_markup.rb', line 131

def acts_as_textile(*columns)
  acts_as_markup :language => :textile, :columns => columns
end

#acts_as_wikitext(*columns) ⇒ Object

This is a convenience method for ‘acts_as_markup :language => :wikitext, :columns => [:body]`



139
140
141
# File 'lib/acts/as_markup.rb', line 139

def acts_as_wikitext(*columns)
  acts_as_markup :language => :wikitext, :columns => columns
end