No Clean Feed - Stop Internet Censorship in Australia

Search for Simplicity

Nigel Thorne's software development blog, focusing on finding simple solutions to real work problems.

Monday, March 15, 2010

Ruby BlankSlate Issue.. or The Problem Of Accidental Monkey Patching

I was updating a Rails app recently and found the tests weren't running.
The error was reported in HoboField (an ace gem that lets define your model fields within your model and auto-generate your migrations by 'diff'ing between schema and models).

The error was .../hobo_fields/field_declaration_dsl.rb:22 'field': wrong number of arguments (1 for 2)

This was being thrown when ruby was getting to a model class like this...


class SomeModel < ActiveRecord::Base
fields do
timestamps
end
...
end



The error turns out to be because the DSL class in Hobofields uses BlankSlate as it's base class. ActiveSupport also has a definition for BlankSlate, so Hobofields does an 'unless defined? BlankSlate' check before it defines one.

However... I was also using 'Riot' for my testing. Riot also defines BlankSlate.
The ActiveSupport version leaves the instance_eval method on the BlankSlate class. The Riot version removes it and doesn't check to see that BlankSlate is not already defined. Riot was accidentally Monkey Patching ActiveSupport.

As both libraries were being loaded, so the Riot definition was extending the ActiveSupport version and it was loosing it's instance_eval method. This leads to missing method picking up calls it shouldn't and eventually ends in the error I was getting.

I patched Riot and the problem went away. I expect to run into this sort of problem again in the future.

Do me a favor :) If you are developing a gem, please be really careful what you define in global space. It is best to namespace any classes you define so you don't risk cross over.

Thanks

Friday, March 12, 2010

Visual Studio 2010 Beta2 says "The application cannot start."

We have been having this issue with Visual Studio 2010 Beta2. It just stops loading. For some it stops loading unless you load it via loading a solution, others it's the opposite.

We can't move to the RC2 until we it supports Silverlight 4 properly..

The solution?

We have had some success by just loading up a visual studio command prompt and running:
devenv /ResetSettings

UPDATE:
Also try:
devenv /reset
devenv /resetuserdata


Give it a go. Let me know how you get on :)
(It might also be worth backing up your currentsettings.xml file first)

Monday, January 11, 2010

accepts_nested_attributes_for and sti

I have a site that lets you create child entities on the create form for the aggregate root. Thanks to accepts_nested_attributes_for this isn't too hard.

accepts_nested_attributes_for :kids, :allow_destroy => true

The problem I hit was mixing this with single table inheritance. When the child node is for a derived class, it saves without the 'type' information being set.

It seems the accepts_nested_attributes only creates new instances of the generic base class entity, which then ignores the value passed to the 'type' field.

I ended up doing this work around to get it to replace #new on the base class with an implementation that returns the correctly typed derived instance when the hash passed includes a 'type' field.

Thanks to Coderrr for posting this work around.

Note: I've backed up the code to http://gist.github.com/273858

Monday, September 21, 2009

Insert multiple word documents into another.

Just thought I would share this quick macro I wrote for our test team to let them join a bunch of documents together in word..

http://gist.github.com/190041

It's just simple enough to work.

Monday, August 24, 2009

Pit:: Manages your user config settings easily.

Pit is a real 'gem' of a gem.

I have a script that installs a bunch of gems to set up a development environment on rails. The problem is each user of the script needs to set up the proxy for their own specific user settings. I don't want to store that information in the script as everyone would be logged in as me. I don't want to have to remember to manually set up some config file for each user of the system either.

Pit acts like a repository for configuration data. In your code, just ask Pit for specific data and it will either return the known information OR prompt the user with an editor so they can fill in the missing information (which is then remembered). So simple!..

Install: gem install pit

Usage:

require "pit"
config = Pit.get("proxy.settings", :require => {
"username" => "default value",
"password" => "default value"
})


Install the gem and run gem server for more information.

The files are hosted at http://rubyforge.org/frs/?group_id=4607

Thanks LowReal whoever you are!

Search This Blog

Loading...

Blog Archive

About Me

My Photo
Nigel Thorne
I'm an agile developer with a focus in finding the simple solution that will scale.
View my complete profile

GitHub Projects