Code insight for a local class?

Here’s a quick 3rdRail overview in screenshots (flickr required).

Code insight for a local class?

CodeGear released 3rdRail this morning. A few highlights:

  • Full Rails project support.
  • Rails specific refactoring, ie renaming a method in a controller will update all references as well as link_to and rename the associated view file.
  • Console with command completion.
  • Integrated Gecko browser with request monitor, DOM source, CSS and JavaScript

You can watch a screen cast here and download a trial for Windows, Unix and OSX here.

Here’s a quick 3rdRail overview in screenshots (flickr required).

dev_mode_performance_fixes plugin by Josh Goebel promises to speed up your Rails development server.

THE PROBLEM

So, what is the problem? It’s the fact that classes aren’t cached in development and that Rails insists on flushing them at the end of every request resulting in large chunks of your apps code having to be loaded, parsed, and reinterpreted for EVERY SINGLE request… which is SLOW.

THE IDEA

Why don’t we only reload the files when they have actually changed? Store the file names and their last modified time and then stat all the files before every request… if any are newer, kill the objects they originally defined and let the Dependency auto-loading code reload the objects from disk.

ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/dev_mode_performance_fixes/

I’ve signed up for Facebook today to check out how the whole application development works. Setting up a development server at home which is accessible from Facebook is pretty straight forward.

Here’s what you need:

  1. Facebook account
  2. Local box with Apache
  3. Domain name pointing to your home IP. I use No-Ip.

Here are the steps I took.

  1. Setting up development application on Facebook. Follow the guide, it’s pretty straight forward.
  2. Set up No-Ip to point *.dev.mydomain.com to my local box. This allows me to create as many Rails applications as I need to.
  3. Configured Apache to forward all calls to fbtest.dev.mydomain.com to localhost:3000 using mod_proxy like so:
    <VirtualHost *:80>
      ServerName fbtest.dev.mydomain.com
      ProxyPass / http://127.0.0.1:3000/
    </VirtualHost>
  4. gem install rfacebook
  5. cd /anywhere/fbtest
  6. rails .
  7. ruby script/plugin install svn://rubyforge.org/var/svn/rfacebook/plugins/rfacebook_on_rails
  8. rake facebook:setup
  9. Go to http://www.facebook.com/developers/apps.php
  10. Copy and paste API key and secret to config/facebook.yml. Set canvas_path to the local path on Facebook, ie /myfacebookapp/ and callback_path to local path on your server, ie /facebook/.
  11. ruby script/generate controller facebook index
  12. Edit index method in Facebook controller, like so:
    class FacebookController < ApplicationController
      def index
        @result = fbsession.friends_get.uid_list
      end
    end
  13. Edit index.rhtml like so:
    <% @result.each do |uid| %>
      <fb:name uid="<%= uid %>" />
    <% end %>
  14. Start Apache
  15. ruby script/server
  16. Now you should be able to hit your Facebook application page at http://apps.facebook.com/myfacebookapp

You should see a list of your friends. Took me about 30 minutes to figure all this out.

require 'taggable'

Now you can use acts_as_taggable without any warnings. While this may seem obvious to many here, I hope it prevents the waste of a couple hours to a few other newbies thus my post.

Source. Definitely saved me time.

Just stumbled on the fact that ActiveRecord automatically creates properties for all column in a SQL query, not just those in the table.

Model.find_by_sql("SELECT *, COUNT(*) AS rank FROM ...").each do |item|
  puts "%s (%d)" % [item.name, item.rank]
end

I’ve looked low and high for this feature in ActiveRecord but can’t seem to find a solution.

What I need is ability to specify associations by a column other than a primary key. For example:

create_table :nodes do |t|
  t.column :parent_id,        :integer
  t.column :uid,              :string,    :limit => 32
end

create_table :info do |t|
  t.column :uid,         :string, :limit => 32
  t.column :formatted,   :text
end

class Node < ActiveRecord::Base
  acts_as_tree
  has_many :info, :foreign_key => :uid, :primary_key => :uid
end

class Info < ActiveRecord::Base
  belongs_to :node, :foreign_key => :uid, :primary_key => :uid
end

As you can see, I’ve used a made up :primary_key option to indicate which key column should be used. set_primary_key isn’t a good option here because UID column is for lose association and it isn’t a primary key.

Is this at all possible in Rails 1.2.3? Unfortunately my Rails core knowledge is very limited at this point and I wouldn’t be able to add this feature myself.

So I’m really pissed with Mediatemple.

Not only it’s not the fastest dog on the block, especially the backend, they consistently kept shutting down Noobkit 5-8 times a day.

Mediatemple allocates 64MB of RAM for a Mongrel instance, which is fine. If your app goes over, they just shut Mongrel down, automatically… without restarting it. So I’m getting an SMS from Pingdom at 3AM telling me that the site is down and I have to go “cap restart” it.

Noobkit is a very small site currently and only been up and running for just over 10 days with barely 200 uniques a day. There’s absolutely no way Mongrel can tip over 64MB with this type of load. I tried running a production Mogrel locally and pound it with Apache’s AB. After 10,000 hits I’m seeing 45MB of RAM.

Response from Mediatemple to my question about frequent Mongrel shutdowns was as expected:

After reviewing our records, I was able to confirm that it has been shutting down repeatedly. However, testing it live versus on another system will not necessarily be an accurate estimate. You will either need to debug your application, or increase your container to another a larger server.

So yesterday I found a no-registration coupon and got myself an account at DreamHost. Moving the blog was a peace of cake. Setting up Capistrano was pretty easy as well and the DNS should be updated overnight.

At first, DreamHost seems much snappier. We’ll see how it works out.

As always folks, all good ideas come to you right after your release the bad once.

After writing and making my user registration all pretty for Noobkit, I realized today, about 12 hours after release, that it would’ve been much easier and better if were to just use Open ID.

Having looked around for a decent acts_as_open_id_user_and_does_everything_else plugin, the only thing I found was this shameful attempt by none other than the DHH himself.

I don’t know if he was high when he submitted it because it’s just horrible. Never mind the fact that it doesn’t run at all and code has syntax errors. Methods that are called just once, strange object/result/message bubbling and integration with user models which are never mentioned anywhere except on the error pages. Worst of all, it actually extends ApplicationController. Do I really need Open ID authentication methods in every single controller? Where’s all the prettiness and tidiness?

Well, I brought my own bag of said “prettiness and tidiness”. The result is my own open_id_authentication plugin. I’ve cleaned up the code, made it a regular module to be included in a model and split up spaghetti dual purpose method into two simple methods. One to be called to start Open ID authentication process and the second one to process response from the Open ID server.

Next time, when I learn how to create generators in plugins I will add some nice model generation. For now, it’s completely decoupled from the user model.

Enjoy!

Story goes like this: I was trying to find a way to display form error messages in a pretty tight place. Using your regular error_messages_for creates a pretty long list and makes my page scroll.

Turns out, ActiveRecordHelper has another far much less known method called error_message_on. I thought it was just me at first, but a quick look on Google revealed that error_message_on returns only 3,240 hits where as error_messages_for comes in at 16,200 hits.

Here’s how this looks like in code:

<label for="email">Email <%= error_message_on :user, :email %></label>
<%= f.text_field(:email) %>

And with a little bit of CSS magic this is what the form looks like:

It occurred to me that a good idea would be to add a Google sitemap to Noobkit, especially because it’s using frames. Here’s how I went about it:

In a regular controller I load up all my @packages but instead of a plain sitem_map.rhtml, I have a site_map.rxml file which looks like this:

xml.instruct!
xml.urlset "xmlns" => "http://www.google.com/schemas/sitemap/0.84" do
  xml.url do
    xml.loc         "http://test"
    xml.lastmod     w3c_date(Time.now)
    xml.changefreq  "monthly"
  end
  @packages.each do |package|
    xml.url do
      xml.loc         "http://www.noobkit.com/page/ref/#{package.full_name}"
      xml.lastmod     w3c_date(package.created_at)
      xml.changefreq  "monthly"
    end
  end
end

w3c_date goes into your_controller_helper.rb.

def w3c_date(date)
  date.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
end

That’s all to it. Packages provide entry points to their entire API trees and should be enough information for Google to start crawling there.

Last week I saw a post by Rob Sanheim about sucky Rails documentation. I don’t know why, but I got seriously inspired and started playing around with RDoc.

4 hours later, I had RDoc generator importing everything into a database and a few hours later it was completely browse-able.

After that, it’s was all the matter of picking a name (seriously, I used it) and making a pretty CSS for it.

So today I’m finally getting this baby public and I give you:

Noobkit Docs. Ruby on Rails documentation… on Rails.

I’ve got big plans for the upcoming releases. This is the first iteration to get the foot out of the door without feature creep.

Please help me get the word out and digg it :)

It’s time to dive into Capistrano and I can’t find any good get started tutorial.

Funny enough, I couldn’t even gem install capistrano. Apparently, on Windows, it’s common to be getting ERROR: While executing gem … (Zlib::BufError) buffer error.

To fix the problem I had to run gem update --system. Twice, because the first time it failed.

Thnx to don’t repeat yourself.

This issue allows an attacker to insert inject random JavaScript code which could potentialy be very harmful. It’s the same exploit that was used by the MySpace Worm. If you are using sanitize(), this is something to be aware of.

sanitize("<div style=\"width: expression(alert('gotcha'))\">pure innocence</div>")

Causes an infinite loop alert box in IE 7. Currently applies to Ruby on Rails 1.2.2.

A little gotcha about validating presence of boolean fields from the doc:

If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true.

Read the rest of this entry »