Category Archives: Rails

Ruby on Rails

Merge on the fly in development with juicer

Finally upgrading to the latest juicer, I found my prior hack no longer worked. I decided to revisit my solution and replace it with something more effective. While I still overrode a method, I dispatched the temporary file I was using previously. Ultimately this ought to be Rack middleware. Perhaps next time. class JuicerController < [...]

Exclusive favorite association for ActiveRecord has_many

It’s not uncommon in my application to maintain an exclusively preferred selection out of a collection on an object. For example: class Person < ActiveRecord::Base has_many :employers, :class_name => ‘Employee’ end   class Company < ActiveRecord::Base has_many :employees end   class Employee < ActiveRecord::Base belongs_to :person belongs_to :company   named_scope :favorite, {:conditions => {:favorite => [...]

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 stack, [...]

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, [...]

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 this [...]

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 class Static def initialize(app) [...]

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 [...]

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 [...]

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. Of course, [...]

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, [...]