Skip to content

Instantly share code, notes, and snippets.

@BlakeMesdag
Created October 7, 2012 23:43
Show Gist options
  • Save BlakeMesdag/3849983 to your computer and use it in GitHub Desktop.
Save BlakeMesdag/3849983 to your computer and use it in GitHub Desktop.
Module for Rendering Markdown easily
# Assumes your schema for user has a bio attribute
class User < ActiveRecord::Base
include Renderable
attr_accessible :bio
renderable :bio
end
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