Created
October 7, 2012 23:43
-
-
Save BlakeMesdag/3849983 to your computer and use it in GitHub Desktop.
Module for Rendering Markdown easily
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assumes your schema for user has a bio attribute | |
class User < ActiveRecord::Base | |
include Renderable | |
attr_accessible :bio | |
renderable :bio | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Renderable | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
def self.renderer | |
@renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, | |
:autolink => true, | |
:space_after_headers => true, | |
:fenced_code_blocks => true) | |
end | |
module ClassMethods | |
def renderable(*args) | |
args.each do |arg| | |
define_method("rendered_#{arg}") do | |
Renderable.renderer.render(self.send(arg)).html_safe | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment