<?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; flash</title>
	<atom:link href="http://railsontherun.com/tag/flash/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>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<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 [...]]]></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 [...]]]></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>
		<item>
		<title>Rails/Flash, part 2</title>
		<link>http://railsontherun.com/2007/05/07/flash-rails-part2/</link>
		<comments>http://railsontherun.com/2007/05/07/flash-rails-part2/#comments</comments>
		<pubDate>Mon, 07 May 2007 22:15:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[respond_to]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/05/07/flash-rails-part2</guid>
		<description><![CDATA[In part I I explained how to access Rails data from Flash. However Yves aka Kadoudal was wondering what I did with Rails to return the event record: Rails.get(&#8216;events&#8217;, rails_events); how rails returns the event record as we don&#8217;t call a controller/action &#8230; ? I believe doing it RESTFul it&#8217;s depending upon your route ? [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.railsontherun.com/2007/4/9/rails-and-flash">part I</a> I explained how to access Rails data from Flash.</p>
<p>However Yves aka Kadoudal was wondering what I did with Rails to return the event record:</p>
<p>Rails.get(&#8216;events&#8217;, rails_events); how rails returns the event record as we don&#8217;t call a controller/action &#8230; ? I believe doing it RESTFul it&#8217;s depending upon your route ? is it not?</p>
<p>We are actually calling a controller/action If you look at the Restfulflash class, you&#8217;ll notice a function called get</p>
<pre><code>public function get(controller, callback){
        var railsReply:XML = new XML();
        railsReply.ignoreWhite = true;
        railsReply.onLoad = function(success:Boolean){
            if (success) {
                    trace ('Rails responded: '+railsReply);
                    callback.text = railsReply;
            } else {
                    trace ('Error while waiting for Rails to reply');
               callback.text = 'error';
            }
        }
        var railsRequest:XML = new XML();
        railsRequest.contentType='application/xml';
        railsRequest.sendAndLoad(this.gateway+controller, railsReply);
        delete railsRequest;
    }
</code></pre>
<p>What&#8217;s really interesting are the following 2 lines:</p>
<p>railsRequest.contentType=&#8221;application/xml&#8221;;<br />
railsRequest.sendAndLoad(this.gateway+controller, railsReply);</p>
<p>I&#8217;m preparing a request that I will send to my gateway mentioning the controller name. In the previous example I was calling the &#8216;events&#8217; controller: http://mysite.fr/events because I&#8217;m using REST and because my request is a &#8216;get&#8217; Rails will call the index action.</p>
<p>for more info on <a href="http://www.softiesonrails.com/2007/4/10/rest-101-part-3-just-call-me-the-repo-man">RESTful design</a> check the nice series available from the <a href="http://www.softiesonrails.com">softies on rails blog</a></p>
<p>Let&#8217;s just look at my code and see what&#8217;s up with rails magic <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here is my index action sending you back a different object based on the header of your request.</p>
<pre><code>class EventsController &lt; ApplicationController
  # GET /events
  # GET /events.xml
  def index
    @events = Event.find(:all)

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @events.to_xml }
      format.json { render :json =&gt; @events.to_json }
    end
  end
</code></pre>
<p>That&#8217;s it, and it was automatically created for you by the script/generate scaffold_resource command <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Since I&#8217;m answering Yves&#8217; questions, let&#8217;s look at his final question:</p>
<p>problem : what if the xml returned is not a record, but it has to be prepared by Rails&#8230;<br />
I explain, right now my rails view .. I have a javascript object passing all the info via JS<br />
I would like to replace the datasource file by a direct call to rails from Flash, so this param will disappear&#8230; so (correct me if I am wrong)<br />
    1- I need to pass a user_id value in JS to Flash in the script&#8230;<br />
    2- Flash need to send a request to Rails (controller/action/id to prepare the xml&#8230; ) to get in return a correct xlm object to be displayed</p>
<p>1- I&#8217;m not sure why you need to pass the user_id from JS to Flash but I guess Flash doesn&#8217;t know what user to query?? (anyway that would work)</p>
<p>2- If you are using REST, Flash just needs make a get call to /controller/id Make sure to set the request header type as xml, like I did in my function railsRequest.contentType=&#8221;application/xml&#8221;; Otherwise I believe (but didn&#8217;t try) that you can call /events/1.xml where 1 id the id of the event you want to retrieve.</p>
<p>By the way, it will call the show action from the events controller which looks like that:</p>
<pre><code>def show
  @event = Event.find(params[:id])

  respond_to do |format|
    format.html # show.rhtml
    format.xml  { render <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @event.to_xml }
  end
end
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/05/07/flash-rails-part2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Restful Rails and Flash</title>
		<link>http://railsontherun.com/2007/04/09/rails-and-flash/</link>
		<comments>http://railsontherun.com/2007/04/09/rails-and-flash/#comments</comments>
		<pubDate>Mon, 09 Apr 2007 07:41:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[actionScript]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[bundle]]></category>
		<category><![CDATA[failure]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[scaffhold_resource]]></category>
		<category><![CDATA[spec]]></category>
		<category><![CDATA[textmate]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/04/19/rails-and-flash</guid>
		<description><![CDATA[With the recent Buzz around Adobe Apollo I figured out that since I recently switched to Mac and that I didn&#8217;t try the latest Flex upgrade, I should try Flex 2.01 for Mac. I really like what Adobe did with Flex, Unit testing, better accessibility, etc&#8230; but one thing I regret, it&#8217;s getting closer and [...]]]></description>
			<content:encoded><![CDATA[<p>With the recent Buzz around <a href="http://labs.adobe.com/wiki/index.php/Apollo">Adobe Apollo</a> I figured out that since I recently switched to Mac and that I didn&#8217;t try the latest Flex upgrade, I should try <a href="http://www.adobe.com/products/flex/">Flex 2.01</a> for Mac. </p>
<p>I really like what Adobe did with Flex, Unit testing, better accessibility, etc&#8230; but one thing I regret, it&#8217;s getting closer and closer to Java and <a href="http://flexblog.faratasystems.com/?p=115">AS3 syntax</a> is a pain to use when you got used to Ruby. </p>
<p>Anyway, what I really wanted to do was to have Flash quickly access my Restful Rails app. The adobe guys came up with a <a href="http://code.google.com/p/rubyonrails-ria-sdk-by-adobe/">RoR Ria SDK</a> but well&#8230;.   it only covers Flex and requires FlashPlayer 9.<br />
I also found some great <a href="http://blog.vixiom.com/2006/08/23/flash-remoting-for-rails-tutorial/">tutorials</a> on how to use the efficient  <a href="http://osflash.org/documentation/amf">AMF messaging protocol</a> with Rails using the <a href="http://www.themidnightcoders.com/weborb/rubyonrails/index.htm">WebOrb for Rails plugins</a></p>
<p>All that was really nice and I had fun, but it was an overkill for what I wanted to do. Let me show you how in less than 5 minutes how you can access you Rails Model from Flash and add some new item directly from Flash.</p>
<p>Create your new Rails app and use the script/generate scaffold_resource command to generate your Event Model.</p>
<pre><code>script/generate scaffold_resource Event
</code></pre>
<p>It should create all that for you:</p>
<pre><code>exists  app/models/
  exists  app/controllers/
  exists  app/helpers/
  create  app/views/events
  exists  test/functional/
  exists  test/unit/
  create  app/views/events/index.rhtml
  create  app/views/events/show.rhtml
  create  app/views/events/new.rhtml
  create  app/views/events/edit.rhtml
  create  app/views/layouts/events.rhtml
  identical  public/stylesheets/scaffold.css
  create  app/models/event.rb
  create  app/controllers/events_controller.rb
  create  test/functional/events_controller_test.rb
  create  app/helpers/events_helper.rb
  create  test/unit/event_test.rb
  create  test/fixtures/events.yml
  exists  db/migrate
  create  db/migrate/001_create_events.rb
  route  map.resources :events
</code></pre>
<p>let&#8217;s edit the migration file:<br />
db/migrate/001<em>create</em>events.rb</p>
<pre><code>class CreateEvents &lt; ActiveRecord::Migration
  def self.up
    create_table :events do |t|
      t.column :title, :string
      t.column :description, :string
      t.column :location, :string
        t.column :starts_at, :datetime
        t.column :ends_at, :datetime
    end
  end

  def self.down
    drop_table :events
  end
end
</code></pre>
<p>And let&#8217;s add some fixtures:<br />
test/fixtures/events.yml</p>
<pre><code>meeting:
  id: 1
  title: Meeting
  description: Boring meeting with the whole staff
  location: conference room
  starts_at: 2007-11-02 09:00:00
  ends_at: 2007-11-02 10:30:00
Joe_bday:
  id: 2
  title: Joe Bday Party
  description: Come and celebrate Joe's Birthday
  location: Lapin Agile Pub
  starts_at: 2007-09-07 20:00:00
  ends_at: 2007-09-07 23:30:00
</code></pre>
<p>Ok, now simply migrate your database,load the fixtures and start the webrick:</p>
<pre><code>rake db:migrate
rake db:fixtures:load
script/server
</code></pre>
<p>Great, we are done with Rails.  </p>
<p>Let&#8217;s launch Flash</p>
<p>Create a new Flash document and create a new .As fie in TextMate (or your favorite editor). We&#8217;ll write a quick ActionScript class to access Rails.</p>
<pre><code>class Restfulflash{
    public var gateway:String;

    function Resftfulflash(gateway:String){
        this.set_gateway(gateway);
    }
    public function set_gateway(gateway:String){
        this.gateway = gateway;
        trace("gateway set to:"+gateway);
    }

    public function get(model, callback){
        var railsReply:XML = new XML();
        railsReply.ignoreWhite = true;
        railsReply.onLoad = function(success:Boolean){
            if (success) {
                    trace ('Rails responded: '+railsReply);
                    callback.text = railsReply;
            } else {
                    trace ('Error while waiting for Rails to reply');
               callback.text = 'error';
            }
        }
        var railsRequest:XML = new XML();
        railsRequest.contentType="application/xml";
        railsRequest.sendAndLoad(this.gateway+model, railsReply);
        delete railsRequest;
    }

    public function create(model, newItem){
        railsRequest.onLoad = function(success){
                trace("Item creation success: " + success);
                trace(this);
        };
        var railsRequest:XML = new XML();
        railsRequest.parseXML(newItem);
        railsRequest.contentType="application/xml";
        railsRequest.sendAndLoad(this.gateway+model+'/create/', railsRequest,'POST');
        delete railsRequest;
    }
}
</code></pre>
<p>Save this file in the same directory as your .fla file  </p>
<p>In your fla file add:</p>
<pre><code>// Create a XML object to hold the events from our Rails app
rails_events = new XML();

// Prepare the connection to Rails (it would be nicer to do that in 1 step, but to make things clearer i decided to do it in 2)
var rails:Restfulflash = new Restfulflash();
rails.set_gateway("http://localhost:3000/");

// Get the events from rails and load the result in the rails_event XML object.
rails.get('events', rails_events);
trace(rails_events);

// Let's create a new event
newEvent = new XML('&lt;event&gt;&lt;description&gt;Spend some time with Grandma before its too late&lt;/description&gt;&lt;ends-at type="datetime"&gt;2007-11-02T18:30:00-07:00&lt;/ends-at&gt;&lt;id type="integer"&gt;1&lt;/id&gt;&lt;location&gt;Paris, France&lt;/location&gt;&lt;starts-at type="datetime"&gt;2007-11-02T16:00:00-07:00&lt;/starts-at&gt;&lt;title&gt;Visit Grandma&lt;/title&gt;&lt;/event&gt;&amp;#8217;)
rails.create(&amp;#8216;events&amp;#8217;, newEvent);

// Verify  that the event was added
rails.get(&amp;#8216;events&amp;#8217;, rails_events);
trace(rails_events);
</code></pre>
<p>There you go, you have all the events provided to you by Rails nicely prepared in an easy to parse XML object. You can bind the results to a Datagrid or display the info the way you want it. Ohh and by the way, we just added a new Event to the database&#8230; easy, isn&#8217;t it?  The code is a bit dirty but it&#8217;s still a good example why you need to use REST and how easy it is to get Flash to talk with Rails. (I strongly encourage that you also look at the very good WebOrb plugin for Rails)</p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/04/09/rails-and-flash/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

