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.



150
151
152
153
# File 'lib/acts/as_markup.rb', line 150

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.

For any column you can specify markup extensions by including :extensions => in any of the forms of acts_as_markup illustrated below.

Sample extensions methods are in: lib/markup_extensions. You can include methods that reside anywhere as long as they are in module MarkupExtensionMethods. (see the samples in lib/markup_methods).

You can invoke all the methods in module MarkupExtensionMethods with :extensions => :all.

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
With markup extension methods
class Post < ActiveRecord
  acts_as_markup :language => :markdown, :columns => [:body],
        :extensions => [:method1, method2, ...].
end

class Post < ActiveRecord
  acts_as_markdown :body, :extensions => :all
end


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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/acts/as_markup.rb', line 90

def acts_as_markup(options)
  case options[:language].to_sym
  when :markdown, :textile, :wikitext, :rdoc
    klass = require_library_and_get_class(options[:language].to_sym)
  when :variable
    markup_klasses = {}
    [:textile, :wikitext, :rdoc, :markdown].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
  
  # create the proc object in current scope
  set_markup_object = markup_object_proc
  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
        # call the proc to make all 'to_html' methods return an MString instead of a String
        set_markup_object.call("@#{col}",options,          
          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 
        # call the proc to make all 'to_html' methods return an MString instead of a String
        set_markup_object.call("@#{col}",options, 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
        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.



177
178
179
180
# File 'lib/acts/as_markup.rb', line 177

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.



159
160
161
162
# File 'lib/acts/as_markup.rb', line 159

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.



168
169
170
171
# File 'lib/acts/as_markup.rb', line 168

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