<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Ruby developer for Heroku. Climbs rocks in Austin &amp; teaches Rails classes at the University of Texas</description><title>Schneems</title><generator>Tumblr (3.0; @schneems)</generator><link>http://schneems.com/</link><item><title>Cedar goes General Availability on Heroku</title><description>&lt;a href="http://blog.heroku.com/archives/2012/5/24/cedar_goes_ga/"&gt;Cedar goes General Availability on Heroku&lt;/a&gt;</description><link>http://schneems.com/post/23688052638</link><guid>http://schneems.com/post/23688052638</guid><pubDate>Thu, 24 May 2012 16:39:04 -0400</pubDate></item><item><title>The HourSchool Blog: How can companies ask better questions?</title><description>&lt;a href="http://blog.hourschool.com/post/23634405396/how-can-companies-ask-better-questions"&gt;The HourSchool Blog: How can companies ask better questions?&lt;/a&gt;: &lt;p&gt;&lt;a href="http://blog.hourschool.com/post/23634405396/how-can-companies-ask-better-questions" class="tumblr_blog"&gt;hourschool&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One core pillar of &lt;a href="http://en.wikipedia.org/wiki/User-centered_design"&gt;human-centered design&lt;/a&gt; is the idea of seeing the world through other people’s eyes, gaining empathy for their worldview, and meeting them where they are. Many companies are getting on board with the idea of putting the user or customer at the center of their business and design…&lt;/p&gt;&lt;/blockquote&gt;</description><link>http://schneems.com/post/23677215057</link><guid>http://schneems.com/post/23677215057</guid><pubDate>Thu, 24 May 2012 13:00:30 -0400</pubDate></item><item><title>Sextant: A Gem to Help you Find your Routes</title><description>&lt;p&gt;News flash, writing a Rails app without knowing your routes is pretty much impossible, and this just in &lt;code&gt;$ rake routes&lt;/code&gt; takes &lt;strong&gt;forever&lt;/strong&gt; [1] to run. So how can we build a Rails site with a minimum of time and a maximum of awesome? You can use the recently released &lt;a href="http://github.com/schneems/sextant"&gt;Sextant&lt;/a&gt;  Gem [2]  to generate routes in your Rails app.&lt;/p&gt;

&lt;p&gt;As it turns out if you benchmark the code in  &lt;code&gt;$ rake routes&lt;/code&gt; it is reasonably fast, the slow part comes from having to initialize the app environment every time it&amp;#8217;s run. So lets take some of that code and put it in your already initialized web app. What you get is a whole lot of awesome in a tiny package. Next time you want to see your routes, just visit &lt;code&gt;/rails/routes&lt;/code&gt; in your browser if you&amp;#8217;ve got sextant installed, and you&amp;#8217;ll see your routes in no time.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_m42lwz86aB1qevexf.png" alt="Sextant Output"/&gt;&lt;/p&gt;

&lt;p&gt;To install add this to your Gemfile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem 'sextant' 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then run &lt;code&gt;bundle install&lt;/code&gt; and you&amp;#8217;re good to go. Start your server and visit &lt;code&gt;/rails/routes&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This in-browser information is local only. If you like &lt;a href="http://github.com/schneems/sextant"&gt;Sextant&lt;/a&gt; tell your friends, and tell me &lt;a href="http://twitter.com/schneems"&gt;@schneems&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;[1] Forever is measured in seconds&lt;/li&gt;
&lt;li&gt;[2] &lt;a href="http://en.wikipedia.org/wiki/Sextant"&gt;Sextant&lt;/a&gt; is an old device for navigating. It can help you find your route on a long journey.&lt;/li&gt;
&lt;/ul&gt;</description><link>http://schneems.com/post/23543653526</link><guid>http://schneems.com/post/23543653526</guid><pubDate>Tue, 22 May 2012 09:46:50 -0400</pubDate><category>rails</category><category>ruby on rails</category><category>routes</category><category>sextant</category></item><item><title>OAuth: A Tale of Two Servers</title><description>&lt;p&gt;What exactly is OAuth, and how can you use it to access data on sites like Facebook? This introductory video explains the basic flow behind OAuth 2 and how OAuth Clients are implemented.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/tFYrq3d54Dc" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;If you&amp;#8217;re using OAuth, you&amp;#8217;ll likely want to use a pre-built client library to help with the process. For Ruby you&amp;#8217;ll likely want to use &lt;a href="https://github.com/intridea/omniauth"&gt;Omniauth&lt;/a&gt; or &lt;a href="https://github.com/arsduo/koala"&gt;Koala&lt;/a&gt;, though understanding the process is still important.&lt;/p&gt;

&lt;p&gt;This is being posted from Singapore, where I&amp;#8217;m about to give a talk at &lt;a href="http://reddotrubyconf.com/"&gt;Red Dot Ruby Conf&lt;/a&gt;. Have fun learning about OAuth, i&amp;#8217;ll post slides from my talk as soon as they&amp;#8217;re ready. Wish me luck!&lt;/p&gt;</description><link>http://schneems.com/post/23164666348</link><guid>http://schneems.com/post/23164666348</guid><pubDate>Wed, 16 May 2012 09:55:17 -0400</pubDate><category>oauth</category><category>oauth 2</category><category>client</category><category>provider</category></item><item><title>Deploy &amp; Edit a Facebook App in 5 Min</title><description>&lt;p&gt;Ever want to write an App that uses the Facebook graph? You could be the next Zynga,  Foursquare, or Causes; but first you need to create your app. This quick demo shows getting started by &lt;a href="https://devcenter.heroku.com/articles/facebook"&gt;generating an app through Facebook&lt;/a&gt; and then editing it. If you&amp;#8217;ve already got a working web app it&amp;#8217;s simple to add Facebook functionality to it, though we&amp;#8217;ll save that for another day. What are you waiting for, five minutes from now you could have your very own live Facebook App!&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/HgiONvm5JB0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;If you&amp;#8217;re interested in more information on Facebook Apps let me know &lt;a href="http://twitter.com/schneems"&gt;@schneems&lt;/a&gt;&lt;/p&gt;</description><link>http://schneems.com/post/22778757689</link><guid>http://schneems.com/post/22778757689</guid><pubDate>Thu, 10 May 2012 09:53:32 -0400</pubDate><category>Facebook</category><category>app</category><category>deploy</category><category>graph</category></item><item><title>Multiple Ruby Version Support on Heroku</title><description>&lt;p&gt;Starting today &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; will allow you to &lt;a href="https://devcenter.heroku.com/articles/ruby-support#ruby_versions"&gt;specify a version of Ruby&lt;/a&gt; in your production app. As one of the most requested features we have been asked for time and time again, we&amp;#8217;re happy to announce that it&amp;#8217;s now live. To get started you&amp;#8217;ll want to update your version of Bundler locally to version 1.2.0, or above.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;This is a re-post of an Article I wrote for the &lt;a href="http://blog.heroku.com/archives/2012/5/9/multiple_ruby_version_support_on_heroku/"&gt;Heroku Blog&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre&gt;&lt;code&gt;$ gem install bundler --pre
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then you&amp;#8217;ll want to specify the version of Ruby you want to use in your application inside of your Gemfile. For example if we wanted to use Ruby 1.9.3 in our production application you would want to include &lt;code&gt;ruby '1.9.3'&lt;/code&gt; inside of your Gemfile. In a rails Gemfile it might look something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;source 'http://rubygems.org'

ruby '1.9.3'
gem  'rails', '3.2.3'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once you&amp;#8217;ve added &lt;code&gt;ruby&lt;/code&gt; to your Gemfile, commit it to git&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git add Gemfile
$ git commit -m 'use Ruby 1.9.3 on Heroku'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then you&amp;#8217;ll want to deploy your app&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git push heroku master
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once your application is done deploying you will be able see the version of Ruby you are using is 1.9.3.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ heroku run bash
$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&amp;#8217;s that simple!&lt;/p&gt;

&lt;p&gt;With this new feature you can also use this process to call a specific version of Ruby other than 1.9.3, however, if it is not installed on the Heroku system you&amp;#8217;ll receive an error.&lt;/p&gt;

&lt;h2&gt;Production and Development Parity&lt;/h2&gt;

&lt;p&gt;At Heroku, we strongly believe that there should always be a strong &lt;a href="http://www.12factor.net/dev-prod-parity"&gt;parity between development and production environments&lt;/a&gt; in order to minimize any surprises. When the tooling of your development environment most closely matches your production environment, there is far less room for error. Another good example of keeping parity between dev and production environments would be running &lt;a href="http://postgresapp.com/"&gt;PostgreSQL locally&lt;/a&gt; in the development environment instead of SQLite since you&amp;#8217;re production system is running &lt;a href="https://postgres.heroku.com/"&gt;Postgres on Heroku&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While there are other reasons you may want to use different versions of Ruby in certain scenarios including performance issues or to access version-specific Ruby features, developers should overall strive to use the same tools and versions of software for development as are used for production.&lt;/p&gt;

&lt;h2&gt;Patch Versions&lt;/h2&gt;

&lt;p&gt;While you can now specify the version of Ruby you would like your web application to use, at this time we do not support that ability to request a specific patch version to be called, such as Ruby 1.9.2-p290. Ruby patches often include important bug and security fixes and are extremely compatible. While you can specify the version of Ruby you wish to use, Heroku will provide the most secure patch level of that version.&lt;/p&gt;

&lt;h2&gt;Debugging&lt;/h2&gt;

&lt;p&gt;If you&amp;#8217;ve followed all the steps above and you&amp;#8217;re still seeing a different version of Ruby than you need, please recheck your path.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ heroku config
PATH     =&amp;gt; vendor/bundle/ruby/1.9.1/bin:/usr/local/bin:/usr/bin:/bin:bin
RACK_ENV =&amp;gt; production
# ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You need to ensure that &lt;code&gt;bin&lt;/code&gt; is in front of your path so you could change the above to&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ heroku config:add PATH=bin:vendor/bundle/ruby/1.9.1/bin:/usr/local/bin:/usr/bin:/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As a tip, It is a good idea to use the free &lt;a href="https://devcenter.heroku.com/articles/releases"&gt;releases&lt;/a&gt; feature whenever you are modifying your &amp;#8216;config&amp;#8217; in case you need to roll back to a previous version of Ruby.&lt;/p&gt;

&lt;h2&gt;Thanks&lt;/h2&gt;

&lt;p&gt;Thanks to &lt;a href="http://twitter.com/hone02"&gt;Terence Lee&lt;/a&gt; Heroku Ruby team member and bundler maintainer for the additional support of ruby versions to the &lt;a href="https://github.com/heroku/heroku-buildpack-ruby"&gt;Heroku Ruby Buildpack&lt;/a&gt; and orchestrated the release of Bundler 1.2.0. Also thanks to &lt;a href="https://twitter.com/#!/wycats"&gt;Yehuda Katz&lt;/a&gt; and the entire Bundler team for helping get this release out the door.&lt;/p&gt;

&lt;p&gt;Give this feature a try and let me know what you think &lt;a href="http://twitter.com/schneems"&gt;@schneems&lt;/a&gt;!&lt;/p&gt;</description><link>http://schneems.com/post/22718246618</link><guid>http://schneems.com/post/22718246618</guid><pubDate>Wed, 09 May 2012 11:39:42 -0400</pubDate><category>ruby</category><category>heroku</category><category>versions</category></item><item><title>Wicked Gem Featured on Railscasts</title><description>&lt;a href="http://railscasts.com/episodes/346-wizard-forms-with-wicked"&gt;Wicked Gem Featured on Railscasts&lt;/a&gt;: &lt;p&gt;&lt;a href="http://github.com/schneems/wicked"&gt;Wicked&lt;/a&gt; was featured by &lt;a href="http://twitter.com/rbates"&gt;Ryan Bates&lt;/a&gt; on Railscasts this past week. I’ve learned quite a bit over the years from Railscasts, so it’s a great honor to have my work featured there.&lt;/p&gt;</description><link>http://schneems.com/post/22452416393</link><guid>http://schneems.com/post/22452416393</guid><pubDate>Sat, 05 May 2012 12:24:34 -0400</pubDate><category>railscasts</category><category>ruby</category><category>gem</category></item><item><title>Legacy Concerns in Rails</title><description>&lt;p&gt;The cats out of the bag, Ruby isn&amp;#8217;t immune to legacy code problems. Just because your app is written in a hip, fun, and dynamic language doesn&amp;#8217;t mean that your codebase can&amp;#8217;t stagnate, bloat, and quickly turn into an unmaintainable ball of mud. Before &lt;a href="http://gowalla.com"&gt;Gowalla&lt;/a&gt; was purchased by Facebook, the Rails code base stood at close to seven thousand files, with the largest model clocking in at around 3,500 lines of code. While we were somewhat unique, being originally written in Merb and then ported to Rails, applications of this size aren&amp;#8217;t all that uncommon. If you&amp;#8217;ve got a large app there are a number of things you can do make your situation better, one of the simplest with the greatest impact is splitting up models into concerns.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;If you&amp;#8217;re not familiar with concerns, you can read up about them at &lt;a href="http://schneems.com/post/21380060358/concerned-about-code-reuse"&gt;Concerned about Code Reuse?&lt;/a&gt;. Go ahead, we&amp;#8217;ll wait.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Rails has long advocated a thin controller, fat model approach to development, which works great early but can lead to a model obesity epidemic. If we split out our models into different concerns we can group related code, and even make re-using code between projects easier. Best of all, if you start early on it&amp;#8217;s a pretty painless process.&lt;/p&gt;

&lt;h2&gt;The User Model&lt;/h2&gt;

&lt;p&gt;There&amp;#8217;s only two businesses that refer to their customers as &amp;#8216;users&amp;#8217; and software is one of them. It&amp;#8217;s also bound to be one of the largest models in your app since so much will likely need to be connected to a user. It&amp;#8217;s a good place to start splitting up a model into a concern. Lets say that we want to add some methods on our user object so they can access Facebook information we can start by creating a new file &lt;code&gt;app/models/user/facebook_methods.rb&lt;/code&gt; (you&amp;#8217;ll need to create the &lt;code&gt;user&lt;/code&gt; folder).&lt;/p&gt;

&lt;p&gt;This file is where we&amp;#8217;ll group all of our methods related to Facebook. For this example i&amp;#8217;ll be using the &lt;a href="https://github.com/arsduo/koala"&gt;Koala&lt;/a&gt; Facebook gem, and we assume our user model has a &lt;code&gt;facebook_token&lt;/code&gt; attribute persisted to the database.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module User::FacebookMethods
  extend ActiveSupport::Concern

  def facebook_graph
    @facebook_graph ||= Koala::Facebook::API.new(facebook_token)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Not a bad start, now we want to add this ability to our user model, open up &lt;code&gt;app/models/user.rb&lt;/code&gt; and add our concern.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class User &amp;lt; ActiveRecord::Base
  include User::FacebookMethods
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Great! Now we can construct our Facebook graph object straight from our user.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user = User.where("facebook_token is not null").first
user.facebook_graph
# =&amp;gt; &amp;lt;# Koala #...

user.facebook_graph.get_connections("me", "friends")
# =&amp;gt; {52930 =&amp;gt; 'Terence Lee',  12345 =&amp;gt; "Ruby Ku" #...
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;That Was Easy, but&amp;#8230;&lt;/h2&gt;

&lt;p&gt;What did that buy us? First we&amp;#8217;ve got an obvious place to store our code. Want to write a method that pulls out all of a user&amp;#8217;s Facebook friends? Put it in the &lt;code&gt;facebook_methods.rb&lt;/code&gt; file. If you forget the name of that method, check out your Facebook methods. If you need the &lt;code&gt;facebook_graph&lt;/code&gt; method, bet you money it&amp;#8217;s in that concern. If all related code is in one place its a lot easier to scan visually and to search for keywords.&lt;/p&gt;

&lt;h2&gt;Won&amp;#8217;t This add More Code to the Codebase?&lt;/h2&gt;

&lt;p&gt;In the example above, we added 4 extra lines of code to save us 3 measly lines in our &lt;code&gt;user.rb&lt;/code&gt; file. While this isn&amp;#8217;t ideal for such a small concern, as it grows in size its much easier to keep track of the components, which in turn helps keep your code small and manageable. It also nudges developers to create unit tests for those specific concerns. This sounds minor, but when you get to a file with 3500 lines of code, you start duplicating functionality that you didn&amp;#8217;t know existed. Either it was added by another developer, or you forgot you added it months ago. Keeping everything in its place helps keep your code sane.&lt;/p&gt;

&lt;h2&gt;How Should I Break up My Code&lt;/h2&gt;

&lt;p&gt;Often times I like breaking out my concerns based on knowledge of third party services. For example I broke out a concern for all the Facebook methods above. I use &lt;a href="https://addons.heroku.com/websolr"&gt;websolr&lt;/a&gt; and I like having a separate concern for all the search related methods. Recently I played around with the &lt;a href="https://github.com/seejohnrun/ice_cube"&gt;Ice Cube&lt;/a&gt; gem which is a library for creating an iCal formatted recurring date syntax. I split that code out into a concern, not because it was touching another service, but because I might want to re-use that code in another model some day, and it&amp;#8217;s easier to break out the code now. There are no hard and fast rules, just don&amp;#8217;t go overboard and have 100 concerns for every model with 3 lines of code in each of them, and on the flip side don&amp;#8217;t have one concern with &lt;strong&gt;everything&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Just think of the different areas of &amp;#8216;concern&amp;#8217;, that your code covers. Get it?&lt;/p&gt;

&lt;h2&gt;Legacy Code&lt;/h2&gt;

&lt;p&gt;While building out the final version of the Gowalla service we managed to promote Redis and Cassandra to first class data storage citizens, completely re-write all web controllers, and split out a brand new api into a separate set of controllers (more on this in a later date). It was a ton of work, we were completely changing the way our service worked and creating new paradigms such as a &amp;#8220;Stories&amp;#8221; where multiple users could check each other in and at the same time, we still had to support a ton of 3rd party client applications using the old API.&lt;/p&gt;

&lt;p&gt;So, how did concerns help? We used concerns to isolate new code and new services. It also helped us to add more &amp;amp; better unit testing by focusing different spec files on different areas so &lt;code&gt;models/users/following_methods.rb&lt;/code&gt; would be tested by &lt;code&gt;spec/models/users/following_methods_spec.rb&lt;/code&gt;. Most of the developers used some form of automatic test runner such as &lt;a href="https://github.com/guard/guard-rspec"&gt;Guard Rspec&lt;/a&gt;, and it is nice being able to run only the unit tests associated with the concern you&amp;#8217;re writing without having to run &lt;strong&gt;all&lt;/strong&gt; the unit tests for that model.&lt;/p&gt;

&lt;p&gt;Bonus! Ever find a method that you were &lt;strong&gt;pretty sure&lt;/strong&gt; wasn&amp;#8217;t being called by anything. Maybe it&amp;#8217;s in a model that wasn&amp;#8217;t exactly 100% tested. You could try making a concern for methods of questionable value. In a month if it&amp;#8217;s still there, delete the sucker, deploy to staging, validate and commit to master.&lt;/p&gt;

&lt;h2&gt;Shared Concerns&lt;/h2&gt;

&lt;p&gt;If you have code that needs to be shared by multiple models in your project, you can keep this in your &lt;code&gt;lib&lt;/code&gt; folder. I actually like to have a &lt;code&gt;concerns&lt;/code&gt; folder like &lt;code&gt;lib/concerns/models/duplicate_code.rb&lt;/code&gt;. When we added Cassandra to the Gowalla project we needed a way to get our Postgres models to play nice. Thats when &lt;a href="https://twitter.com/#!/therealadam"&gt;Adam Keys&lt;/a&gt; and &lt;a href="https://twitter.com/#!/bdoughty"&gt;Bill Doughty&lt;/a&gt; pulled out common logic and put it into a concern using another library called Chronologic. &lt;code&gt;lib/concerns/models/chronologify.rb&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then any time you wanted this shared code into your model, you just had to include it.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# models/checkin.rb
class Checkin &amp;lt; ActiveRecord::Base
  include Concerns::Models::Chronologify
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don&amp;#8217;t forget to add the concern folder in your lib to your search path.&lt;/p&gt;

&lt;h2&gt;Wrap it Up&lt;/h2&gt;

&lt;p&gt;There have been tomes written about dealing with legacy code in software, I&amp;#8217;ve been recently recommended &lt;a href="http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052"&gt;Working Effectively with Legacy Code &lt;/a&gt;. Using concerns won&amp;#8217;t be a magic bullet, but it will help keep your code nicely organized. Even if you&amp;#8217;re dealing with a pristine new app, concerns are one way to help it stay that way. Give concerns a try and let me know if you have a good or bad experience &lt;a href="http://twitter.com/schneems"&gt;@schneems&lt;/a&gt;.&lt;/p&gt;</description><link>http://schneems.com/post/22192005006</link><guid>http://schneems.com/post/22192005006</guid><pubDate>Tue, 01 May 2012 10:01:00 -0400</pubDate><category>ruby</category><category>rails</category><category>concern</category><category>legacy</category><category>clean code</category></item><item><title>Building an iOS Photo-sharing and Geolocation Mobile Client and API with Rails and Heroku</title><description>&lt;a href="https://devcenter.heroku.com/articles/ios-photo-sharing-geo-location-service"&gt;Building an iOS Photo-sharing and Geolocation Mobile Client and API with Rails and Heroku&lt;/a&gt;: &lt;p&gt;My good friend &lt;a href="http://twitter.com/mattt"&gt;@mattt&lt;/a&gt; just released this great tutorial for creating an iOS Photo-Sharing app on Rails. You should hop, skip, or jump on over to the article now. What are you waiting for?&lt;/p&gt;</description><link>http://schneems.com/post/21457295221</link><guid>http://schneems.com/post/21457295221</guid><pubDate>Fri, 20 Apr 2012 18:52:08 -0400</pubDate><category>rails</category><category>heroku</category><category>iOS</category><category>tutorial</category></item><item><title>Concerned about Code Reuse?</title><description>&lt;p&gt;Right out of the gate, Ruby gives us some powerful ways to re-use instance and class methods without relying on inheritance. Modules in Ruby can be used to mixin methods to classes fairly easily. For example, we can add new instance methods using &lt;code&gt;include&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module DogFort
  def call_dog
    puts "this is dog!"
  end
end

class Dog
  include DogFort
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we&amp;#8217;re able to call any methods defined in our &lt;code&gt;DogFort&lt;/code&gt; Module as if they were simply slipped into (included) into our &lt;code&gt;Dog&lt;/code&gt; class.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;dog_instance = Dog.new
dog_instance.call_dog
# =&amp;gt; "this is dog!"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Using Modules a fairly easy way to re-use methods, if you want you can &lt;code&gt;extend&lt;/code&gt; a Module to add methods to a class directly.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module DogFort
  def board_the_doors
    puts "no catz allowed"
  end
end

class Dog
  extend DogFort
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now if we were to call &lt;code&gt;Dog.new.board_the_doors&lt;/code&gt; we would get an error, since we&amp;#8217;ve added it as a class method instead.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Dog.board_the_doors
# =&amp;gt; "no catz allowed"

Dog.class
# =&amp;gt; Class
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sweet! Though what if you wanted to add an instance method and a class method to a class. We could have two Modules, one to be included and one to be extended, wouldn&amp;#8217;t be to hard but it would be nice if we only had to use one include statement, especially if the two Modules are related. So is it possible to add instance and class methods with only one include statement? Of course&amp;#8230;&lt;/p&gt;

&lt;h2&gt;Enter Concerns&lt;/h2&gt;

&lt;p&gt;A concern is a Module that adds instance methods (like  &lt;code&gt;Dog.new.call_dog&lt;/code&gt;) and class methods (like &lt;code&gt;Dog.board_the_doars&lt;/code&gt;) to a class. If you&amp;#8217;ve poked around the Rails source code you&amp;#8217;ll see this everywhere. It&amp;#8217;s so common that Active Support added a helper Module to create concerns. To use it require ActiveSupport and then &lt;code&gt;extend ActiveSupport::Concern&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'active_support/concern'

module DogFort
  extend ActiveSupport::Concern
  # ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now any methods you put into this Module will be instance methods (methods on a new instance of a class &lt;code&gt;Dog.new&lt;/code&gt;) and any methods that you put into a Module named &lt;code&gt;ClassMethods&lt;/code&gt; will be added on to the class directly (such as &lt;code&gt;Dog&lt;/code&gt;).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'active_support/concern'

module YoDawgFort
  extend ActiveSupport::Concern

  def call_dawg
    puts "yo dawg, this is dawg!"
  end


  # Anything in ClassMethods becomes a class method
  module ClassMethods
    def board_the_doors
      puts "yo dawg, no catz allowed"
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So now when we add this new Module to a class, we&amp;#8217;ll get instance and class methods&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class YoDawg
  include YoDawgFort
end

YoDawg.board_the_doars
# =&amp;gt; "yo dawg, no catz allowed"

yodawg_instance = YoDawg.new
yodawg_instance.call_dawg
# =&amp;gt; "yo dawg, this is dawg!"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Pretty cool huh?&lt;/p&gt;

&lt;h2&gt;Included&lt;/h2&gt;

&lt;p&gt;That&amp;#8217;s not all, Active Support also gives us a special method called included that we can use to call methods during include time. If you add &lt;code&gt;included&lt;/code&gt; to your &lt;code&gt;ActiveSupport::Concern&lt;/code&gt; any code in there will be called when it is included&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module DogCatcher
  extend ActiveSupport::Concern

  included do
    if self.is_a? Dog
      puts "gotcha!!"
    else
      puts "you may go"
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So when we include &lt;code&gt;DogCatcher&lt;/code&gt; in a class it&amp;#8217;s included block will be called immediately.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Dog
  include DogCatcher
end
# =&amp;gt; "gotcha!!"

class Cat
  include DogCatcher
end
# =&amp;gt; "you may go"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While this is a contrived example, you can imagine wanting to maybe make a concern for Rails controllers and wanting to add &lt;code&gt;before_filter&lt;/code&gt;&amp;#8217;s to our code. We can do this easily adding the included block.&lt;/p&gt;

&lt;h2&gt;Is this magic?&lt;/h2&gt;

&lt;p&gt;Nope, under the hood we&amp;#8217;re just using good old fashioned Ruby. If you want to learn more about all the fun things you can do with Modules I recommend checking out one of my favorite Ruby books &lt;a href="http://pragprog.com/book/ppmetr/metaprogramming-Ruby"&gt;Metaprogramming Ruby&lt;/a&gt; and Dave Thomas also has a fantastic &lt;a href="http://pragprog.com/screencasts/v-dtRubyom/the-Ruby-object-model-and-metaprogramming"&gt;screencast series&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Gotcha&lt;/h2&gt;

&lt;p&gt;When you&amp;#8217;re writing Modules I guarantee that you&amp;#8217;ll slip up and accidentally try to create a class method using &lt;code&gt;self&lt;/code&gt; or &lt;code&gt;class &amp;lt;&amp;lt; self&lt;/code&gt; but it won&amp;#8217;t work because it&amp;#8217;s now a method on the Module.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module DogFort
  def self.call_dog
    puts "this is dog!"
  end
ene
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the example above the context of &lt;code&gt;self&lt;/code&gt; is actually the Module object &lt;code&gt;DogFort&lt;/code&gt; so when we include it into another class we won&amp;#8217;t see the method.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Wolf
  include DogFort
end

Wolf.call_dog
# NameError: undefined local variable or method `call_dog'

wolf_instance = Wolf.new
wolf_instance.call_dog
# NameError: undefined local variable or method `call_dog'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you want to use that method in this context you will need to call the Module directly&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DogFort.call_dog
# =&amp;gt; "this is dog!"
puts DogFort.class
# =&amp;gt; Module
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Fin&lt;/h2&gt;

&lt;p&gt;That&amp;#8217;s all for today, in my next post I&amp;#8217;m going to show you how to &lt;a href="http://schneems.com/post/22192005006/legacy-concerns-in-rails"&gt;clean up your legacy code base with concerns&lt;/a&gt;. Let me know if you have any questions &lt;a href="http://twitter.com/schneems"&gt;@schneems&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;You may also be interested in &lt;a href="http://www.fakingfantastic.com/2010/09/20/concerning-yourself-with-active-support-concern/"&gt;Concerning Yourself with ActiveSupport::Concern&lt;/a&gt;, &lt;a href="http://weblog.jamisbuck.org/2007/1/17/concerns-in-activerecord"&gt;Concerns in ActiveRecord&lt;/a&gt; and &lt;a href="http://yehudakatz.com/2009/11/12/better-Ruby-idioms/"&gt;Better Ruby Idioms&lt;/a&gt;.&lt;/p&gt;</description><link>http://schneems.com/post/21380060358</link><guid>http://schneems.com/post/21380060358</guid><pubDate>Thu, 19 Apr 2012 09:59:00 -0400</pubDate><category>rails</category><category>ruby</category><category>module</category><category>concern</category><category>concerns</category><category>include</category><category>extend</category><category>metaprogramming</category></item><item><title>Partial Validation of Active Record Objects in Wicked</title><description>&lt;p&gt;This question comes up a lot, people want to have an object, lets call it a &lt;code&gt;Product&lt;/code&gt; that they want to create in several different steps. Let&amp;#8217;s say our product has a few fields &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;price&lt;/code&gt;, and &lt;code&gt;category&lt;/code&gt; and to have a valid product all these fields must be present.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;This is a re-post of a wiki I wrote for &lt;a href="https://github.com/schneems/wicked/wiki/Partial-Validation-of-Active-Record-Objects"&gt;Wicked&lt;/a&gt;.
  While it was written to be used with a wizard, the pattern can be used without it. Enjoy!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;The Problem&lt;/h2&gt;

&lt;p&gt;We want to build an object in several different steps but we can&amp;#8217;t because that object needs validations. Lets take a look at our &lt;code&gt;Product&lt;/code&gt; model.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Product &amp;lt; ActiveRecord::Base
  validates :name, :price, :category, :presence =&amp;gt; true

end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So we have a product that relies on name, price, and category to all be there. Lets take a look at a simple Wizard controller for ProductController.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class ProductController &amp;lt; ApplicationController
  include Wicked::Wizard

  steps :add_name, :add_price, :add_category

  def show
    @product = Product.find(params[:product_id])
    render_wizard
  end


  def update
    @product = Product.find(params[:product_id])
    @product.update_attributes(params[:product])
    render_wizard @product
  end


  def create
    @product = Product.create
    redirect_to wizard_path(steps.first, :product_id =&amp;gt; @product.id)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here the create action won&amp;#8217;t work because our product didn&amp;#8217;t save. OhNo!&lt;/p&gt;

&lt;h2&gt;The Solution&lt;/h2&gt;

&lt;p&gt;The best way to build an object incrementally with validations is to save the state of our product in the database and use conditional validation. To do this we&amp;#8217;re going to add a &lt;code&gt;status&lt;/code&gt; field to our &lt;code&gt;Product&lt;/code&gt; class.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class ProductStatus &amp;lt; ActiveRecord::Migration
  def up
    add_column :products, :status, :string
  end

  def down
    remove_column :product, :status
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we want to add an &lt;code&gt;active&lt;/code&gt; state to our &lt;code&gt;Product&lt;/code&gt; model.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def active?
  status == 'active'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And we can add a conditional validation to our model.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Product &amp;lt; ActiveRecord::Base
  validates :name, :price, :category, :presence =&amp;gt; true, :if =&amp;gt; :active?

  def active?
    status == 'active'
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we can create our &lt;code&gt;Product&lt;/code&gt; and we won&amp;#8217;t have any validation errors, when the time comes that we want to release the product into the wild you&amp;#8217;ll want to remember to change the status of our Product on the last step.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class ProductController &amp;lt; ApplicationController
  include Wicked::Wizard

  steps :add_name, :add_price, :add_category

  def update
    @product = Product.find(params[:product_id])
    params[:product][:status] = 'active' if step == steps.last
    @product.update_attributes(params[:product])
    render_wizard @product
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Great, but&amp;#8230;&lt;/h2&gt;

&lt;p&gt;So that works well, but what if we want to disallow a user to go to the next step unless they&amp;#8217;ve properly set the value before it. We&amp;#8217;ll need to split up or validations to support multiple conditional validations.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Product &amp;lt; ActiveRecord::Base
  validates :name,      :presence =&amp;gt; true, :if =&amp;gt; :active_or_name?
  validates :price,     :presence =&amp;gt; true, :if =&amp;gt; :active_or_price?
  validates :category,  :presence =&amp;gt; true, :if =&amp;gt; :active_or_category?

  def active?
    status == 'active'
  end

  def active_or_name?
    status.include?('name') || active?
  end

  def active_or_price?
    status.include?('price') || active?
  end

  def active_or_category?
    status.include?('category') || active?
  end

end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then in our ProductController Wizard we can set the status to the current step name in in our update.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  def update
    @product = Product.find(params[:product_id])
    params[:product][:status] = step
    params[:product][:status] = 'active' if step == steps.last
    @product.update_attributes(params[:product])
    render_wizard @product
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So on the &lt;code&gt;:add_name&lt;/code&gt; step &lt;code&gt;status.include?('name')&lt;/code&gt; will be &lt;code&gt;true&lt;/code&gt; and our product will not save if it isn&amp;#8217;t present. So in the update action of our controller if &lt;code&gt;@product.save&lt;/code&gt; returns false then the &lt;code&gt;render_wizard @product&lt;/code&gt; will direct the user back to the same step &lt;code&gt;:add_name&lt;/code&gt;. We still set our status to active on the last step since we want all of our validations to run.&lt;/p&gt;

&lt;h2&gt;Wow that&amp;#8217;s cool, but seems like a bunch of work&lt;/h2&gt;

&lt;p&gt;What you&amp;#8217;re trying to do is fairly complicated, we&amp;#8217;re essentially turning our Product model into a state machine, and we&amp;#8217;re building it inside of our wizard which is a state machine. Yo dawg, i heard you like state machines&amp;#8230; This is a very manual process which gives you, the programmer, as much control as you like.&lt;/p&gt;

&lt;h2&gt;Cleaning up&lt;/h2&gt;

&lt;p&gt;If you have conditional validation it can be easy to have incomplete Product&amp;#8217;s laying around in your database, you should set up a sweeper task using something like Cron, or Heroku&amp;#8217;s scheduler to clean up Product&amp;#8217;s that are not complete.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lib/tasks/cleanup.rake&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;namespace :cleanup do
  desc "removes stale and inactive products from the database"
  task :products =&amp;gt; :environment do
    # Find all the products older than yesterday, that are not active yet
    stale_products = Product.where("DATE(created_at) &amp;lt; DATE(?)", Date.yesterday).where("status is not 'active'")

    # delete them
    stale_products.map(&amp;amp;:destroy)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When cleaning up stale data, be very very sure that your query is correct before running the code. You should also be backing up your whole database periodically using a tool such as Heroku&amp;#8217;s &lt;a href="https://addons.heroku.com/pgbackups"&gt;PGBackups&lt;/a&gt; incase you accidentally delete incorrect data.&lt;/p&gt;

&lt;h2&gt;Wrap it up&lt;/h2&gt;

&lt;p&gt;Hope this helps, I&amp;#8217;ll try to do a screencast on this pattern. It will really help if you&amp;#8217;ve had problems implementing this, to let me know what they were. Also if you have another method of doing partial model validation with a wizard, I&amp;#8217;m interested in that too. As always you can find me on the internet &lt;a href="http://twitter.com/schneems"&gt;@schneems&lt;/a&gt;. Thanks for using Wicked!&lt;/p&gt;</description><link>http://schneems.com/post/21267434507</link><guid>http://schneems.com/post/21267434507</guid><pubDate>Tue, 17 Apr 2012 09:34:00 -0400</pubDate></item><item><title>Performance Testing Rails with BlitzIO</title><description>&lt;p&gt;Haven&amp;#8217;t you always wanted to make some changes to your server and then absolutely slam it with traffic to see the result? Thats pretty much what I did last week while writing how to &lt;a href="http://schneems.com/post/20472425017/super-charge-your-rails-app-with-rack-cache-and"&gt;Super Charge your Rails App with Rack Cache&lt;/a&gt;, using the BlitzIO tool.&lt;/p&gt;

&lt;p&gt;If you&amp;#8217;re interested in trying out Blitz watch a demo in the screencast below.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/sMslNCnAlNc" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;For a walkthrough on how to add Rack::Cache and Memcache to your server read my  &lt;a href="https://devcenter.heroku.com/articles/rack-cache-memcached-static-assets-rails31"&gt;performance article&lt;/a&gt; on the Heroku Dev Center.&lt;/p&gt;</description><link>http://schneems.com/post/21027306312</link><guid>http://schneems.com/post/21027306312</guid><pubDate>Fri, 13 Apr 2012 11:57:23 -0400</pubDate><category>performance</category><category>blitzio</category><category>ruby</category><category>rails</category><category>testing</category><category>load</category></item><item><title>Mama Schneems Deploys A Web App

When my Mother asked me what I...</title><description>&lt;iframe width="400" height="225" src="http://www.youtube.com/embed/r-hq6dSLN-U?wmode=transparent&amp;autohide=1&amp;egm=0&amp;hd=1&amp;iv_load_policy=3&amp;modestbranding=1&amp;rel=0&amp;showinfo=0&amp;showsearch=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;&lt;br/&gt;&lt;br/&gt;&lt;h2&gt;Mama Schneems Deploys A Web App&lt;/h2&gt;

&lt;p&gt;When my Mother asked me what I do at my new job at Heroku, I decided to grab a camera and show her. With a little help she deployed a Rails app that I made in around 30 minutes. How fast can your mom deploy your web app? &lt;a href="http://www.youtube.com/watch?v=r-hq6dSLN-U"&gt;Hi Def Video&lt;/a&gt;&lt;/p&gt;</description><link>http://schneems.com/post/20525742163</link><guid>http://schneems.com/post/20525742163</guid><pubDate>Thu, 05 Apr 2012 09:38:00 -0400</pubDate><category>heroku</category><category>rails</category><category>web</category><category>app</category><category>deploy</category></item><item><title>Super Charge your Rails App with Rack Cache and Memcache</title><description>&lt;p&gt;Slow is sweeping the nation: slow food, slow living, and slow reading. Unfortunately your app called, it said it wants to be fast. Web apps that respond quickly are more enjoyable to work with and Google even gives them a small SEO bump. Recently &lt;a href="http://37signals.com/svn/posts/3112-how-basecamp-next-got-to-be-so-damn-fast-without-using-much-client-side-ui"&gt;basecamp next&lt;/a&gt; got quite a bit of customer love based on how quickly it responds. The fact of the matter is if it&amp;#8217;s on the web, fast matters.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;This is based on an article I wrote for the Heroku Dev Center
  &lt;a href="https://devcenter.heroku.com/articles/rack-cache-memcached-static-assets-rails31"&gt;Using Rack::Cache with Memcached for Static Asset Caching in Rails 3.1+&lt;/a&gt;. 
  If  you have any questions ping me &lt;a href="http://twitter.com/schneems"&gt;@schneems&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the quickest ways we can speed up your whole application is to add on HTTP caching. Not only does this mean we return static files quickly, we also reduce the overall load of your application. The easy answer to this hard problem is to configure your application to use &lt;a href="https://devcenter.heroku.com/articles/rack-cache-memcached-static-assets-rails31"&gt;Rack::Cache with Memcache&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Performance Testing&lt;/h2&gt;

&lt;p&gt;I did some load testing of the &lt;a href="http://rack-cache-demo.herokuapp.com/"&gt;example app&lt;/a&gt; with the trial version of the &lt;a href="https://addons.heroku.com/blitz"&gt;BlitzIO addon&lt;/a&gt;. BlitzIO will hit whatever url you specify with as many simulated users as you want and graph the result for you. This is a run from 1 to 250 concurrent users over the course of 60 seconds with four dynos. Compare the default production settings to using Memcache with Rack::Cache.&lt;/p&gt;

&lt;h3&gt;Rails Default Settings&lt;/h3&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_m1xfilDL7F1qevexf.png" alt="Rails Default Settings"/&gt;&lt;/p&gt;

&lt;p&gt;(Left is response time [peaks around 500ms] right is number of concurrent users represented by straight line, bottom is time of test )&lt;/p&gt;

&lt;h3&gt;Memcache &amp;amp; Rack Cache&lt;/h3&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_m1xfk5rZIt1qevexf.png" alt="Memcache Rack::Cache"/&gt;&lt;/p&gt;

&lt;p&gt;(Left is response time [peaks around 100ms] right is number of concurrent users represented by straight line, bottom is time of test )&lt;/p&gt;

&lt;p&gt;Here the Memcache &amp;amp; Rack::Cache combo smokes the default rails settings. If you want to run your own tests, you should know that each time you run BlitzIO, your performance graph will be different. I recommend running a few tests to make sure you&amp;#8217;re not seeing a fluke. I also &lt;strong&gt;highly&lt;/strong&gt; recommend doing any type of load testing on a staging server instead of production, or it could take down your site and your users wouldn&amp;#8217;t be too happy.&lt;/p&gt;

&lt;h2&gt;Settings&lt;/h2&gt;

&lt;p&gt;So how can you, the speedfreak you are, get these types of results in your app? Long story short, you want to add this to your &lt;code&gt;config/production.rb&lt;/code&gt; file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;config.action_dispatch.rack_cache = {
                        :metastore    =&amp;gt; Dalli::Client.new,
                        :entitystore  =&amp;gt; 'file:tmp/cache/rack/body',
                        :allow_reload =&amp;gt; false }
config.static_cache_control = "public, max-age=2592000"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For more details on how to implement add this to your Rails app and why we chose these settings, please &lt;a href="https://devcenter.heroku.com/articles/rack-cache-memcached-static-assets-rails31"&gt;read the devcenter article&lt;/a&gt; or &lt;a href="https://github.com/heroku/rack-cache-demo"&gt;browse source&lt;/a&gt; on my demo app. Good luck, and enjoy the speed!&lt;/p&gt;</description><link>http://schneems.com/post/20472425017</link><guid>http://schneems.com/post/20472425017</guid><pubDate>Wed, 04 Apr 2012 12:59:00 -0400</pubDate><category>rails</category><category>rack</category><category>cache</category><category>performance</category><category>blitzio</category><category>addon</category><category>speed</category><category>memcache</category></item><item><title>You got NoSQL in my Postgres! Using Hstore in Rails</title><description>&lt;p&gt;Heroku just announced their &lt;a href="https://postgres.heroku.com/blog/past/2012/3/14/introducing_keyvalue_data_storage_in_heroku_postgres/"&gt;support of hstore&lt;/a&gt; in their dedicated Postgres 9.1 instances. Hstore is a schema less key value store inside of PostgreSQL that allows us to store data like hashes directly inside of a column. It&amp;#8217;s great for when you don&amp;#8217;t know exactly what types of attributes you need to store on a model, or if you need to support many different attributes for the same model.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Update: You can now use Hstore with &lt;a href="https://postgres.heroku.com/blog/past/2012/4/26/heroku_postgres_development_plan"&gt;development databases&lt;/a&gt; on Heroku&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A good example is storing attributes for a Product model. We might start out only selling books, which have an author, number of pages, but then transition over to selling laptops which have cpu speed and display resolution. Using Hstore allows us to easily store all these values without having to make a bunch mostly blank columns.&lt;/p&gt;

&lt;p&gt;To get started with Rails and hstore you can watch the screencast below or visit the &lt;a href="http://hstoredemo.herokuapp.com/"&gt;hstore example app&lt;/a&gt; running on Heroku.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/trycX7CL2A0?rel=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;h2&gt;More on Hstore&lt;/h2&gt;

&lt;p&gt;Hstore in Rails functions much like serializing hashes, except that we can query our data much faster since hstore is a native data type. It is supported natively in Rails 4, but until then we&amp;#8217;ll need to use the &lt;a href="https://github.com/softa/activerecord-postgres-hstore"&gt;activerecord-postgres-hstore&lt;/a&gt; gem.&lt;/p&gt;

&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;You will need a version of PostgreSQL locally that supports the hstore extension. I recommend installing postgres using &lt;a href="http://mxcl.github.com/homebrew/"&gt;homebrew&lt;/a&gt; on OS X. Once you&amp;#8217;ve done that you can enable hstore usage by running this in Postgres&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;CREATE EXTENSION hstore;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can put this in a migration if you prefer&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class SetupHstore &amp;lt; ActiveRecord::Migration
  def self.up
    execute "CREATE EXTENSION hstore"
  end

  def self.down
    execute "DROP EXTENSION hstore"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once that is done you will need to create a column with a type of hstore, here we are giving our Product model a column called &lt;code&gt;data&lt;/code&gt; with hstore type.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class CreateProducts &amp;lt; ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string  :name
      t.hstore  :data
      t.timestamps
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once we&amp;#8217;ve done that we can now store any type of attributes in the data column.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Product.create(:name =&amp;gt; "Geek Love: A Novel", :data =&amp;gt; {'author' =&amp;gt; 'Katherine Dunn', 'pages' =&amp;gt; 368, 'category' =&amp;gt; 'fiction'})
Product.last.data['category']  # =&amp;gt; 'fiction'
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Querying&lt;/h2&gt;

&lt;p&gt;Not only does hstore allow us to store arbitrary keys and values it allows us to quickly query them.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  # Find all products that have a key of 'author' in data
  Product.where("data ? :key", :key =&amp;gt; 'author')

  # Find all products that have a 'pages' and '368' key value pair in data
  Product.where("data @&amp;gt; (:key =&amp;gt; :value)", :key =&amp;gt; 'pages', :value =&amp;gt; '368')

  # Find all products that don't have a key value pair 'pages' and '999' in data
  Product.where("not data @&amp;gt; (:key =&amp;gt; :value)", :key =&amp;gt; 'pages', :value =&amp;gt; '999')

  # Find all products having key 'author' and value like 'ba' in data
  Product.where("data -&amp;gt; :key LIKE :value",     :key =&amp;gt; 'author, :value =&amp;gt; "%Kat%")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;More information available in the &lt;a href="http://www.postgresql.org/docs/8.4/static/hstore.html"&gt;Postgres hstore docs&lt;/a&gt;.
Though like a normal column if you query it frequently, you can get even more speed by adding an index. You can do this using one of two indexes that also speed up full text searches. They&amp;#8217;re GiST (Generalized Search Tree) or GIN (Generalized Inverted iNdex). Which sill speed up queries using the &lt;code&gt;@&amp;gt;&lt;/code&gt; and &lt;code&gt;?&lt;/code&gt; postgres operators.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Index &amp;lt; ActiveRecord::Migration
  def up
    execute "CREATE INDEX products_gin_data ON products USING GIN(data)"
  end

  def down
    execute "DROP INDEX products_gin_data"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Use It&lt;/h2&gt;

&lt;p&gt;Try out the &lt;a href="http://hstoredemo.herokuapp.com/"&gt;hstore example app&lt;/a&gt;, clone the &lt;a href="https://github.com/heroku/hstore_example"&gt;Github repo&lt;/a&gt;, and let me know what cool things you build on twitter &lt;a href="http://twitter.com/schneems"&gt;@schneems&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Thanks&lt;/h2&gt;

&lt;p&gt;Special thanks to &lt;a href="http://twitter.com/tenderlove"&gt;Aaron Patterson&lt;/a&gt; and &lt;a href="https://github.com/joelhoffman"&gt;Joel Hoffman&lt;/a&gt; for their work with hstore &amp;amp; Rails4, to the team at &lt;a href="https://github.com/softa/activerecord-postgres-hstore"&gt;Softa&lt;/a&gt; for writing this gem, &amp;amp; and the team at &lt;a href="https://postgres.heroku.com/"&gt;Heroku&lt;/a&gt; for their contributions to Postgres, and supporting this feature.&lt;/p&gt;</description><link>http://schneems.com/post/19298469372</link><guid>http://schneems.com/post/19298469372</guid><pubDate>Wed, 14 Mar 2012 14:40:00 -0400</pubDate><category>heroku</category><category>postgres</category><category>hstore</category><category>rails</category><category>ruby on rails</category></item><item><title>Get Down with Heroku at SXSW this year register now. It’s...</title><description>&lt;img src="http://24.media.tumblr.com/tumblr_m0g1gqzAHM1qf8jnho1_500.png"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;Get Down with Heroku at SXSW this year &lt;a href="http://getdownparty.com/"&gt;register now&lt;/a&gt;. It’s gonna be awesome.&lt;/p&gt;</description><link>http://schneems.com/post/18907336588</link><guid>http://schneems.com/post/18907336588</guid><pubDate>Wed, 07 Mar 2012 13:00:06 -0500</pubDate><category>SXSW</category><category>heroku</category><category>party</category></item><item><title>I love doing screencasts, but hate the way my MBP mic sounds, so...</title><description>&lt;iframe width="400" height="299" src="http://www.youtube.com/embed/1oINexw7YMY?wmode=transparent&amp;autohide=1&amp;egm=0&amp;hd=1&amp;iv_load_policy=3&amp;modestbranding=1&amp;rel=0&amp;showinfo=0&amp;showsearch=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;I love doing screencasts, but hate the way my MBP mic sounds, so I got a Rode Podcaster mic. You can see the difference in this quick video. All sound is raw and un-edited. I’m pretty happy with the purchase :)&lt;/p&gt;</description><link>http://schneems.com/post/18901870101</link><guid>http://schneems.com/post/18901870101</guid><pubDate>Wed, 07 Mar 2012 10:00:05 -0500</pubDate></item><item><title>Test Drive: Induction - The Everything Database Client for Mac</title><description>&lt;p&gt;Join me for a quick demo of Induction, the latest project by &lt;a href="http://twitter.com/mattt"&gt;@Mattt&lt;/a&gt; from &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt;. Induction will let you view your data-stores including: PostgreSQL, MySQL, SQLite, Redis, MongoDB, and so much more. The alpha product also lets you run queries, and visualize data.&lt;/p&gt;

&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/XC9GDDT28Vw?rel=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;Induction is completely free and open source, for more info visit the &lt;a href="http://inductionapp.com/"&gt;website&lt;/a&gt; or clone the &lt;a href="https://github.com/Induction/Induction"&gt;repo&lt;/a&gt;.&lt;/p&gt;</description><link>http://schneems.com/post/18847708067</link><guid>http://schneems.com/post/18847708067</guid><pubDate>Tue, 06 Mar 2012 10:00:00 -0500</pubDate><category>osx</category><category>induction</category><category>database</category><category>nosql</category><category>mac</category><category>client</category></item><item><title>Give Tumblr Some Code &lt;3 with Prettify.js</title><description>&lt;p&gt;Lets admit it, code and Tumblr don&amp;#8217;t exactly get along right now. Sure you can write your posts in markdown, but there isn&amp;#8217;t really an out of the box experience for syntax highlighting in code blocks.&lt;/p&gt;

&lt;p&gt;In the past I&amp;#8217;ve resorted to using &lt;a href="http://schneems.com/post/15948562424/speed-up-capybara-tests-with-devise"&gt;Gists&lt;/a&gt; to store code in my blog, but this can be a hassle to manage, especially if you need to modify your code later. One day I noticed that one of my favorite blogs &lt;a href="http://thechangelog.com/post/18070793740/recommendify-ruby-redis-based-recommendation-engine"&gt;The Changelog&lt;/a&gt; has great code highlighting on their Tumblr blog. With a little view source-ing and a little googling I eventually found out that the best way to add code highlighting to your blog is through a javascript library called &lt;code&gt;prettify.js&lt;/code&gt;. I followed these directions by &lt;a href="http://blog.blairvanderhoof.com/post/17703850161/code-syntax-highlighting-on-tumblr"&gt;blairvanderhoof&lt;/a&gt;, and came up with something not too bad. While his instructions are good, I made a few modifications. Lets take a look.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;&lt;p&gt;Link to the prettify.js CDN in your html, by customizing and putting this in right before the&lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tag:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/prettify/188.0.0/prettify.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add an the &amp;#8216;prettyprint&amp;#8217; class to all &lt;code&gt;&amp;lt;pre&amp;gt;&lt;/code&gt; tags that contain a &lt;code&gt;&amp;lt;code&amp;gt;&lt;/code&gt; tag. Then initialize the &lt;code&gt;prettyPrint()&lt;/code&gt; javascript method by adding this in your HTML:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$(document).ready( function(){
  $('code').parent('pre').addClass('prettyprint');
  prettyPrint();
});
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pick a &lt;a href="http://google-code-prettify.googlecode.com/svn/trunk/styles/index.html"&gt;prettify theme&lt;/a&gt;. I chose the Desert theme. Add that style to the &lt;code&gt;Add Custom CSS&lt;/code&gt; under the advanced section, or directly to your HTML.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; /* desert scheme ported from vim to google prettify */
pre { display: block; background-color: #333; overflow:scroll; font-size:12px;  }
pre .nocode { background-color: none; color: #000 }
pre .str { color: #ffa0a0 } /* string  - pink */
pre .kwd { color: #f0e68c; font-weight: bold }
pre .com { color: #87ceeb } /* comment - skyblue */
pre .typ { color: #98fb98 } /* type    - lightgreen */
pre .lit { color: #cd5c5c } /* literal - darkred */
pre .pun { color: #fff }    /* punctuation */
pre .pln { color: #fff }    /* plaintext */
pre .tag { color: #f0e68c; font-weight: bold } /* html/xml tag    - lightyellow */
pre .atn { color: #bdb76b; font-weight: bold } /* attribute name  - khaki */
pre .atv { color: #ffa0a0 } /* attribute value - pink */
pre .dec { color: #98fb98 } /* decimal         - lightgreen */

/* Specify class=linenums on a pre to get line numbering */
ol.linenums { margin-top: 0; margin-bottom: 0; color: #AEAEAE } /* IE indents via margin-left */
li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8 { list-style-type: none }
/* Alternate shading for lines */
li.L1,li.L3,li.L5,li.L7,li.L9 { }

@media print {
  pre { background-color: none }
  pre .str, code .str { color: #060 }
  pre .kwd, code .kwd { color: #006; font-weight: bold }
  pre .com, code .com { color: #600; font-style: italic }
  pre .typ, code .typ { color: #404; font-weight: bold }
  pre .lit, code .lit { color: #044 }
  pre .pun, code .pun { color: #440 }
  pre .pln, code .pln { color: #000 }
  pre .tag, code .tag { color: #006; font-weight: bold }
  pre .atn, code .atn { color: #404 }
  pre .atv, code .atv { color: #060 }
}
.content pre code {background-color:inherit; color:inherit}
.content pre      { padding:1em; margin: 0.5em 0px; }
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Note: I changed some of the Desert theme to play nice with my existing styles.&lt;/p&gt;

&lt;p&gt;If your blog includes code snippets, you owe it to your audience to have good looking and readable syntax highlighting.&lt;/p&gt;

&lt;p&gt;If you&amp;#8217;re interested in getting started and aren&amp;#8217;t already locked into a blogging platform, I recommend trying out &lt;a href="https://github.com/markpundsack/jekyll-heroku"&gt;Jekyll&lt;/a&gt; on &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; it&amp;#8217;s easy to set up and a real hackers blog. You also get syntax highlighting out of the box. Hope this helped &amp;amp; happy blogging.&lt;/p&gt;</description><link>http://schneems.com/post/18551760180</link><guid>http://schneems.com/post/18551760180</guid><pubDate>Thu, 01 Mar 2012 10:00:06 -0500</pubDate><category>tumblr</category><category>code</category><category>pretty</category><category>syntax highlighting</category></item><item><title>Wizard-ify Your Rails Controllers with Wicked</title><description>&lt;p&gt;If insanity is doing the same thing and expecting different results, I was going crazy writing step-by-step wizards.
I was never happy with the end result, they did what I wanted, but were messy and had too many moving parts. I wanted a simple &amp;amp; re-usable way to create restful-ish controllers. Thats when I decided to rip out all that scary controller logic and bake it into in a Gem I call &lt;a href="http://github.com/schneems/wicked"&gt;Wicked&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Wizards can be used for a number of things, they appear frequently after signing up for a service. They typically ask for additional information, or they give the user a tour of a service. Before I started working for &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt;, I helped out a bit on the peer learning site, &lt;a href="http://hourschool.com"&gt;Hourschool&lt;/a&gt;. Since students are most interested in the courses that are close to them, Hourschool asks users for their zip code. When a student signs up via Facebook, the zipcode isn&amp;#8217;t directly exposed via the API. So to get that extra input, we decided to add an after register wizard to ask for this information and more. If you want to add similar functionality to your Rails site, you can watch the &lt;a href="http://www.youtube.com/watch?v=K1IEy4UJLjE"&gt;screencast&lt;/a&gt;, &lt;a href="https://github.com/schneems/wicked_example"&gt;download an example&lt;/a&gt;, &lt;a href="https://github.com/schneems/wicked"&gt;browse the source&lt;/a&gt;, or check out the getting started guide below.&lt;/p&gt;

&lt;iframe width="480" height="360" src="http://www.youtube.com/embed/K1IEy4UJLjE?rel=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;h2&gt;Get Started&lt;/h2&gt;

&lt;p&gt;First install the gem, then inherit from &lt;code&gt;Wicked::WizardController&lt;/code&gt; and you can specify a set of steps. Here we have a controller called &lt;code&gt;AfterRegisterController&lt;/code&gt; with existing routes.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class AfterRegisterController &amp;lt; Wicked::WizardController

  steps :add_zip, :add_twitter, :add_friends
  # ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Create a Show and Update action, calling &lt;code&gt;render_wizard&lt;/code&gt; at the end of each which allows us to show the appropriate view for the step.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class AfterRegisterController &amp;lt; Wicked::WizardController

  steps :add_zip, :add_twitter, :add_friends

  def show
    @user = current_user
    render_wizard
  end

  def update
    @user = current_user
    render_wizard
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By default the wizard will render a view with the same name as the step. So you need to create view files for each step, in this case &lt;code&gt;views/after_register/add_zip.html.erb&lt;/code&gt; &amp;amp; &lt;code&gt;views/after_register/add_twitter.html.erb&lt;/code&gt;. In those views we can use wizard helpers to create links to the next step.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;%= link_to 'skip', next_wizard_path %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or you can manually specify which wizard action you want to link to by using the wizard_path helper.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;%= link_to 'skip', wizard_path(:find_friends) %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Our &lt;code&gt;:add_zip&lt;/code&gt; action can have a form that uses the wizard path and calls the update action. To get to this update action, you simply need to submit a form that PUT&amp;#8217;s (or PATCH&amp;#8217;s) to the same url:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;p&amp;gt;Please Fill in your zip code!&amp;lt;/p&amp;gt;
&amp;lt;%= form_for(@user, :url =&amp;gt; wizard_path, :method =&amp;gt; :put) do |f| %&amp;gt;
  &amp;lt;%= f.text_field :zip, :placeholder =&amp;gt; "zip code" %&amp;gt;
  &amp;lt;%= f.submit 'Next', :class =&amp;gt; 'btn btn-primary' %&amp;gt;
&amp;lt;% end %&amp;gt;

&amp;lt;%= link_to 'skip', next_wizard_path %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We then need to make sure our Signup wizard&amp;#8217;s update action updates the current_user&amp;#8217;s attributes. After modifying the user we can pass it into &lt;code&gt;render_wizard&lt;/code&gt;, which will show the next step if the object saves or re-render the previous view if the user has validation errors.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def update
  @user = current_user
  @user.update_attributes(params[:user])
  render_wizard @user
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you want to skip showing a step under certain conditions you can do it by using the &lt;code&gt;step&lt;/code&gt; method which returns a symbol of the current step you&amp;#8217;re on, and &lt;code&gt;skip_step&lt;/code&gt; if you don&amp;#8217;t want to render the current step. So if we wanted to skip asking our users to authenticate with twitter if they skipped the add_zip step, we can do it like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def show
  @user = current_user
  case step
  when :add_twitter    
    skip_step if @user.zip.blank?
  end
  render_wizard
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Redirect the user here after they are created in your system, and you have a fully baked after registration wizard. Thats all there is to it, just create a new view file every time you add a new step and you&amp;#8217;re good to go. Please give this a try and message me at &lt;a href="http://twitter.com/schneems"&gt;@schneems&lt;/a&gt; if you find any other really killer ‘wizard’ applications&lt;/p&gt;</description><link>http://schneems.com/post/18437886598</link><guid>http://schneems.com/post/18437886598</guid><pubDate>Tue, 28 Feb 2012 10:00:00 -0500</pubDate></item></channel></rss>

