Change of Strategy
Instead of updating my constantly breaking Typo sidebar plugin, and to implement Feedvertising from TextLinkAds, I’ve changed gears and chosen to implement just a regular plugin (very close to the new way of doing sidebar plugins in typo 4.1). This approach should work in all versions of typo, as well as any other Ruby on Rails’s application.
The support for feedvertising is slightly different than the one from the WordPress plugin that is the only option offered so far, but should be fairly close.
Installation
Get the plugin from subversion:
script/plugin install http://svn.nanorails.com/plugins/textlinkads/
or to use svn:externals and get future updates via svn update
script/plugin install -x http://svn.nanorails.com/plugins/textlinkads/
The installation will copy a file textlinkads.yml into your config directory.
The file looks like this:
key: TLA_KEY
affiliateid: 0
title: Sponsors
advertisehere: Advertise here!
testing: false
caching: true
Replace TLA_KEY with the one provided by TextLinkAds, set your affiliateid if you’d like to have a link to TextLinkAds with your affiliate id (so you can get credit if someone signs up for a TextLinkAds account). Change the title and advertisehere messages if you don’t like the defaults.
Set testing to false once you’ve verified it works (while testing=true, the plugin will use a special page provided by TextLinkAds that displays to links. However, that page does not contain any RSS links)
Finally, if the caching done by Rails is not enough, the plugin can cache the calls to retrieve the links. See the caching section for explanations on how to setup caching.
Integrating with Typo
Adding the display of regular links
To add the regular TextLinkAds links, you need to add a call to render_TLA anywhere in the rendering code. In typo, the most likely place is in your template’s default.rhtml
Here’s what my template looks like
<div id="sidebar">
...
<div class="sidebar-node"><%= render_TLA %></div>
<% response.lifetime = 6.hour %>
<%= render_sidebars %>
</div>
to replace the original:
<div id="sidebar">
<%= render_sidebars %>
</div>
Add the RSS links
For RSS2.0 for example, edit the file app/views/xml/_rss20_item_article.rxml to add a call to render_TLA_RSS(post_id)
here’s the original file:
xm.item do
xm.title post_title(item)
if this_blog.show_extended_on_rss
content = item.full_html
else
content = item.body_html
end
xm.description content
xm.pubDate pub_date(item.published_at)
...
xm.item do
xm.title post_title(item)
if this_blog.show_extended_on_rss
content = item.full_html
else
content = item.body_html
end
content += render_TLA_RSS(item.id)
response.lifetime = 6.hour
xm.description content
xm.pubDate pub_date(item.published_at)
Depending of which format you need, you may need to edit a different file.
That’s it.
Integrating with other apps
For other apps, just take a similar approach and add calls to render_TLA and render_TLA_RSS where most appropriate. Both calls are accessible from any Controller or Helper class.
Caching
Set “caching: true” in textlinkads.yml. Make sure Rails is configured with some caching. Typically, you need to have
config.action_controller.perform_caching = true
config.action_controller.fragment_cache_store = :file_store, "#{RAILS_ROOT}/tmp/cache"
either set from config/environment.rb or config/environment/[CURRENT ENVIRONMENT].rb
This will cache the pages where the ads appear, and cache the feed pages. But to make sure these pages will update properly if the TextLinkAds ads inventory is updated, you must ensure the cache will expire.
The best way to do that is to use the expiring_cache_action plugin. To install:
script/plugin install http://typosphere.org/trac/browser/trunk/vendor/plugins/expiring_action_cache
Then where you added calls to render_TLA, just add:
response.lifetime = 6.hour
This way, the cached page will expire 6 hours later so that would be the lapse of time to wait to see the updated ads.
No need to expire the cache for RSS, it should get expired automatically every time an article is added.
If you use this plugin in other applications, add a comment or send me a not (psq_0×40_nanorails_0×2e_com) and I’ll add a link to your instructions or code.