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]` Additional options can be given at the end, if necessary.



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

def acts_as_markdown(*columns)
  options = columns.extract_options!
  acts_as_markup options.merge(: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. The language used to process the column will be based on another column. By default a column named “markup_language” is used, but this can be changed by providing a :language_column option. 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.

You can specify additional options to pass to the markup library by using :markdown_options, :textile_options or :wikitext_options. RDoc does not support any useful options. The options should be given as an array of arguments. You can specify options for more than one language when using :variable. See each library’s documentation for more details on what options are available.

Examples

Using Markdown language
class Post < ActiveRecord
  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 < ActiveRecord
  acts_as_markup :language => :variable, :columns => [:body], :language_column => 'language_name'
end

@post = Post.find(:first)
@post.language_name        # => "markdown"
@post.body.to_s            # => "## Markdown Headline"
@post.body.to_html         # => "<h2> Markdown Headline</h2>"
Using options
class Post < ActiveRecord
  acts_as_markup :language => :markdown, :columns => [:body], :markdown_options => [ :filter_html ]
end

class Post < ActiveRecord
  acts_as_markup :language => :textile, :columns => [:body], :textile_options => [ [ :filter_html ] ]
end

class Post < ActiveRecord
  acts_as_markup :language => :wikitext, :columns => [:body], :wikitext_options => [ { :space_to_underscore => true } ]
end


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
118
119
120
121
122
123
# File 'lib/acts/as_markup.rb', line 72

def acts_as_markup(options)
  case options[:language].to_sym
  when :markdown, :textile, :wikitext, :rdoc, :simple_format
    klass = require_library_and_get_class(options[:language].to_sym)
  when :variable
    markup_klasses = {}
    [:textile, :wikitext, :rdoc, :markdown, :simple_format].each do |language|
      markup_klasses[language] = require_library_and_get_class(language)
    end
    options[:language_column] ||= :markup_language
  else
    raise ActsAsMarkup::UnsupportedMarkupLanguage, "#{options[:langauge]} is not a currently supported markup language."
  end

  unless options[:language].to_sym == :variable
    markup_options = options["#{options[:language]}_options".to_sym] || []
    options[:columns].each do |col|
      define_method col do
        if instance_variable_defined?("@#{col}")
          unless send("#{col}_changed?")
            return instance_variable_get("@#{col}")
          end
        end
        instance_variable_set("@#{col}", klass.new(self[col].to_s, *markup_options))
      end
    end
  else
    options[:columns].each do |col|
      define_method col do
        if instance_variable_defined?("@#{col}")
          unless send("#{col}_changed?") || send("#{options[:language_column]}_changed?")
            return instance_variable_get("@#{col}")
          end
        end
        instance_variable_set("@#{col}", case send(options[:language_column])
        when /markdown/i
          markup_klasses[:markdown].new self[col].to_s, *(options[:markdown_options] || [])
        when /textile/i
          markup_klasses[:textile].new self[col].to_s, *(options[:textile_options] || [])
        when /wikitext/i
          markup_klasses[:wikitext].new self[col].to_s, *(options[:wikitext_options] || [])
        when /rdoc/i
          markup_klasses[:rdoc].new self[col].to_s
        when /simple/i
          markup_klasses[:simple_format].new self[col].to_s
        else
          self[col]
        end)
      end
    end
  end
end

#acts_as_rdoc(*columns) ⇒ Object

This is a convenience method for ‘acts_as_markup :language => :rdoc, :columns => [:body]` Additional options can be given at the end, if necessary.



156
157
158
159
# File 'lib/acts/as_markup.rb', line 156

def acts_as_rdoc(*columns)
  options = columns.extract_options!
  acts_as_markup options.merge(:language => :rdoc, :columns => columns)
end

#acts_as_textile(*columns) ⇒ Object

This is a convenience method for ‘acts_as_markup :language => :textile, :columns => [:body]` Additional options can be given at the end, if necessary.



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

def acts_as_textile(*columns)
  options = columns.extract_options!
  acts_as_markup options.merge(:language => :textile, :columns => columns)
end

#acts_as_wikitext(*columns) ⇒ Object

This is a convenience method for ‘acts_as_markup :language => :wikitext, :columns => [:body]` Additional options can be given at the end, if necessary.



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

def acts_as_wikitext(*columns)
  options = columns.extract_options!
  acts_as_markup options.merge(:language => :wikitext, :columns => columns)
end