Category Archives: Rails

Ruby on Rails

Failing fast, Ruby style

While building up the server side RPC methods for client side calls from ExtJS’s new Direct RPC framework, I thought it would be useful to fail fast whenever the client caller violated the interface contract. Enter fail fast.

def submit(params)
action = params.delete(:action)
id = params.delete(:id)
assert(action) {|v| [’update’, ‘create’].include?(v)}

Further up the [...]

Beware the Class.new dragons

When using this neat snippet from Dr Nic for testing a plugin I use internally, I came across the behavior described by Avdi. The proposed solution works great!
A single line modification to Dr Nic’s class ensures it works with class_inheritable_accessor from ActiveSupport.

def create_class(class_name, superclass, &block)
klass = Class.new superclass
klass.instance_eval &block
Object.const_set class_name, klass
end

decorators for ActiveRecord model attributes

Something I’ve always felt was missing from ActiveRecord is the ability to abstract out presentation logic for attributes. For example, you may want to be able to get at an attribute called full_name, but store the constituent parts, first_name and last_name, in your relational database.
Thankfully, I am not alone. There’s a ticket on [...]

Rails serving an alternate public_path

The introduction of the new Rails.public_path setter seems useful only for page caching particulars. If you want to get Mongrel via script/server to serve up static content from an alternate root on request, you actually need to monkey patch your way into the Rack stuff, specifically Rails::Rack::Static.

module Rails
module Rack
[...]

juicer and dynamic merges

While a hack, a dirty way of getting Juicer to merge JavaScript files at every load is the following, which I’m running under a custom Webrick setup:

# Shamelessly cheat so Juicer cannot close its copy of STDOUT
module Juicer
LOGGER.instance_eval do
def close ; end
end
end
 
# Later, far away in a custom handler
Juicer::Cli.run(%w[merge -s –force -o /tmp/output.js js/my.js])
# Unfortunately, [...]

Discover card attempts to scam me into disability insurance

I received a call today from someone claiming to be calling on behalf of Discover with some entity called Monumental something, wanting to sell me some kind of a disability policy (maybe this?) paid for the first three months by Discover, then by me thereafter and charged to my card.
The first person I spoke to, [...]

Write awesome DNS server, teach English?

One of the reasons I left IT is nicely summed up by the author of maradns. While it’s an awesome DNS server, apparently being awesome doesn’t pay the bills.

I can not guarantee that MaraDNS will get this feature. There are a lot
of feature requests on the table right now, and no one (so [...]

Extend Array instance with anoymous Module

It’s hacks like these that grow my appreciation of Ruby.

array = []
array.extend(Module.new{
def append(value)
value.is_a?(Array) ? replace(self|value) : replace(self|self<<value)
end
})

In a plugin that mixes in functionality to many of my Rails models, I have a model array that I stick method names into. Some of the methods are preconfigured in the mixin itself. The rest are [...]

Floating Acrobat Reader in an Ext.Window via an iframe

Once in a while, you pull of something quite spectacular. Loading Acrobat Reader inside an Ext.Window is such a feat. It’s the cleanest solution I could come up with that enables a single Rails action to handle the entire PDF creation process without a redirect, keeping a session, or using a GET request. [...]

putline undefined for PGconn

If you’re running a recent version of Rails, the PostgreSQL ActiveRecord adapter has been updated, requiring an updated Pg driver. If you were previously using putline for doing a PostgreSQL COPY inside of Ruby, you’ll need to change two method calls accordingly if you’re getting an exception:

undefined method `putline’ for #<PGconn:0xb63968e8>

In a migration, I [...]