Posted by admin Thu, 18 Jun 2009 21:03:00 GMT

Running RCOV, and having it complain about a p articular plugin that it should not have been looking at in the first place, made me want to find a way to exclude it. In my usual practical fashion, I hit google up for a quick way to exclude the unwanted plugin.

Here it is re-posted from Dan Mange’s Blog

config.plugins = Rails::Initializer.new(config).send(:find_plugins, config.plugin_paths).map {|path| File.basename(path)}
config.plugins -= %W(plugin_one plugin_two)

UPDATE: JJ Barrett has an update for Rails 2.x… http://www.jjbarrett.net/archives/plugin-ordering-and-exclusion-in-rails-20

  config.plugins = config.plugin_locators.map do |locator|
                     locator.new(Rails::Initializer.new(config)).plugins
                   end.flatten.map{|p| p.name.to_sym}
  config.plugins -= [:do_not_load_plugin_1, :do_not_load_plugin_2]

Posted by admin Thu, 31 May 2007 23:48:00 GMT

If you are experiencing the error: “undefined class/module MyClass” when fetching data from memcached, be assured that you are not alone. It is a known bug and the simplest way I know of to get around it is to reference the class or classes right before you retrieve data from the cache.

For example, if the following code causes the problem:

if not (genres = Cache.get(key))
  genres = Genre.find(:all, :condition => "platform_id = 1")
  Cache.put(key, genres, 60*60*24) # cache for 1 day
end

… then this code will work around it:

Genre
if not (genres = Cache.get(key))
  genres = Genre.find(:all, :condition => "platform_id = 1")
  Cache.put(key, genres, 60*60*24) # cache for 1 day
end

Notice the ‘Genre’ reference before the if statement. Some have reported success by using the ‘model’ statement within the controller, however that is deprecated. This workaround will get you going again.

Posted by admin Wed, 23 May 2007 23:47:00 GMT

I am fairly anal about my code and the code that my firm (CBCI) produces. I like it to be formatted perfectly. It makes me cringe to even have a single extra space out of place, borking the indentation. Using tabs for indentation instead of spaces should be grounds for termination. :-D

In addition to perfect indentation, I don’t like scrolling to the right in my editor to view long lines of code. This can usually be handled properly by adding linefeeds. Consider the following example:

sql = %Q{
  SELECT
    users.id,
    users.username,
    users.email,
    users.created_at,
    users.activated_at,
    users.confirmed_at,
    affiliations.custom1 as affiliation_custom1,
    affiliations.custom2 as affiliation_custom2,
    affiliations.custom3 as affiliation_custom3,
    campaigns.name as affiliation_campaign_name
  FROM users
  LEFT OUTER JOIN affiliations ON affiliations.user_id = users.id
  LEFT OUTER JOIN campaigns ON campaigns.id = affiliations.campaign_id
}

There are several ways to handle this type of long string. First, for SQL statements, extra spaces in front of each line don’t matter, nor do the linefeeds. So the code above, using the ‘%Q’ method, would work just fine for building a string with spaces embedded. We could also use the lowercase ‘%q’ method which would wrap the string with single-quotes and prevent any escape sequences or interpolation of embedded expressions, which is fine since we don’t have any need for that in the above string. You could also wrap the quotation marks, single or double, around the string explicitly.

If you are working with a long character string, and are fussy about your code being formatted consistently, the indents will embed groups of spaces into your strings. As stated above, this technically doesn’t matter when considering SQL statements. In other cases where it does matter we use the Ruby HERE document as follows:

# first we extend the String class
class String
  def here_with_pipe
    lines = self.split("\n")
    lines.map! {|c| c.sub!(/\s*\|/, '')}
    new_string = lines.join(" ")
    self.replace(new_string)
  end
end

This strips off the leading whitespace and pipe character that gets embedded in the string using the HERE document like this:

  s = <<-end.here_with_pipe
    |This string will be concatenated
    |into a single line
    |containing no extra embedded spaces
    |and no line feeds.
  end

Isn’t Ruby cool!

Posted by admin Tue, 22 May 2007 23:46:00 GMT

Have you ever wanted to look at the call stack without raising an exception to do it?

caller.each {|c| puts c}

Posted by admin Tue, 28 Apr 2009 05:00:00 GMT

Thought I had posted this last year but can’t seem to find it, so here it is again:

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../config/boot'

# Restart Applicable Passenger Instance
filename = "#{RAILS_ROOT}/tmp/restart.txt"
File.open(filename, 'w') {|f| f.write('restart passenger') }

Save this file as: ./scripts/process/reaper, then deploy and it will restart your passenger instance.