tag:blogger.com,1999:blog-273933562024-03-07T20:29:18.199-08:00Barefoot DevelopmentA web application development blog of techniques and practices we've have come up with in the course of building web-based applications. Since we've benefitted from others sharing ideas, this is our way of giving back.
We build applications for <a href="http://www.thinkbarefoot.com">Barefoot</a>, a Cincinnati-based Advertising/Interactive agency.Ryan Hamiltonhttp://www.blogger.com/profile/06518998741954456358noreply@blogger.comBlogger36125tag:blogger.com,1999:blog-27393356.post-86935765099825916082008-02-08T11:48:00.000-08:002008-02-08T11:52:46.462-08:00Barefoot is hiring developers againTake a look at our website for these two job openings and how to apply.<br /><br /><ol><li><a href="http://www.thinkbarefoot.com/jobs/development/2008/01/senior-application-developer-rails/">Senior Rails application developer</a></li><li><a href="http://www.thinkbarefoot.com/jobs/development/2008/02/web-application-developer-2/">Web application developer</a></li></ol>Sean Brownhttp://www.blogger.com/profile/10018498282709787995noreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-34170942212781620362008-02-01T10:48:00.001-08:002008-02-01T11:36:52.970-08:00DRY Up Your SWF SEOWe've talked about <a href="http://barefootdevelopment.blogspot.com/2006/05/flash-seo-home-run-for-barefoot-part-1.html">search engine optimization with Flash before</a>, and for large database-driven sites the methodology still applies. But for smaller Flash sites that still have great content there may be a simpler, DRYer (Don't Repeat Yourself) solution.<br /><br />This technique, just like <a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript">unobtrusive javascript</a>, works best when the page is first designed for the lowest common denominator (in this case search bots or folks without Flash) and then <i>progrssively enhanced</i> with a richer experience when supported. With that in mind, let's start with the navigation.<br /><br /><code><div id="nav"><br /> <ul><br /> <li><a href="/" class="selected">Home</a></li><br /> <li><a href="/about/">About Us</a></li><br /> <li><a href="/contact/">Contact Us</a></li><br /> </ul><br /></div></code><br /><br />Next, our page needs some content. It may have images, paragraphs, bulleted lists and some formatting. It's not XML, so we'll want to keep the tags that lend the formatting.<br /><br /><code><div id="content"><br /> <p>Hello <b>World</b></p><br /></div></code><br /><br />This basic HTML has created a great foundation for our site. Screen readers, search engines, and iPhone users will all be able to enjoy our content. But for folks with Flash, we can make the experience even better. If we place the nav and content divs inside a wrapper div, SWFObject can replace the contents of the wrapper with our nifty Flash movie.<br /><br />Getting started, that unordered list nav looks very familiar as HTML, but it also looks like something else — XML. So why don't we load this XML into our Flash movie? It even tells us which page we're on (via <code>class="selected"</code>). That will allow great flexibility with the design of the nav in Flash. Using <a href="http://blog.deconcept.com/swfobject/">SWFObject</a>, it's as simple as this:<br /><br /><code>so.addVariable("xml_input", escape(document.getElementById("nav").innerHTML));</code><br /><br />Now we should feed the content into Flash the same way as the faux XML nav:<br /><br /><code>so.addVariable("html_input", escape(document.getElementById("content").innerHTML));</code><br /><br /> All that's left is to parse the XML for the nav and render the HTML from the content. Executions will vary, but this should get us started:<br /><br /><code>// Parse the xml content<br />XML.prototype.ignoreWhite = true;<br />var myXML:XML = new XML();<br />myXML.parseXML(_level0.xml_input);<br />var rootNode:XMLNode = myXML.firstChild;<br />...<br />// Set the html content<br />_root.content_txt.htmlText = _level0.html_input;</code><br /><br />This method allows the developer to spend more time building a sound, accessible site and less time creating XML schemas to load in duplicate content for two presentations. Flash supports a <a href="http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_14808">good subset of HTML tags</a> as well as custom CSS, so you should be able to match most of the usual content formatting.<br /><br />Bobby Uhlenbrock, Application Developer, <a href="http://www.thinkbarefoot.com">Barefoot</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-26427418942849046482007-12-04T15:35:00.000-08:002007-12-04T15:37:54.313-08:00NetBeans Ruby Debugger Doesn't Like Symbolic LinksLet me say that the new <a href="http://www.netbeans.org/" target="_blank">NetBeans 6 IDE</a>, released in its final version just yesterday, is an amazing <a href="http://www.rubyonrails.org" target="_blank">Ruby on Rails</a> development environment. I love the intellisense features, quick-switching between controller, view, and test files, and so many other things. But the biggest thing I was looking forward to was the integrated debugger.<br /><br />Unfortunately, on my Mac OSX Tiger system I couldn't get breakpoints to work. NetBeans just ignored them. I spent several hours trying to debug it, unfortunately led astray by a <a href="http://en.wikipedia.org/wiki/Red_herring" target="_blank">red herring</a>. Since I'm using <a href="http://weblog.rubyonrails.org/2007/11/29/rails-2-0-release-candidate-2" target="_blank">Rails 2.0 RC2</A> on my current project, and a new empty app based on Rails 1.2.5 worked great in the NetBeans debugger, I though it must be some problem in Rails 2.0. However, after eliminating everything different between the two apps, I found the real culprit.<br /><br />I keep all of my production working directories in a main directory, and have a symbolic link in my home directory to provide quick access. Unfortunately, when the NetBeans project includes a symbolic link, breakpoints in the debugger don't fire. When I changed my reference to use the absolute path (sans symlink), the breakpoints work great. <br /><br />Now NetBeans and I are getting along great. I highly recommend it for any Ruby or Java development.<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-11425163733414680862007-11-27T12:36:00.001-08:002007-11-28T07:29:28.936-08:00Processing Remote Files with Attachment FuI've used <a href="http://techno-weenie.net/">Rick Olson's</a> <a href="http://www.clarkware.com/cgi/blosxom/2007/02/24">attachment_fu</a> on a few projects now, and it's become one of the first plugins I import into my <a href="http://rubyonrails.org/down">rails</a> directory. But for my current task I need to not only upload photos but also import external images from a data service and run them through the usual cropping and scaling. I think it is ideal for all images, regardless of source, to be run through the same model where they are subjected to the same operations and validations. So that's what we'll do.<br /><br />Currently, when you post a multi-part form with a file input box named <code>uploaded_data</code>, attachment_fu will grab that TempFile with a setter of the same name and do its magic. I have followed the same process with a new method. It can go right in the model that <code>has_attachment</code>:<br /><pre><code><br /> # Takes input of a remote file via an absolute URL,<br /> # reads it and passes it to attachment_fu for processing <br /> def remote_data=(file_url)<br /> return nil if file_url.nil? <br /> open(file_url) do |data|<br /> # extract the filename and extension from the url<br /> temp_filename = URI.split(file_url)[5][/[^\/]+\Z/]<br /> # pass details to attachment_fu<br /> self.filename = temp_filename<br /> self.temp_data = data.read<br /> self.content_type = data.content_type <br /> end <br /> end<br /></code></pre><br />This opens the attachment and places the contents in a TempFile just as if it had been uploaded. Keep in mind that while my input data is quite reliable, you may need to add a few checks to ensure you have a valid extension and mime type. You can use the new method thusly:<br /><pre><code><br /> photo = Photo.new<br /> photo.remote_data = "http://www.somewhere.com/an_image.jpg"<br /> photo.save<br /></code></pre><br />Bobby Uhlenbrock, Application Developer, <a href="http://www.thinkbarefoot.com">Barefoot</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-76527055103031967712007-11-09T10:12:00.000-08:002007-11-13T06:18:36.363-08:00A Fix for a WordPress Paging ProblemI recently discovered a problem in the next/previous paging links of a custom <a href="http://wordpress.org/">WordPress</a> 2.2.1 site we created for <a href="http://www.fractionaljetsfocus.com">Fractional Jets Focus</a>. When viewing a list of articles by category, the previous posts link generated a 404 error. <br /><br />After digging for too long through WordPress code and <a href="http://www.google.com">Google</a> search results, I found that the problem was related to an apparent bug or conflict between the paging feature and custom permalinks. <br /><br />Our custom permalink setting in Options -> Permalinks is this:<br /><br /><code>/%category%/%year%/%monthnum%/%postname%/</code><br /><br />When you want to view all the posts for a certain category, an example URL is /blog/2007/9/. The problem is that the URL generated by <code>next_posts_link()</code> was misinterpreted by WordPress because of the permalink. The link was: <code>/blog/2007/9/page/2/</code>. Unfortunately, the string "page" in this URL was interpreted as a post name, instead of a token for the page index.<br /><br />There is probably some voodoo I could have done in the .htaccess file with mod_rewrite to fix the issue, but I feared breaking something else. So, I wrote the following plugin. It's so short that I'm publishing the whole thing inline.<br /><pre><code><br /><?php<br />/*<br />Plugin Name: Fix Paging in Category Listings <br />Plugin URI: http://www.thinkbarefoot.com<br />Description: Fixes a bug where next/previous links are broken in category by year/month listings<br />Version: 0.5<br />Author: Doug Smith<br />Author URI: http://www.thinkbarefoot.com<br /><br />Copyright 2007 Doug Smith (email: dsmith@thinkbarefoot.com)<br /><br />This program is free software; you can redistribute it and/or modify<br />it under the terms of the GNU General Public License as published by<br />the Free Software Foundation; either version 2 of the License, or<br />(at your option) any later version.<br /><br />This program is distributed in the hope that it will be useful,<br />but WITHOUT ANY WARRANTY; without even the implied warranty of<br />MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />GNU General Public License for more details.<br /><br />*/<br /><br />/**<br /> * Function to fix problem where next/previous buttons are broken on list<br /> * of posts in a category when the custom permalink string is:<br /> * /%category%/%year%/%monthnum%/%postname%/ <br /> * The problem is that with a url like this:<br /> * <br /> * /category/2007/10/page/2<br /> * <br /> * the 'page' looks like a post name, not the keyword "page"<br /> */<br />function remove_page_from_query_string($query_string)<br />{ <br /> if ($query_string['name'] == 'page' && isset($query_string['page'])) {<br /> unset($query_string['name']);<br /> // 'page' in the query_string looks like '/2', so split it out<br /> list($delim, $page_index) = split('/', $query_string['page']);<br /> $query_string['paged'] = $page_index;<br /> } <br /> return $query_string;<br />}<br /><br />add_filter('request', 'remove_page_from_query_string');<br /><br />?><br /></code></pre><br />This plugin simply checks to see whether the post name is 'page', and if there is a 'page' parameter too. If so, it removes the 'name', and assigns the page index to the magic 'paged' parameter. Paging restored.<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Unknownnoreply@blogger.com25tag:blogger.com,1999:blog-27393356.post-81502370201118291152007-11-01T11:08:00.000-07:002007-11-01T12:19:10.469-07:00I'm Glad Rails Loves SQLIt's a good thing that the core team for <a href="http://www.rubyonrails.org/">Ruby on Rails</a> has chosen to embrace SQL, and easily expose it to developers. Other object-relational mapping layers often try to hide SQL as 'evil'. However, there are times when nothing will substitute for being able to talk directly to the database, in its own language.<br /><br />Yesterday was such a time. I have an application that displays articles divided into hierarchies of categories. Articles are rendered differently on the site depending on their category. <br /><br />I needed to change a list of articles from one top-level category (let's call it food) so that some articles in one of its sub-categories (call it expert reviews) only appeared one time per sub-category. So, while articles from other sub-categories of food would all appear in the list, only the latest article from any expert reviewer would appear in that same list. <br /><br />I wanted to handle this in the model using a custom finder so that controllers could just call something like <code>Category.published_article_list(options)</code> and the details would be irrelevant. <br /><br />Ok, enough background, here's the method I added to the Category model:<br /><pre><code><br />def published_article_list(options = {})<br /> if (food?)<br /> # Special query that returns only the latest article for each 'reviewer' category<br /> sql = "mid_category_id != #{REVIEW_CAT} OR articles.id IN ( "<br /> sql += "SELECT SUBSTRING( MAX( CONCAT( published_on, id ) ), 11 ) AS ra_id "<br /> sql += "FROM articles "<br /> sql += "WHERE mid_category_id = #{REVIEW_CAT} AND "<br /> sql += " #{Article.conditions_published} "<br /> sql += "GROUP BY category_id ) ) "<br /> options[:conditions] = sql<br /> end<br /> self.published_root_articles.find(:all, options)<br />end<br /></code></pre><br />That SQL is a little serious, so here's what it does in English. It starts by removing any articles that have a mid-level category of "review". It then adds the articles from that category that we want using a subquery. <br /><br />The meat of the subquery is in this part of the <code>SELECT</code> clause: <code>SUBSTRING( MAX( CONCAT( published_on, id ) ), 11 ) AS ra_id</code>. I needed to get the ID of the latest article, grouped by category_id (the individual reviewer). So, this code concatenates the published_on date to the article ID, finds the max of all those values, then uses <code>SUBSTRING</code> to chop the concatenated date off, leaving only the id of the latest article. So, for a list of articles with IDs and dates like this:<br /><pre><code><br />id date category_id<br />101 2007-10-01 1<br />102 2007-09-30 1<br />103 2007-10-02 2<br />104 2007-10-01 2<br /></code></pre><br />When the concatenated dates and IDs are sorted, they end up like this:<br /><code><br />2007-10-02103<br />2007-10-01104<br />2007-10-01101<br />2007-09-30102<br /></code><br />Using this method, the subquery accurately adds the IDs of articles 101 and 103 to the IN clause above. <br /><br />The only downside to this method is that the <code>CONCAT</code> function requires a full table scan, so indexes won't be used in the subquery. Since this page is cached, it wasn't a problem for this application, but if performance becomes an issue other optimization could be done while still using this strategy.<br /><br />Thanks for loving SQL, Rails!<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-86774681802224381832007-10-19T14:49:00.000-07:002007-11-27T19:55:30.899-08:00classic_pagination is not my friendI recently upgraded a nearly completed <a href="http://www.rubyonrails.org">Ruby on Rails</a> project to version <a href="http://weblog.rubyonrails.org/2007/10/5/rails-1-2-4-maintenance-release">1.2.4</a>, then to version <a href="http://weblog.rubyonrails.org/2007/10/12/rails-1-2-5-maintenance-release">1.2.5</a>. I worked through the deprecation warnings and everything seemed good to go. <br /><br />Then, I ran into a snag with pagination -- something I didn't have covered in my functional tests. Because I'm caching this site, I have routes that include the <a href="http://www.railsenvy.com/2007/2/28/rails-caching-tutorial#pagination">:page property for the paginator in the URL</a>. Otherwise, pages 2-n don't get cached.<br /><br />After installing the <a href="http://plugins.require.errtheblog.com/browser/classic_pagination">classic_pagination plugin</a>, I'd get an error when viewing pages with pagination code like this in the view:<br /><code><br /> <%= link_to('« Previous', :page => @article_pages.current.previous) if @article_pages.current.previous %><br /> <%= link_to('Next »', :page => @article_pages.current.next) if @article_pages.current.next %><br /></code><br />This is the standard, recommended method for creating the Next and Previous links. The error said: <code>undefined method `paginator' for "2":String</code>. The top-most offending line of the stack trace was: <code>vendor/plugins/classic_pagination/lib/pagination.rb:307:in `=='</code>.<br /><br />The confusing part was that when I removed the :page from the route and added ?page=2 back as a query string parameter, the error went away. In theory, the :page should be the :page whether it is on the query string or embedded in the URL, right?<br /><br />Well, long story short, here's the fix. Unfortunately, I can't explain exactly why this is necessary, and don't have the time now to dig into the innards of classic_pagination and the supporting Rails infrastructure to find out. I just hope this post helps somebody. <br /><br />Oh, the fix? Add the "number" method to the previous or next Page object reference, like so:<br /><code><br /> <%= link_to('« Previous', :page => @article_pages.current.previous<b>.number</b>) if @article_pages.current.previous %><br /> <%= link_to('Next »', :page => @article_pages.current.next<b>.number</b>) if @article_pages.current.next %><br /></code><br />I'll probably consider classic_pagination my friend again once I get over being grumpy about this ... or, I may consider one of the <a href="http://plugins.require.errtheblog.com/browser/will_paginate">other</a> <a href="http://cardboardrocket.com/pages/paginating_find">pagination</a> <a href="http://paginator.rubyforge.org/">contenders</a> for a future application.<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-27393356.post-36401894694240150042007-10-04T08:13:00.000-07:002007-10-04T08:17:37.254-07:00Attorneys on RailsCross-posting this link to our main Barefoot blog, where we describe <a href="http://www.thinkbarefoot.com/blog/technology/2007/09/attorneys-on-rails/">a significant Rails site we recently completed</a>, and show at a high level how we accomplished it. Let us know if you'd like more details.<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-22506316727146236482007-09-19T13:11:00.000-07:002007-09-19T13:48:37.290-07:00Disable items in Flash ComboBox componentRecently, Barefoot built a new promotional site for Miller Lite–<a href="http://millerpregame.com/">millerpregame.com</a>. The site is a cross promotion with Wal-Mart. The main attraction allows users to vote on their favorite NFL city to tailgate at. We built a small voting widget in Flash and utilize Flash Remoting. It is a simple form really, just a <a href="http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00002139.html">ComboBox</a> that lists the 32 NFL teams and a textfield so users can add comments as to why the city submitted is really the "best."<br /><br />Each week the votes are tallied and a city is declared a winner. We then compile tidbits about the city, the atmosphere the fans create, and any pregame rituals that have been submitted. Once a city wins they are then taken out of the list of eligible cities going forward throughout the season.<br /><br />To avoid confusion, we wanted to grey out the previous weeks winners in the ComboBox. Disabling options in the ComboBox Component should be as easy as setting a property in the dataprovider right? Wrong.<br /><br />Luckily, the ComboBox uses the <a href="http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00002485.html">List Component</a> and the List Component can utilize the <a href="http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00002101.html">CellRenderer API</a>. The CellRenderer API is a way of styling and manipulating the individual items (aka "cells") in a List Component.<br /><br />To implement your own extended <a href="http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00002101.html">CellRenderer Class</a>, you simply define the class with a few required methods and properties. Similar to an Interface structure in Java, etc...<div><br /></div><div>You then create a MovieClip in your library and define it's linkage properties using the Class you have created to be your CellRenderer.</div><div><br /></div><div>Next, you designate the cellrenderer property of the List inside the ComboBox to point at your new Class. </div><div><code><br />// define cellRenderer class<br />comboBox.dropdown.cellRenderer = "MyCell";<br /></code><br />MyCell is the name given to the identifier in the MovieClip linkage properties mentioned above.</div><div><br /></div><div>OK, so now we have more control over "cell" rendering. We now use the setValue method required in our new MyCell class.</div><div><br /></div><div>We decided to set a property in our dataprovider array that would identify which items in the ComboBox we wanted to disable.</div><div><br /></div><div><code>// for example<br />cities.push({label: 'Cincinnati', data: 7, selectable: 'false'});<br />comboBox.dataProvider = cities;<br /></code><br /></div><div>We then check for this property in the setValue method.</div><div><br /></div><div><code>// define the item index<br />var cellIndex:Number = getCellIndex().itemIndex;<br /><br />// Apply text formatting<br />if (listOwner.dataProvider.getItemAt(cellIndex).selectable == 'false') {<br />// code goes here<br />}<br /></code><br /></div><div>If that if statement is true we set the cell textfield to html so we can easily style it using a font tag and setting a color like grey to indicate to the user the option is disabled. We also need to remove the default rollover action and onpress action for the cell.</div><div><br /></div><div><code>_tLabel.html = true;<br />_tLabel.htmlText = '<font style="color:#DDDDDD;">' + sLabel + '</font>';<br />// define row<br />var row = this._parent;<br />// remove the highlight<br />row.highlight = row.background;<br />// add a fake onPress event<br />row.onPress = null;<br />// don't show the hand cursor<br />row.useHandCursor = false;<br /></code><br /></div><div>That is it! Items we designate are now disabled and appear greyed out. Some times Flash is better than HTML and other times HTML is just easier.</div><div><br /></div><div><br /></div>Mikehttp://www.blogger.com/profile/03660862347691755922noreply@blogger.com2tag:blogger.com,1999:blog-27393356.post-80321032969562467792007-07-16T16:24:00.001-07:002007-07-17T10:50:46.227-07:00JRuby on Rails -- PerformanceI have been spending some time evaluating <a href="http://jruby.codehaus.org/">JRuby 1.0</a> as a possible <a href="http://www.rubyonrails.org/">Ruby on Rails</a> deployment platform. Following RailsConf 2007, I was very excited by the potential of JRuby as a vehicle for deploying Rails apps that would have the advantages of robust J2EE application servers, including multi-threaded load management, database connection pools, and wide-ranging enterprise support.<br /><br /><b>JRuby Install</b><br /><br />We typically deploy Rails apps using <a href="http://mongrel.rubyforge.org/index.html">mongrel</a> clusters behind an <a href="http://httpd.apache.org/">Apache 2</a> webserver. So, I started with a pretty straightforward Rails app I'm building, and deployed it as normal on the mongrels. <br /><br />(By the way, unless otherwise mentioned, software versions used are native Ruby 1.8.6, Rails 1.2.3, JRuby 1.0, <a href="http://tomcat.apache.org/">Tomcat 6.0.13</a>, Java 1.5.0_07 on OSX, <a href="http://java.sun.com">Java 1.6.0</a> on RedHat Linux, Apache web server 2.2.3.)<br /><br />The next step was to get JRuby and setup a .war file to deploy. I found <a href="http://www.techcfl.com/blog/?p=109">this blog post to be a very helpful</a> start. I already had the latest Java from Apple installed, so I installed JRuby to <code>/usr/local/jruby-1.0</code>. I edited <code>/etc/profile</code> to include the following:<br /><pre><code>export JRUBY_HOME="/usr/local/jruby-1.0"<br />export PATH="/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin:$JRUBY_HOME/bin"<br />export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Home"<br />export ANT_HOME="/Developer/Java/Ant"</code></pre><br />I decided to deploy the .war into Tomcat since I'm more familiar with it than the recommended <a href="https://glassfish.dev.java.net/">Glassfish</a> and I knew how to connect it well with Apache. So, I created a JDBC connection pool to the MySQL database by adding a META-INF directory to the Rails app, with the following <code>context.xml</code> file:<br /><pre><code><Context path="/myapp" reloadable="true" crossContext="true"><br /> <!-- Database Connection Pool --><br /> <Resource name="jdbc/myapp" <br /> auth="Container" <br /> type="javax.sql.DataSource"<br /> factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"<br /> maxWait="1000"<br /> removeAbandoned="true"<br /> maxActive="30"<br /> maxIdle="10"<br /> removeAbandonedTimeout="60"<br /> logAbandoned="true"<br /> username="myuser" <br /> password="mypass" <br /> driverClassName="com.mysql.jdbc.Driver"<br /> url="jdbc:mysql://127.0.0.1:3306/myapp_development?autoReconnect=true" /><br /></Context></code></pre><br />This is the corresponding <code>database.yml</code> file that tells Rails where to find the connection pool data source:<br /><pre><code>development:<br /> adapter: jdbc<br /> jndi: java:comp/env/jdbc/myapp<br /> <br />production:<br /> adapter: jdbc<br /> jndi: java:comp/env/jdbc/myapp</code></pre><br />With these configuration files, I went through the JRuby process for creating the .war file. I setup Tomcat and deployed the app, which worked after I put the MySQL driver into Tomcat's /lib directory. I connected the Tomcat app to Apache with mod_proxy_ajp. I can post more details about these configs if there is interest.<br /><br />One important optimization that I added was to include JAVA_OPTS that launched Tomcat with enhanced memory heap limits for the JVM, and with a couple other parameters I found while searching for JRuby optimizations online. So, I added this line to my <code>/etc/profile</code>:<br /><pre><code>export JAVA_OPTS="-Xms128m -Xmx512m -Djruby.objectspace.enabled=false -Djruby.jit.enabled=true"</code></pre><br /><b>Performance</b><br /><br />After configuring and setting everything up on my MacBook Pro (2GHz Intel Core Duo, 1.5GB RAM), I was excited by the performance of JRuby. Using simple comparison of wget spiders between the native Ruby on Rails (using 3 mongrels behind Apache) and JRuby versions, I was seeing over a 2x improvement in JRuby. The spider of around 24 URLs was taking about 10 seconds on native Ruby, while only about 4 seconds on JRuby/Tomcat.<br /><br />I then moved to deploying on our RedHat Linux-based staging server, where the 24 URL spider was running in about 1.5 seconds with native Ruby (same 3 mongrel cluster behind Apache 2). With repeated tweaking, I couldn't get the JRuby/Tomcat to run faster than 4 seconds, even swapping out JVMs of Java 5 and Java 6. <br /><br />I then considered that JRuby might have an advantage under load, so I setup <a href="http://grinder.sourceforge.net/">Grinder</a>, an open-source Java-based load testing tool, to "grind" both versions. The test simulated 10 simultaneous threads from three users, with each test repeated three times. Here are some comparison numbers of a couple URLs (times in milliseconds):<br /><br /><TABLE style="text-align: right;"> <TR> <th>Run #</th> <th>Native A</th> <th>Java A</th> <th>Native B</th> <th>Java B</th> </TR> <TR> <TD>1</TD> <TD>554</TD> <TD>1070</TD> <TD>586</TD> <TD>378</TD> </TR> <TR> <TD>2</TD> <TD>861</TD> <TD>928</TD> <TD>519</TD> <TD>885</TD> </TR> <TR> <TD>3</TD> <TD>719</TD> <TD>1450</TD> <TD>496</TD> <TD>680</TD> </TR> <TR> <TD>4</TD> <TD>833</TD> <TD>800</TD> <TD>484</TD> <TD>507</TD> </TR> <TR> <TD style="border-bottom: 1px solid #000000;">5</TD> <TD style="border-bottom: 1px solid #000000;">863</TD> <TD style="border-bottom: 1px solid #000000;">827</TD> <TD style="border-bottom: 1px solid #000000;">701</TD> <TD style="border-bottom: 1px solid #000000;">414</TD> </TR> <TR> <TD>Average</TD> <TD>766</TD> <TD>1015</TD> <TD>557</TD> <TD>572</TD> </TR></TABLE><br />On average, JRuby is either slower or nearly equal in performance under load. It's still respectable considering JRuby's 1.0 status. The main downside was the significant disparity in CPU and RAM usage between JRuby and native Ruby. JRuby pegged the dual core CPUs under load, using upwards of 250MB of RAM, while the mongrels maintained under 30% CPU with about 40MB per mongrel. <br /><br />I remain excited about JRuby, but can't use it for production work quite yet. I'm sure the JRuby team will continue with optimization -- and I'll be keeping a close eye on its progress for sure.<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Unknownnoreply@blogger.com11tag:blogger.com,1999:blog-27393356.post-15695842887914527072007-06-25T06:46:00.001-07:002007-06-25T06:48:51.350-07:00Barefoot is adding a developerBarefoot has an immediate opening for a Web Application Developer. We’d like you to have 3+ years experience developing database-driven, dynamic web sites. We’re strong in a number of technologies, the primary ones being Ruby on Rails, C#.NET, and PHP. You need to be very strong in one of them, and if you’ve got experience in a second, bonus points. You’ve also got to be at least comfortable with client-side Web development (JavaScript, CSS, XHTML), although that won’t be the main part of your job. You’ll receive excellent benefits and a salary commensurate with your experience. <a href="http://www.thinkbarefoot.com/jobs/development/2007/06/web-application-developer/">To submit your resume, go here.</a>Sean Brownhttp://www.blogger.com/profile/10018498282709787995noreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-14049796957306997002007-05-31T15:16:00.000-07:002007-06-01T10:09:33.188-07:00RESTful Rails Naming Conventions with Nested ControllersI recently spent some time learning how to name everything in <a href="http://www.rubyonrails.org">Rails</a>' new <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">RESTful</a> <a href="http://api.rubyonrails.org/classes/ActionController/Resources.html">map.resources</a> world, given that I like to group my controllers into subdirectories. For typical content managed sites, I keep the main site in a directory <code>/site</code>, and the content management tools in a directory <code>/admin</code>. <br /><br />So, how do you get your syntactically-sugar-coated paths when you have a controller in a directory like this:<br /><br /><code>/app/controllers/admin/users_controller.rb</code><br /><br />The map.resources line that worked for me is:<br /><br /><code>map.resources :users, :controller => 'admin/users', :name_prefix => 'admin_', :path_prefix => '/admin'</code><br /><br />Some of the URLs of the resources that are automatically by that line include:<br /><br /><code>/admin/users</code>: list all users (index action)<br /><code>/admin/user/1</code>: show one user's details (show action)<br /><code>/admin/user/new</code>: create a new user (new action)<br /><br />When you want to use the <code>*_path</code> or <code>*_url</code> shortcuts for these nested controllers, these work:<br /><br /><code>admin_users_path</code>: renders <code>/admin/users</code><br /><code>admin_edit_user_path(@user)</code>: renders <code>/admin/user/1;edit</code><br /><br />Rails is opinionated, but once you figure out how to agree with its opinion of how things should be named, it makes the rest of the development process a lot easier.<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-27393356.post-49195722923561932682007-04-23T06:26:00.000-07:002007-04-28T13:36:47.685-07:00Ruby, mongrel_cluster, ferret and Linux PATHsI have less hair. There's no two ways about it. In a fit of self-pity this weekend, I considered getting some of that <a href="http://www.onlyhairloss.com/glh/">great spray paint</a> to solve the issue, but instead, I've decided to swear off various un*x distributions. Can't we all just get along? I digress.<br /><br />While preparing a recent <a href="http://www.rubyonrails.org">Rails</a> application for production, I came across some odd issues with various ruby-powered applications and <a href="http://docs.rubygems.org">gems</a>. I struggled to understand why these apps failed to launch on startup, but would work without a hitch when run "by hand" at the command prompt. After tearing through boot logs, application logs and a good portion of my hair, I finally figured it out: it's a <code>PATH</code> issue. On each of the boxes in question (a few Fedora Core and two RHE4 machines), <a href="http://www.ruby-lang.org">ruby</a> was installed in <code>/usr/local/bin</code>, and that, my friends, is not in the default <code>PATH</code> for root. So, I thought, "Easy fix. Simply add <code>/usr/local/bin</code> to the <code>PATH</code> in a system wide script like <code>/etc/profile</code>." Alas, t'was not to be. These newer incarnations of <a href="http://www.redhat.com">RedHat linux</a> have the added security of SELinux, which, among many positive things, sets and unsets root's <code>PATH</code> multiple times during the boot sequence. In the end, we had to add <code>/usr/local/bin</code> directly within each component's startup script. The Rails (and gems) components we are using are <a href="http://mongrel.rubyforge.org">mongrel</a>, <a href="http://mongrel.rubyforge.org/docs/mongrel_cluster.html">mongrel_cluster</a>, and <a href="http://ferret.davebalmain.com/trac/">ferret</a> (using the DRb server). For those keeping score at home, that's a 9.7 on the new technology in production (NTIP) scale. So you can keep your hair in tact, here are the startup scripts we're using.<br /><br />Mongrel cluster:<br /><br /><pre><code>#!/bin/bash<br />#<br /># Copyright (c) 2006 Bradley Taylor, bradley@railsmachine.com<br />#<br /># mongrel_cluster Startup script for Mongrel clusters.<br />#<br /># chkconfig: - 85 15<br /># description: mongrel_cluster manages multiple Mongrel processes for use \<br /># behind a load balancer.<br />#<br /><br />CONF_DIR=/etc/mongrel_cluster<br />RETVAL=0<br />PATH=/usr/local/bin:$PATH<br /><br /># Gracefully exit if the controller is missing.<br />which mongrel_cluster_ctl >/dev/null || exit 0<br /><br /># Go no further if config directory is missing.<br />[ -d "$CONF_DIR" ] || exit 0<br /><br />case "$1" in<br /> start)<br /> mongrel_cluster_ctl start -c $CONF_DIR<br /> RETVAL=$?<br />;;<br /> stop)<br /> mongrel_cluster_ctl stop -c $CONF_DIR<br /> RETVAL=$?<br />;;<br /> restart)<br /> mongrel_cluster_ctl restart -c $CONF_DIR<br /> RETVAL=$?<br />;;<br /> *)<br /> echo "Usage: mongrel_cluster {start|stop|restart}"<br /> exit 1<br />;;<br />esac</code></pre><br /><br />Ferret DRb server:<br /><pre><code>#!/bin/bash<br />#<br /># This script starts and stops the ferret DRb server<br /># chkconfig: 2345 89 36<br /># description: Ferret search engine for ruby apps.<br />#<br /># Sean Brown, Partner at Barefoot, Inc.<br />#<br /># save the current directory<br />CURDIR=`pwd`<br />PATH=/usr/local/bin:$PATH<br /><br />RORPATH="/path/to/ror_root"<br /><br />case "$1" in<br /> start)<br /> cd $RORPATH<br /> echo "Starting ferret DRb server."<br /> FERRET_USE_LOCAL_INDEX=1 \<br /> script/runner -e production \<br /> vendor/plugins/acts_as_ferret/script/ferret_start<br /> ;;<br /> stop)<br /> cd $RORPATH<br /> echo "Stopping ferret DRb server."<br /> FERRET_USE_LOCAL_INDEX=1 \<br /> script/runner -e production \<br /> vendor/plugins/acts_as_ferret/script/ferret_stop<br /> ;;<br /> *)<br /> echo $"Usage: $0 {start, stop}"<br /> exit 1<br /> ;;<br />esac</code></pre>Sean Brownhttp://www.blogger.com/profile/10018498282709787995noreply@blogger.com1tag:blogger.com,1999:blog-27393356.post-37929822807471244762007-04-12T14:42:00.000-07:002007-04-12T11:47:00.245-07:00We're hiring a UI Developer (XHTML/CSS)<a href="http://www.thinkbarefoot.com">We</a> have just posted <a href="http://jobs.37signals.com/jobs/1394">a position</a> on <a href="http://www.37signals.com">37Signals</a> job board for an experience UI Developer.<br /><br />--<br />Barefoot has an immediate opening for an experienced User Interface developer. Strong skills in XHMTL and CSS is a must. Significant experience in Javascript is a big plus. To be considered as a candidate, please submit at least 5 URLs for which you were either the only UI developer or at least the principal developer. No phone calls please.<br />--<br /><br />So, all you CSS wizards, this is your dream job. Cool agency. Hip modern design. And you get to bring it to life. Not to mention the development staff is cool too (I'm a little biased). If you understand server-side programming, that is a bonus. If you know some Flash/Actionscript that is a big bonus as well. The big chunk is XHTML/CSS. We're talking about browser compatibility, W3C standards, etc... We want to implement standards and if you know them and know how to code them, then we want you. We work with some amazing clients, including Miller Brewing Company, KnowledgeWorks Foundation, and many many other large and small companies. This is a cool position.<br /><br />Send your resume to <a href="mailto:recruiting@thinkbarefoot.com">recruiting@thinkbarefoot.com</a><br /><br /><br />Michael Krisher, Senior Developer, <a href="http://www.thinkbarefoot.com">Barefoot</a>Mikehttp://www.blogger.com/profile/03660862347691755922noreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-62266483065391985992007-03-09T11:22:00.000-08:002007-04-27T18:53:02.267-07:00Regex to display X words in RailsI just created a useful combination of regex's in <a href="www.rubyonrails.com">Ruby on Rails</a> and wanted to share (and remember). I needed to display a summary of an article, as the first group of words from a string that might contain HTML, start with garbage, or otherwise not be pretty. <br /><br />So, I stripped the HTML, removed non-printable stuff from the beginning, and came up with this to show the first 40 words of the article's body: <br /><br /><code>article.body.gsub(/<\/?[^>]*>/, "").gsub(/^\W*/, "")[/([\S]*[\W]*){40}/]</code><br /><br />Enjoy! (Or, leave a comment with a better way to do it.)<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-27393356.post-52753133402658529322007-03-01T09:58:00.000-08:002007-03-01T10:31:40.262-08:00The Joy of Rails Automated TestingMany developers are not in the habit of adding unit and functional tests to their projects. With many frameworks, testing is a tedious addition that can get pushed aside because of time constraints. However, with <a href="http://www.rubyonrails.com/">Ruby on Rails</a>, automated testing is baked right in, so there's no excuse for not using it. And, once you start using unit & functional tests, you will sleep better, have less stress, exercise more, lose weight, improve your marriage, and have more money to save for retirement. Well, all of those benefits may not come directly from automated testing, but you'll certainly build a much more high-quality application, and that can't hurt the rest of your life!<br /><br />I'm not going to go in-depth on the basics of unit/functional testing here ... many other resources do a great job of that, such as the excellent <a href="http://www.pragmaticprogrammer.com/titles/rails/index.html">Agile Web Development with Rails</a>. However, I wanted to show a simple & quick way to add functional tests in response to daily development problems.<br /><br />It happens all the time during development: you build a controller with a page and see "Action Controller: Exception caught" when you view it in your browser. You immediately move to the task of fixing whatever problem is revealed.<br /><br />However, it only takes an extra minute to add a new functional test that will give you the added assurance that once you fix the problem, <span style="font-style: italic;">you'll never be surprised by it again</span> because your test will automatically tell you if it happens again.<br /><br />They key is in the very nice display that Rails renders when an exception is caught. Immediately following the error trace, Rails displays the current Request parameters. For example, I just received an error on a seach page with parameters like this:<br /><h3>Request</h3><b>Parameters</b>: <code>{"commit"=>"Search", "office_id"=>"0", "school_name"=>nil, "first_name"=>nil, "last_name"=>nil}</code><br /><br />The parameters are in a hash that is in the exact form needed by the get or post methods you use within a Rails functional test. So here's what you do:<br /><br />1. Create a new method within the functional test file for your controller, something like this:<br /><code><br />def test_search_empty_params<br /> # Use all empty fields<br /> get :search<br /> assert_response :success<br /> assert_template 'search'<br /> assert_not_nil assigns(:search_params)<br /> assert_not_nil assigns(:results)<br />end<br /></code><br /><br />2. Copy & paste the list of request parameters from the error page in your browser to the second parameter of the get method (this also works with the post method), so your test method ends up like this:<br /><br /><code><br />def test_search_empty_params<br /> # Use all empty fields<br /> get :search, {"commit"=>"Search", "office_id"=>"0", "school_name"=>nil, "first_name"=>nil, "last_name"=>nil}<br /> assert_response :success<br /> assert_template 'search'<br /> assert_not_nil assigns(:search_params)<br /> assert_not_nil assigns(:results)<br />end<br /></code><br /><br />Now, go on to fix whatever caused the problem in the first place, and run your functional test to not only make sure your problem is fixed, but that your fix didn't break anything else. Now, you'll be sure that the problem will never show up again, at least, not without a message from your test. Doesn't that bring you joy?<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-1171483830742685132007-02-14T12:10:00.000-08:002007-02-16T09:47:41.333-08:00Flash: Classes and PackagesAt Barefoot we have many different kinds of developers. Some come from a Java background. Some have been using Flash since Macromedia purchased it and released it as Flash Version 3. In addition, we work with developers from other agencies or from our clients and that requires project portability.<br /><br />In this case, portability means the ability to package up a project with all of its pieces and deliver it to someone else in a way that they can begin working on the project without problems caused by missing dependencies.<br /><br />We also use Subversion as a version control system. One way we use Subversion is to deliver project packages. Because of the way Subversion works, we have to be concious of file system restraints. The majority of the time, this means all of our project files need to live under a main parent directory.<br /><br />This brings us to the organization of files that make up our projects, in this case Flash projects.<br /><br />At Barefoot, we borrow a practice from the Java paradigm called Classes and Packages. For in-depth information about using Classes and Packages in Flash, check out Collin Moock's O'Reilly book–Essential Actionscript 2.0 - <a href="http://moock.org/eas2/">http://moock.org/eas2/</a>.<br /><br />In simple terms, Classes are external Actionscript files. Classes are how logic for your Flash application is separated and stored.<br /><br />Packages are simply a set or group of interrelated classes in a file structure.<br /><br />So for example, say Barefoot has created a custom Video Player to play FLV files.<br /><br />We would have a Flash directory to hold all of our files related to the project. This includes any Classes. Default Macromedia Classes can stay in their default directory since we assume anyone else working on this project has Flash, including those core Classes. We also don't add Classes to the default Macromedia directory, all of our custom Classes stay with the project–including our FLA file.<br /><br />Inside our Flash directory we'll store our FLA(s). We'll also want to store our Packages, remembering that they are just groups of our Classes that are interrelated.<br /><br />This is where we borrow from Java and other mature language paradigms. We don't want our Packages, and the Classes inside them, to interfere with code from other developers. So, we store them in unique namespaces. The standard is to start this namespace with our company's domain name.<br /><br />Inside the Flash directory with our FLA is a "Classes" directory. This becomes the root directory of the classpath, or the place where we tell Flash to look for any classes it needs to find. In "Classes", we add a "com" directory. And inside that is a "thinkbarefoot" subdirectory. Get it, <a href="http://thinkbarefoot.com/">thinkbarefoot.com</a>? Now going back to the example, we'll create a "videoplayer" subdirectory inside "thinkbarefoot." Again, this is all covered in Moock's book.<br /><pre><br />\flash<br /> |<br /> +--\Classes<br /> | |<br /> | ---\com<br /> | |<br /> | ---\thinkbarefoot<br /> | |<br /> | ---\videoplayer<br /> | |<br /> | +--Player.as<br /> | +--Loader.as<br /> |<br /> +---video_player.fla<br /></pre>Inside the "videoplayer" directory will be all of our Classes (*.as), along with maybe some other subdirectories if we break things out further.<br /><br />Organizing our files like this has two advantages. First, a check-in to Subversion only has to contain one parent directory. Then, if we deliver the project to someone else, all we have to do is zip the Flash directory and they'll have everything they need. That portability is essential when you are working with multiple developers or sending your code to another company.<br /><br />Without this level of file organization, you may deliver an FLA and some of your logic, but forget one key Class that prevents a future developer from compiling a quick text fix. Adobe has made Actionscript 2.0+ very friendly for use with Classes and Packages. Give them a try, you'll be happy you did.<br /><br />Mike Krisher, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com/">Barefoot</a>Mikehttp://www.blogger.com/profile/03660862347691755922noreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-1169139026157435762007-01-18T08:47:00.000-08:002007-04-28T13:37:34.776-07:00SlideShowPro - XML attributesI am using the <a href="http://www.slideshowpro.net/">SlideShowPro</a> component in a Flash piece I am working on and so far it has been a great experience. This is a pretty solid as Sears component.<br /><br />However, I have the need to extend the XML schema and include additional attributes on my image nodes. It can be done no problem, as discussed <a href="http://www.slideshowpro.net/forums/viewtopic.php?id=1487">here</a> on the SlideShowPro forums. BUT, I noticed that the component converts all attribute names to lowercase.<br /><br />So, say you want to add an attribute containing the path to download the image, call it "downloadPath." Notice the uppercase P. When retrieving that attribute via the data Object created by SlideShowPro (eventObject.data) the name has to be all lowercase (i.e. eventObject.data.downloadpath). I'm going to stick to just using all lowercase attribute names in the XML to thwart off confusion.<br /><br />I was going to add this as a reply in the forums, however, they seem to be closed to new registrations?<br /><br />Michael Krisher, Senior Developer, <a href="http://www.thinkbarefoot.com">Barefoot</a><br /><i>cross posted <a href="http://www.mikekrisher.com/?p=436">here</a></i>Mikehttp://www.blogger.com/profile/03660862347691755922noreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-1167781899834029712007-01-17T08:45:00.000-08:002007-04-27T18:52:11.929-07:00Custom Sorting in RubyI have been an expert Java developer for many years, but over the past year I've had the opportunity to use Ruby on Rails for a couple real projects. I'm really enjoying Ruby and hope to to use it more often in the future.<br /><br />My current RoR project has several models with bi-directional <code>has_many :through</code> relationships where two models link to a third join model that has one or more additional property fields. For example, Users link to Albums through a Selections join model. The selections table includes fields for user_id and album_id, with an additional field that contains the rating each user gives to each album. (This example uses different entity names to protect the client.) <br /><br />These relationships are illustrated (with simplication) here:<br /><code><br />class User < ActiveRecord::Base<br /> has_many :selections, :dependent => :destroy<br /> has_many :albums, :through => :selections <br />end<br /><br />class Album < ActiveRecord::Base<br /> has_many :selections, :dependent => :destroy<br /> has_many :users, :through => :selections <br />end<br /><br />class Selection < ActiveRecord::Base<br /> belongs_to :user<br /> belongs_to :album <br />end</code><br /><br />I needed to be able to list all the albums chosen by a user, sorted by album name. I tried this: <br /><code><br />@user.selections.each do |selection|<br /> # ...<br />end</code><br /><br />but the album names were not sorted. I thought that the nifty option <code>:order => 'name'</code> would work on the <code>has_many :through</code>, but alas, it didn't. Then, my Java experience reminded me of the Comparable interface. It turns out that Ruby includes a very similar pattern, but it comes complete with Ruby nice-ness.<br /><br />When you sort an Array (or anything Enumerable), you can override the <code><=></code> method of the Object class to provide your own custom sorting code. (Just like the equals() method in Java.) The <code><=></code> method returns -1, 0, or 1 to indicate whether the instance is smaller, equal, or greater than the other object. Here's the method I added to the Selection model to tell it how to sort:<br /><code><br /> def <=>(o) <br /> # Compare album name<br /> album_name_cmp = self.album.name <=> o.album.name<br /> return album_name_cmp unless album_name_cmp == 0<br /> <br /> # Compare user last name<br /> user_ln_cmp = self.user.last_name <=> o.user.last_name<br /> return user_ln_cmp unless user_ln_cmp == 0<br /> <br /> # Compare user first name<br /> user_fn_cmp = self.user.first_name <=> o.user.first_name<br /> return user_fn_cmp unless user_fn_cmp == 0<br /> <br /> # Otherwise, compare IDs<br /> return self.id <=> o.id<br /> end</code><br /><br />Then, just change the block slightly to use the sorted version:<br /><code><br />@user.selections.sort.each do |selection|<br /> # ...<br />end</code><br /><br />If the model you're trying to sort is not already Comparable, you should add the line <code>include Comparable</code> to include the Comparable mixin. ActiveRecord objects are already Comparable. <br /><br />Have fun writing your own custom sorting rules in Ruby! Leave a comment if you need more details.<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com">Barefoot</a>Unknownnoreply@blogger.com6tag:blogger.com,1999:blog-27393356.post-1161982700015664802006-10-27T13:57:00.000-07:002007-04-28T13:37:24.111-07:00Popups a Spider Can FollowWhile talking with our team about SEO best practices, we discovered that the way <a href="http://www.rubyonrails.org" alt="Ruby on Rails">Rails</a> handles popups makes them not only more user-friendly (like allowing the ability to open the popup in a new full window or in a tab), but also allows a search engine spider to see the link and follow it to index the content of the popup.<br /><br />Here is an example of what you may be used to seeing from a <a href="http://en.wikipedia.org/wiki/WYSIWYG" title="What does WYSIWYG mean?">WYSIWYG</a> editor when popping a new window:<br /><code><br /> <script type="text/JavaScript"><br /> <!--<br /> function MM_openBrWindow(theURL,winName,features) { //v2.0<br /> window.open(theURL,winName,features);<br /> }<br /> //--><br /> </script><br /> <a href="#"><img src="path/to/a.gif" width="124" height="45" border="0" onClick="MM_openBrWindow('path/to/page','','width=900,height=570')"></a><br /></code><br />And this is how we are popping new windows (with the help of Rails):<br /><code><br /><a href="/path/to/page" onclick="window.open(this.href,'event_detail','height=450,width=700');return false;"><br /></code><br /><br />Since the HREF is intact, spiders are able to follow the links. Easy, peasy.<br /><br />Bobby Uhlenbrock, Application Developer, <a href="http://www.thinkbarefoot.com">Barefoot</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-1155226581446996112006-08-10T09:16:00.000-07:002007-04-28T13:37:05.543-07:00Flash sortOn coolness ...It's always fun to discover nice little surprises in function lists. I had such an experience when I recently found the <code>sortOn()</code> function within Flash's Array class.<br /><br />I come from a Java background, and Java's Collections API includes very sophisticated hooks to sort objects in any number of ways. I didn't expect to find that in ActionScript. So, when I had a list of objects that I needed to sort, I was pleasantly surprised to find <code>sortOn()</code>, which sorts an Array of objects.<br /><br />I frequently create value objects to represent things in Flash applications. In one application, I'm placing a number of animals on the stage. The environment is kinda 3D, so animals that have a lower Y axis value are farther away in the distance, while higher Y values are closer to the viewer. The Y axis value is randomly determined, so the order is not pre-determined.<br /><br />Here's a simplified version of the Animal value object with only the properties relevant to this post:<br /><code><br />class Animal <br />{<br /> var initX:Number;<br /> var initY:Number;<br /> var initScale:Number;<br /> var animalType:String;<br /> var animalName:String;<br /> // more props<br /><br /> function init():Void <br /> { <br /> // Init stuff ...<br /> }<br />}<br /></code><br />The application reads user-defined animals from the server, and randomly places these animals on the stage. I keep all the Animal objects in an array for later use.<br /><br />So, I knew that in Java I could sort these Animal objects by implementing the Comparable interface. I was pretty sure I couldn't write a Comparator in ActionScript. But, I decided to check out the Array object just in case.<br /><br />Then, I found the <a href="http://livedocs.macromedia.com/flash/8/main/00001916.html" target="_blank">sortOn() function</a>. Its whole purpose is to let you sort arrays of objects. It has several flavors which you can read about at the link above, but the one relevant to this lets you pass in one or more fields (object properties), and an optional sort type parameter. <br /><br />Here's an example:<br /><code><br />var animals:Array = loadAnimals();<br />animals.sortOn(["initY"], Array.NUMERIC);<br /></code><br />Since initY is a property of every object in the animals array, the <code>sortOn()</code> function sorts the objects on that field. By default, <code>sortOn()</code> sorts in ASCII order, which is fine for strings, but bad for numbers since 100 would be sorted before 99. So, ActionScript offers options like the Array.NUMERIC constant shown above, which tells <code>sortOn()</code> to sort the properties as numbers.<br /><br />So, thanks ActionScript team for putting that in there -- it was a nice surprise!<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com">Barefoot</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-1152674229468889132006-07-11T19:52:00.000-07:002007-04-27T18:52:11.930-07:00Automating Rails deployment without CapistranoCertainly Capostrano is the most elegant way to deploy your Rails apps, but here's an easy way to automate Rails deployment using just rync and rake. And the best part is that it borrows from a <a href="http://barefootdevelopment.blogspot.com/2006/05/public-key-ssh-authentication.html">previous blog entry</a>.<br /><br />This first step is optional, but boy will it make your life easier. Start by setting up public key authentication between your local development machine and your server. <a href="http://barefootdevelopment.blogspot.com/2006/05/public-key-ssh-authentication.html">We've covered this ground before</a>, so just make sure you're doing it for the account on your server that you'd normally use to FTP to your Rails application.<br /><br />The second, and final, step: using rync with rake. To see full coverge of this topic (or how to do it if you're developing on a Windows machine, <a href="http://wiki.rubyonrails.com/rails/pages/HowtoUseRsyncToDeployYourApplication">see the HOWTO</a>.<br /><br />Add the following block to the Rakefile in your local application directory:<br /><code><br />desc "Deploy basic application directories"<br />task :deploy => :environment do<br />dirs = %w{ app lib test public config}<br />onserver = "login@remotehost:/home/rails-app-directory/"<br />dirs.each do | dir|<br /> `rsync -avz -e ssh "#{RAILS_ROOT}/#{dir}" "#{onserver}" --exclude ".svn"`<br />end<br />end<br /></code><br />You'll need to change the onserver line to use your actual username and hostname. Then at the command line, change to your Rails application directory and run:<br /><code><br />$ rake deploy<br /></code><br />That's it. Enoy your new found productivity.<br /><br />Sean Brown, Partner, Technology at <a href="http://www.thinkbarefoot.com">Barefoot</a>Sean Brownhttp://www.blogger.com/profile/10018498282709787995noreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-1152203348150004032006-07-06T09:28:00.000-07:002007-11-27T12:50:16.068-08:00Concerning Media Components and Cue PointsOn a recent Flash project (version 7) with talking characters, it became necessary to start and stop animations at specific points during the playback of a sound file. We decided that the most efficient way to manage the calls was via CuePoints which are available in MX 2004 Pro through the Media components. <br /><br />I placed an instance of the MediaDisplay component on the stage and initialized it thusly:<br /><br /><code><pre><br />var myMedia = _root.my_media_component;<br />myMedia.autoPlay = false;<br /><br />// Load some sound into the component<br />// which has been preloaded<br />myMedia.setMedia("some.mp3", MP3);<br /><br />// Add CuePoints to the component<br />myMedia.addCuePoint("start", 7);<br />myMedia.addCuePoint("stop", 19);<br /><br />// Create a Cue Point Listener object<br />var cuePointListener:Object = new Object();<br /><br />// Call this method when a CuePoint is "heard"<br />cuePointListener.cuePoint = function(eventObject):Void {<br /> // call the function with the CuePoint's name<br /> handleCuePoint(eventObject.target.mostRecentCuePointName);<br />}<br /><br />// Call this method when the sound is complete<br />cuePointListener.complete = function(eventObject):Void {<br /> trace("I'm all done.");<br /> // do some stuff<br />}<br /><br />// Associate/attach the listers to the component<br />myMedia.addEventListener("cuePoint",cuePointListener);<br />myMedia.addEventListener("complete", cuePointListener);<br /><br />// Method to handle each CuePoint<br />// @param myCue: The name of the "heard" CuePoint<br />function handleCuePoint(myCue:String):Void {<br /> switch (myCue) {<br /> case "start" :<br /> trace("Start me up.");<br /> // start some stuff<br /> break;<br /> case "stop" :<br /> trace("Stop it, now.");<br /> // stop some stuff<br /> break;<br /> }<br />}<br /><br />// Now that everything is ready to go, let's play!<br />myMedia.play();<br />trace("The beginning.");<br /></pre></code><br /><br />Okay, that was pretty simple -- the sound is playing great and the CuePoints are called in time. But wait, what was that? The sound finished and my CuePoints just fired again. Let's take a look at the output:<br /><br /><code><br />The beginning<br />Start me up.<br />Stop it, now.<br />Start me up.<br />Stop it, now.<br />I'm all done.<br /></code><br /><br />So, something is amiss between the completion of the sound and our listener "hearing" the <code>complete</code>. If we trace <code>myMedia.playheadTime</code> in the complete function, it returns <code>0</code>. The Media component is automatically <strong>rewinding</strong> the sound when it finishes but <em>before</em> triggering the complete listener. Blockbuster would be proud. This wouldn't be so bad, but the act of rewinding fires the CuePoints as the sound scrubs back to 0. Luckily, the solution is simple. After a CuePoint is heard, just remove it:<br /><br /><code><pre><br /> // Call this method when a CuePoint is "heard"<br /> cuePointListener.cuePoint = function(eventObject):Void {<br /> // call the function with the CuePoint's name<br /> handleCuePoint(eventObject.target.mostRecentCuePointName);<br /> // now that the function above has been called, remove the CuePoint<br /> myMedia.removeCuePoint(mediaStreamer.getCuePoint(eventObject.target.mostRecentCuePointName));<br /> }<br /></pre></code><br /><br />Not much to it, but the behavior of the MediaDisplay component is so odd that I just had to share.<br /><br />Bobby Uhlenbrock, Application Developer, <a href="http://www.thinkbarefoot.com">Barefoot</a>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-27393356.post-1151688150259907972006-06-30T10:13:00.000-07:002006-06-30T10:30:53.220-07:00Custom Class Casting in FlashWhile developing a Flash 8 project using ActionScript 2 classes, I recently created a Tile class that extended MovieClip. The Tile is a custom scrolling background element that tiles itself for a seamless, endless horizontal scrolling. <br /><br />I created a couple variables, typed as Tile, in my AppState class, the class I use to keep the current state of the Flash application. They are like so:<br /><code><br />// Background movie clip tile references<br />var tile1:Tile = null;<br />var tile2:Tile = null;<br /></code><br />Now, since we're not yet using ActionScript 3, the only way to place new Tile instances onto the stage is with a call to <code>attachMovie()</code>. However, the following call won't compile in AS2:<br /><code><br />app.tile1 = _root.attachMovie(bgndMcName, "bgnd1_mc", app.depth++);<br /></code><br />Why does it fail? Well, the compiler says this:<br /><code><br />Type mismatch in assignment statement: found MovieClip where com.barefoot.ui.comp.Tile is required.<br /></code><br />So, I thought to myself, I wonder if you can cast objects like you can with the <code>String()</code> and <code>Number()</code> functions? It turns out you can! This works:<br /><code><br />app.tile1 = Tile(_root.attachMovie(bgndMcName, "bgnd1_mc", app.depth++));<br /></code><br />I'm looking forward to ActionScript 3, where we will be able to instantiate objects to be placed on the stage like you can for any other object:<br /><code><br />app.tile1 = new Tile();<br />addChild(app.tile1);<br /></code><br />But until then, I'll use this casting method. It's not rocket science, but I thought it was cool nonetheless.<br /><br />Doug Smith, Senior Developer, <a href="http://www.thinkbarefoot.com" target="_blank">Barefoot</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-27393356.post-1150516403751410622006-06-16T20:52:00.000-07:002006-06-16T20:53:23.906-07:00HTML e-mailsOK, so we don't have all of the best ideas. Here's a great article with some do's and don'ts for HTML e-mail, which e-mail clients support CSS, and techniques to handle image blocking.<br /><br /><a href="http://www.thinkvitamin.com/features/design/html-emails">HTML Emails - Taming the Beast</a> by David GreinerSean Brownhttp://www.blogger.com/profile/10018498282709787995noreply@blogger.com0