renderable
renderable
is a wee gem I threw together fairly quickly to [a] scratch an itch, and [b] teach myself some new things.
It provides a nice simple hook for automagically converting the content of fields containing Textile into HTML whenever your ActiveRecord model is saved.
Installation
Is fairly simple.
1. add renderable
to your Gemfile
gem "renderable"
2. add appropriate fields to your database
Each renderable property of your model requires two database fields—one to store the original Textile content, and the other to store the rendered HTML. By default, this second field has the same name as the first, only with a ‘_rendered’ suffix.
I have created a couple of migration tasks, though, so you can either do this:
create_table :my_models do |t|
t.string :foo
t.renderable :bar
t.integer :baz, null: false
end
Or this:
add_renderable :my_models, :my_field
In both cases, a :string
type is created by default, but you can override this by specifying the required field type as an additional parameter (t.renderable :foo, :text
and add_renderable :my_models, :my_field, :text
). Additional options (limit, nullability, etc) can be specified as normal.
3. hook into your model
Simply add a call to acts_as_renderable
in your model:
class MyModel < ActiveRecord::Base
…
# automatically render ‘body’ and ‘description’ fields
acts_as_renderable fields: [ :body, :description ]
…
end
4. use
Your model will now have additional properties containing rendered HTML versions of your textile-ed content. Thus, for the above example:
-# output the rendered HTML
.content
~ @my_model.body_rendered.html_safe
Why?
Why not?
…
Seriously though, I love Textile and find myself using it all over the place. In a normal path for most Rails developers, I came across RedCloth and merrily set about writing code.
However after a while, I found myself copy-pasting the same :before_save
hook into each of my models—grab a textile field, shove it through RedCloth, and dump the result into a ‘_rendered’ field. It wasn’t very DRY, but it worked.
renderable
was the next logical step—I wanted to abstract everything into one place, and I wanted to see what all this ‘gem’ malarkey is about. The result is an insanely simple gem—I think there’s more hook code than business logic—but it works.
You’re doing it wrong!/What about %{gem}?
Oh, more than likely. And I’d be very surprised if there wasn’t already something out there that did this for me… but ultimately, I fancied a bit of a challenge and I learned a lot.
Version history
1.0.10 (2017-06-20)
- Made renderable include RedCloth by itself
1.0.9 (2017-05-28)
- Fixing broken dependency in gemspec
1.0.7 (2017-05-28)
- Releasing as a real gem
1.0.6 (2016-10-09)
- [FIX] made a tweak to the callback terminator that now means `before_*`` callbacks are correctly called (seems only to affect Rails 5… no idea why)
1.0.5 (2015-09-06)
- [FIX] fixing exception thrown when options are specified without a field type
1.0.4 (2014-09-11)
- adjusting rendering process to only run if a field has been updated
1.0.3 (2014-08-02)
- now compatible with Rails ~> 4.1.0
1.0.2 (2014-02-05 – I was busy…)
- adding
before_:field_render
andafter_:field_render
callbacks - making callbacks work in a more Rails-esque way (I’m still learnin’)
1.0.1 (2014-02-05)
- added
restrictions
option toacts_as_renderable
, allowing the ability to pass restrictions (eg:filter_html
,:lite_mode
) to RedCloth - added clean-up code to make output of
:lite_mode
a little more sane
1.0.0 (2013-08-17)
Initial release.
…
Share and enjoy!