Change ActiveRecord Pool Size on the Fly

I have a rake task that runs using the application’s production environment. Unlike the production environment though, it is threaded. In order to make any real use out of that, I need to increase the ActiveRecord connection pool size. Normally you’d do this in your database.yml, but in this case I’d rather not modify the production environment’s settings for the sake of a single rake task.

Here’s how you can make the change, on the fly, inside of your rake task:

task :swimming_pool => :environment do
  ActiveRecord::Base.connection_pool.instance_variable_set('@size', 15)
  ActiveRecord::Base.connection_pool.instance_variable_set('@timeout', 10)
  # Do awesome threaded stuff here
end

The first line of the rake task increases your pool size and the second line changes your timeout when waiting for a connection from the pool. You can adjust those values to whatever you need. That just happened to be what I need for this particular task.

Posted in Code, ruby at June 16th, 2010. No Comments.

Earlier today, Elad Meidar asked on Twitter about how to bypass checking the Authenticity Token in Rails for an action, sometimes. The example he mentioned was for a write API but this could theoretically be used for other situations where you only want to skip the authenticity token check of an action under specific circumstances. We went back and fort

First off, you need to do some before filter work:

skip_before_filter :verify_authenticity_token, :only => [:your_action]
before_filter :semi_verify_authenticity_token, :only => [:your_action]

Then you need a function to define when to check for the token authenticity:

def semi_verify_authenticity_token
  verify_authenticity_token unless request.xhr? # Or whatever other criteria you would use
end

All you really have to do then is make sure that verify_authenticity_token gets called based on the params or request and you should be set. This can be useful for APIs or AJAX calls calls to a given action where you don’t mind skipping the token check, but you still want to enforce it for the regular HTML browser view.

Posted in Uncategorized at April 7th, 2010. No Comments.

Ruby String#Nameize Revised

This morning, Kevin Glowacz (@kevinglowacz) replied to me a few times on Twitter about Ruby String#Nameize class extension I had posted a while back.  I had done some work to it after posting it here.  Kevin also asked me a few questions about oddities that were in it that have since been resolved.  So thanks to his prodding, you get a slightly updated version…

The only real “feature” is that it will now handle full names just fine. Otherwise, the rest of the stuff was mostly performance related.  Here it is:

class String
  # Extension of the string class to properly handle camel names
  def nameize
    if self.match(/ /)
      # If the name has a space in it, we gotta run the parts through the nameizer.
      name = self.split(' ').each { |part| part.nameize! }.join(' ')
      return name
    elsif self.match(/^[A-Z]/)
      # If they took the time to capitalize their name then let's just jump out.
      return self
    else
      # If there are no spaces and there is no prior
      # capitalization then let's downcase the whole thing.
      name = self.downcase
    end
    # Let's now assume that they were lazy...
    return case
    when name.match(/^mac/)
      name.gsub(/^mac/, "").capitalize.insert(0, "Mac")
    when name.match(/^mc/)
      name.gsub(/^mc/, "").capitalize.insert(0, "Mc")
    when name.match(/^o\'/)
      name.split("'").each{ |piece| piece.capitalize! }.join("'")
    else
      name.capitalize # name is a first name or it's not Irish then capitalize it.
    end
  end

  def nameize!
    replace nameize # BANG!
  end

end

As always – question, comments, suggestions – shoot me an email, leave a comment, or hit me on Twitter (@PatrickTulskie).

Posted in Code, Software Development, ruby at December 30th, 2008. 5 Comments.

Leopard and MySQL Gem

Those of you doing rails development work on Leopard with MySQL have probably seen this error message when starting your app:

WARNING: You’re using the Ruby-based MySQL library that ships with Rails. This library is not suited for production. Please install the C-based MySQL library instead (gem install mysql).

Normally I don’t care, but I figured since I was doing some cleanup today and getting things ready to move on to a longer term it might be good to have a properly working MySQL gem.  I like to run with a system that is close to what we run production.  The closer you get, the less surprises you have when you push it live.

Read More…

Posted in Code, Software Development, Uncategorized, ruby at November 6th, 2008. 2 Comments.