How to add XML elements to your WordPress RSS feed

A while back, I shared a little way of customising the individual title of each item in your WordPress feed. That was based on filtering the existing title, and prepending the requisite content, which in that example was the post type (Gallery, Video, etc.).

I’m now using a variant of that same technique in The Stage’s new RSS feeds. News, Features, and Columns and their respective subcategories are all implemented using WordPress’s built-in categories system. The relevant category (News, Arts 2.0, Obituaries, etc.) precedes the relevant article’s headline. It’s not an ideal solution: if you grab a category feed, e.g., the RSS feed for Shenton’s View, every article will still contain the category name, even though it’s implicit from the context in which you’re requesting the feed.

Recently I’ve had an additional need, though: to add additional XML elements to an RSS feed in a way that gives additional flexibility to custom clients, but doesn’t break any feed readers.

Continue reading “How to add XML elements to your WordPress RSS feed”

Touch me: cleaning up Rails association code

Yeah, this is one of those posts where I stop wittering on about what I’ve seen at the theatre or in the cinema, and talk about computer code I’ve been writing. Odd mix for a blog, I know: welcome to my world…

In a Rails project I’m currently working on, I have a parent object that has a :has_many collection of children. In this case, they’re all time based, so let’s call the parent class Calendar and the child association Event:

Class Calendar < ActiveRecord::Base
  has_many :events
end

Class Event < ActiveRecord::Base
  belongs_to :calendar
end

(As you may have guessed, these aren’t the real class names. Confidentiality, and all that.)

Many operations on our Calendar object rely on the overall date range between the first and last dates in the list of events. Now we can, if we want to, calculate those on the fly using associations:

Class Calendar < ActiveRecord::Base
  has_many :events

  def first_date
    events.order('starts_on ASC').first.starts_on
  end
end

…and so on. Again, we have quite a few of these methods, and they’re actually a bit more complicated than this.

Continue reading “Touch me: cleaning up Rails association code”