<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rails on the Run &#187; upload</title>
	<atom:link href="http://railsontherun.com/tag/upload/feed/" rel="self" type="application/rss+xml" />
	<link>http://railsontherun.com</link>
	<description>Rails experiments by Matt Aimonetti</description>
	<lastBuildDate>Tue, 23 Feb 2010 07:28:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>attachment_fu, Flash and to_xml tips</title>
		<link>http://railsontherun.com/2007/07/21/attachment_fu-flash-and-to_xml-tips/</link>
		<comments>http://railsontherun.com/2007/07/21/attachment_fu-flash-and-to_xml-tips/#comments</comments>
		<pubDate>Sat, 21 Jul 2007 03:30:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[attachment]]></category>
		<category><![CDATA[attachment_fu]]></category>
		<category><![CDATA[edge]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[to_xml]]></category>
		<category><![CDATA[upload]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/07/21/attachment_fu-flash-and-to_xml-tips</guid>
		<description><![CDATA[I recently had to deal with an interesting challenge. I had to write a simple interface between a rails app and a Flash application. Nothing hard and if you browse the archives, you&#8217;ll find examples and tutorials on how to create a REST interface to communicate between Rails and Flash.
The thing was that this time [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to deal with an interesting challenge. I had to write a simple interface between a rails app and a Flash application. Nothing hard and if you browse the archives, you&#8217;ll find examples and tutorials on how to create a REST interface to communicate between Rails and Flash.</p>
<p>The thing was that this time I had to interface with a model using <a href="http://svn.techno-weenie.net/projects/plugins/attachment_fu/">attachment<em>fu</a>. I&#8217;m a great fan of a</em>fu and it&#8217;s definitely the best way of dealing with uploads. </p>
<p>My model looked more or less like that:</p>
<pre><code>class Photo &lt; ActiveRecord::Base

belongs_to :user

  has_attachment(
    :content_type =&gt; :image,
    :storage =&gt; :file_system,
    :max_size =&gt; 10.megabytes,
    :resize_to =&gt; '640x480&gt;',
    :thumbnails =&gt; { :thumb =&gt; '100x100&gt;',
                              :preview =&gt; '300x200&gt;',
                     }
  )
  validates_as_attachment
  # read more about validates_existence_of http://blog.hasmanythrough.com/2007/7/14/validate-your-existence
  validates_existence_of :user
end
</code></pre>
<p>My show action in my photo controller could have looked a bit like that:</p>
<pre><code>respond_to do |format|
  format.html # show.html.erb
  format.xml  { render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @photo }
end
</code></pre>
<p>That&#8217;s great, the problem is that we are displaying a lot of information that our Flash client doesn&#8217;t need to see, actually we are exposing a lot of information nobody should ever see and we are not displaying what we should. On top of being a waste of bandwidth and giving too much work to the client, we are not actually providing the user with the details of the thumbnail.</p>
<p>The first thing to do would be not to display some of the object attributes, the to_xml method lets you do that. </p>
<p>Note that in Edge, Rails will automatically try to convert your object using to<em>xml, you don&#8217;t even need to mention it. However in our case, we want to use some _advanced</em> features offered by to_xml, and here is how our code should look like:  </p>
<pre><code>format.xml do
  render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @photo.to_xml( :except =&gt; [ :id, :filename, :updated_at, :user_id, :height, :width], :skip_types =&gt; true )
end
</code></pre>
<p>What I just did is very simple, we rendered our object as an xml object but we didn&#8217;t convert few attributes, :id, :filename, :updated<em>at, :user</em>id, :height, :width. By default Rails also adds the object type, we don&#8217;t really need that right now, so let&#8217;s skip them.<br/><br />
(The reason why I don&#8217;t want to convert the filename is that I want to provide our Flash client with the photo thumbnail instead of the original picture.)</p>
<p>As far as I know, to_xml doesn&#8217;t let you create new attributes. (if I have some time, I&#8217;ll submit a patch to get that added).</p>
<p>What we are trying to do is to display the avatar of a user. We found the photo record using @user.photo but that&#8217;s the original photo and we want to provide Flash with the avatar info, not the original.</p>
<p>What we need to do is to simply add a new attribute called avatar:</p>
<pre><code>format.xml do
  @photo[:avatar] = @photo.public_filename(:thumb)
  render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @photo.to_xml( :except =&gt; [ :id, :filename, :updated_at, :user_id, :height, :width], :skip_types =&gt; true )
end
</code></pre>
<p>Simple enough, but it took me a little while to figure it out <img src='http://railsontherun.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Voila, we now have a clean, trimmed and safe XML returned object that you can be consumed by our Flash client. Ohh, and we added a new attribute that the original object didn&#8217;t have <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/07/21/attachment_fu-flash-and-to_xml-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>new rails plugin: mimetype_fu</title>
		<link>http://railsontherun.com/2007/06/14/new-rails-plugin-mimetype_fu/</link>
		<comments>http://railsontherun.com/2007/06/14/new-rails-plugin-mimetype_fu/#comments</comments>
		<pubDate>Thu, 14 Jun 2007 04:45:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[attachment_fu]]></category>
		<category><![CDATA[content type]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[mime]]></category>
		<category><![CDATA[mime type\']]></category>
		<category><![CDATA[mimetype_fu]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/06/14/new-rails-plugin-mimetype_fu</guid>
		<description><![CDATA[mimetype_fu/ is a new plugin I just wrote. It&#8217;s simple and it can be really useful if you need to get the mime type of a file already on your server.
During one of my project, I add to migrate old assets from a legacy system to a new Rails app. The new app uses attachment [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/mimetype-fu/">mimetype_fu/</a> is a new plugin I just wrote. It&#8217;s simple and it can be really useful if you need to get the mime type of a file already on your server.</p>
<p>During one of my project, I add to migrate old assets from a legacy system to a new Rails app. The new app uses <a href="http://svn.techno-weenie.net/projects/plugins/attachment_fu/">attachment fu</a> and even though techno weenie did an amazing job, attachment<em>fu validation is based on the content type. A</em>fu gets the content type coming from the CGI query. </p>
<p>Unit test has a helper faking this process but in real life, if you use a Flash uploader (Flash doesn&#8217;t give you the proper mime type/content type) or if you want to migrate files, the attachment_fu validation won&#8217;t work for you.</p>
<p>The solution is simple: <a href="http://code.google.com/p/mimetype-fu/">mimetype_fu/</a></p>
<p><a href="http://code.google.com/p/mimetype-fu/">mimetype_fu/</a> extends the File class and is really easy to use:</p>
<pre><code>File.mime_type?(@file)
</code></pre>
<p>Check it out <a href="http://code.google.com/p/mimetype-fu/">http://code.google.com/p/mimetype-fu/</a></p>
<p>Expect a post showing how a ninja would use the mimetype<em>fu / attachment</em>fu combo <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/06/14/new-rails-plugin-mimetype_fu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

