Posts

Lesser Known Rails 2.2 Features

With Ruby on Rails 2.2 released , no doubt you've seen the major feature overviews. But did you know there are lots of subtle improvements? Pathfinder Development has a nice summary at Rails 2.2 For Me And For You . My favorite new feature that I didn't know about: Also, partials now take blocks, meaning they can act somewhat like layouts. The partial itself can contain a yield: <table> <tr><td> <%= yield %> </td></tr> </table> Then the call to the partial can include a block: <% render :partial => "table_partial" do %> <%= @project.name %> <% end %> And the yield command invokes the block, as you would expect Awesome!

Rails Backwards Incompatible Change Coming to 2.3, Application is Now ApplicationController

Just a head's up to Ruby on Rails fans out there. DHH just checked in a very backwards incompatible change for the 2.3 branch. The root class for your controllers is now ApplicationController and not Application . I'm hoping this patch is reverted and instead rolled out for 3.0.

Displaying Deployment Date and Time and Git Revision Number in Rails Views

I had a need to display the deployment date and time (timestamp) and the current revision/version number in the footer of all our pages in our Rails application. Our users always were asking "Did you update the web site?" Thanks to capistrano , the deployment tool, this turned out to be fairly easy. Capistrano already collects the deployed version and places it into a file named REVISION in the deployment directory. The contents hold whatever is reported by your version control software. For git , this is a SHA1 hash. For the deployment timestamp, thankfully capistrano uses a timestamp for the deployment directory name. So we can just parse the path, looking for the timestamp. Below is the helper code, please let me know if there's a better way. [sourcecode language='ruby'] # pulls from the capistrano directory structure def deploy_timestamp if Rails.env.production? RAILS_ROOT.split('/').last else "Undeployed" e...

Dear MySQL, Please Include a REST Interface In Your Next Version

Dear MySQL , I know you just got bought by Sun, and are now swimming in money and possibilities. Please continue this momentum by formalizing your connection to the Web by including a RESTful end point in your next version. The Web is where you grew up and matured, and it's time to give back. The architecture wars are nearly over, and REST has proven time and again to be the dominant architectural style for Web applications. You, MySQL, need to expose your data via REST to promote interoperability across clients and further cement your place as one of the top databases for the Web. This is low hanging fruit. Your URL space is well defined, as you have databases, tables and views, rows, and columns. The HTTP methods (GET, PUT, POST, DELETE) map very well to your SQL DML methods of SELECT, INSERT, UPDATE, and DELETE. I'd imagine your URL space to be something like: /databases /databases/NAME/tables /databases/NAME/tables/NAME/rows /databases/NAME/tables/NAME/rows/PK U...

A Count(*) View for CouchDB

I've been working with CouchDB a lot lately. The first thing I tried to do, after I loaded 1,000,000 records, is to do the equivalent of count(*) . Below is the view I used to implement a "count how many records I have in this database" or "count all": { "_id": "_design/counts", "language": "javascript", "views": { "all": { "map": "function(doc) { emit(null, 1); }", "reduce": "function(keys, values, combine) { return sum(values); }" } } }

Calculating Combinations Using Java and Lots of Bits

I was feeling nostalgic and went back to see how I calculated combinations in Erlang and combinations in Ruby . I wanted to see if there was a fun way to do it without resorting to recursion. I started with my crazy hack to use bit strings to calculate combinations . I didn't want to resort to creating bit strings, as I wanted to minimize the amount of work I needed to do. Therefore, I thought about simply using bitwise operators. Below is my Java algorithm for creating calculations using iteration and bitwise operators to create all combinations from an array. Thoughts? public static void generate(String[] list) { int max = (int) Math.pow(list.length, 2)-1; System.out.println(max + " combinations"); for (long i = 0; i < max; i++) { String[] combo = new String[Long.bitCount(i)]; int comboPos = 0; for (int j = 0; j < list.length; j++) { if ((i & (1L< 0) { combo[comboPos++] = list[j]; } } //System.out.println(Arrays.toString(combo)); ...

Easy Way to Configure Rails ActiveRecord and SQL Server on Mac OS X

Thanks to How to Connect To Microsoft SQL Server From Rails On OSX , I found out a very easy way to configure the database connection. Assuming you have FreeTDS installed (easily through macports ) and iODBC (comes out of the box with Mac OS X) then this is all you need in your database.yml file: development: adapter: sqlserver mode: odbc dsn: DRIVER=/opt/local/lib/libtdsodbc.so;TDS_Version=8.0;SERVER=10.0.6.20;DATABASE=awesome_development;Port=1433;uid=sa;pwd=password; No configuration of a DSN required, and no mucking about with freetds.conf! Now, if I could just use database.yml to get rid of SQL Server all together, I'd be a happy developer.