Paperclip Resized Image Details In to_xml

I love paperclip. In fact, I love paperclip enough to name my next child paperclip. The issue at hand is calling to_xml on an object that is using paperclip. What you end up with is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<studio>
  <created-at type="datetime">2009-06-18T17:02:49Z</created-at>
  <description>A Studio Description Here</description>
  <id type="integer">1</id>
  <photo-content-type>image/jpeg</photo-content-type>
  <photo-file-name>jaws.jpg</photo-file-name>
  <photo-file-size type="integer">833900</photo-file-size>
  <photo-updated-at type="datetime">2009-06-18T17:02:48Z</photo-updated-at>
  <state>active</state>
  <title>Some Title Here</title>
  <titles-count type="integer">0</titles-count>
  <updated-at type="datetime">2009-06-18T17:02:49Z</updated-at>
  <url>http://www.google.com</url>
</studio>

That doesn’t give us any real useful information about the attached image, such as the file path to the file or the styles. So, I figured, lets overload to_xml, but I did not want to have a huge nested builder setup in my model, after all, we are talking about Ruby, there has to be a fairly clean way to do it. So, I browsed around and found you can pass proc objects in the options you pass to to_xml. Here is how my to_xml turned out.

1
2
3
4
5
6
7
8
9
10
11
12
def to_xml(options = {}, &block)
  thumbs = Proc.new do |opts|
    [:large, :thumb, :icon].each do |style|
      opts[:builder].tag!("thumbnail-#{style}", self.photo.url(style, false))
    end
  end
  default_options = {
    :procs => [thumbs],
    :except => [:photo_file_name, :photo_file_size, :photo_content_type, :photo_updated_at]
  }
  super(default_options.merge(options), &block)
end

Basically, I just create a Proc to loop over the styles I use for paperclip, and build a tag for each one based on the style. Then, I ignore the original paperclip columns, as I don’t really need those. Finally, pass the options off to super() and call it a day. The result will look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
<studio>
  <created-at type="datetime">2009-06-18T17:02:49Z</created-at>
  <description>A Studio Description Here</description>
  <id type="integer">1</id>
  <state>active</state>
  <title>Some Title Here</title>
  <titles-count type="integer">0</titles-count>
  <updated-at type="datetime">2009-06-18T17:02:49Z</updated-at>
  <url>http://www.google.com</url>
  <thumbnail-large>/content/studios/photos/1/large_jaws.jpg</thumbnail-large>
  <thumbnail-thumb>/content/studios/photos/1/thumb_jaws.jpg</thumbnail-thumb>
  <thumbnail-icon>/content/studios/photos/1/icon_jaws.jpg</thumbnail-icon>
</studio>

Hope this helps others. Let me know if you have a cleaner method of doing this.

Related posts:

  1. RubyOnRails Seed_fu and Paperclip woes
  2. Why my next child will be named Paperclip
  3. Inherited_resources fondles swfupload and you didn’t even know it
  4. Ezmobius Is The Devil And Is Gaining Followers For The Apocalypse Using Nanite
  5. Paperclip before_process For Your Habitual Pornographic Needs
blog comments powered by Disqus