Simple Rails Snippets 13 Jan 2011
There has been a bit of development around content micro-management in the Rails ecosystem lately.
It’s really good to see some simple solutions to what should really be a simple problem.
For me, even these seem a bit much, if you just want to add really basic snippet management it really isn’t that hard to from scratch. I can’t promise you this is the best way to do it, but it works well for me, comments are greatly appreciated.
Now let’s set up the basics: A simple helper
In the snippet helper I’m defining a snippet_for method:
This takes the name of the snippet, and an optional default value. Make sure you use descriptive names here, you’ll thank yourself later.
The helper will try to find a value first by searching the rails cache, followed by the snippet database, followed by the optional default and lastly will show a snippet not found message.
In the last line I’m aliasing snippet_for(…) to s(..) just for brevity
The snippet model contains a simple published scope. And an after_save callback to allow for snippet updating. If you starting enhancing your snippet model you’ll probably want to implement a Rails sweeper.
Now in your code when you need a snippetTweet