Manage your markup with has_markup
I recently decided to extract some plugins, in an effort to clean up the codebase for my blog.
The first thing I tackled was generation of HTML from my Post
model.
In the beginning…
Here’s the starting point for my model:
validates_markdown
really doesn’t belong here, and nothing interesting is happening, but the lines of code start to add up.
Let’s look at the partial for Post
.
Eh, nothing particuarly interesting.
Goals
- One-liner for specifying markup, similar to
has_many
oracts_as_taggable
- Optionally require the markup
- Optionally cache the markup
- Allow for different syntaxes
A whole new look on things
I was able to extract the logic out of my model and hit all of these goals.
The refactored model:
And the view is more or less the same:
Thoughts
- Plugins are really easy to make
- Plugins can pull a good amount of code out of your model, which results in it being more readable
- I think I finally understand the difference between include and extend
Get it
If you’re interested in trying this plugin, it is hosted on GitHub. You can install it by with:
script/plugin install git://github.com/technicalpickles/has_markup.git