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, 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!

Thursday, July 09, 2009

StoryQ

I've been looking at BDD with .Net recently. I'm evaluating a few story runners... There seem to be a few...


  • NBehave
  • NSpecify
  • StoryQ
  • NSpec
  • Spec#
  • MSpec


It's still early days, so I haven't decided on a library yet.

One possibility is the StoryQ story definition library. It isn't a runner, but uses your existing test runner as you define your specs within a test.


[Test]
public void ExecutorRunsTests()
{
var story = new Story("Running a test script");

story.AsA("tester")
.IWant("to run a test script")
.SoThat("I can verify the release meets the acceptance criteria")

.WithScenario("A passing test script")
.Given(() => ThereIsATestFileWithASinglePassingTest())
.When(() => TheTestIsRun())
.Then(() => TheTestPassShouldBeReported())
.And("report no failures or errors")

.WithScenario("A failing test script")
.Given(() => ThereIsATestFileWithASingleFailingTest())
.When(() => TheTestIsRun())
.Then(() => TheTestFailureShouldBeReported())
.And("report the failure")

.WithScenario("An erroring test script")
.Given(() => ThereIsATestFileWithASingleErroringTest())
.When(() => TheTestIsRun())
.Then(() => TheTestErrorShouldBeReported())
.And("report the error")
;
story.Assert();
}


The Statements can be written as Given(() => ThereIsATestFileWithASingleErroringTest()) or .Given("There is a test file with a single erroring test")

This allows you to start by defining them as strings and later go back and introduce the code. This makes the development feel bite sized and focused.

To help with this I wrote a simple VBA Macro to convert from text to a lambda method. It also converts back, but it's not perfect as ToUpper is a lossy operation, but it's good enough.

You can grab the macros here.

Wednesday, May 27, 2009

Rack Up Some RSpec Tests

Here's a simple rack application to render the output from running RSpec.
(Note: this is tailored to work on windows..)


#\ -w -p 8765
use Rack::Reloader, 0
use Rack::ContentLength

app = proc do |env|
formatting = '--require "C:\\...\\Ruby RSpec.tmbundle\\Support\\lib\\text_mate_formatter" -f ' +
'Spec::Runner::Formatter::TextMateFormatter' if env["REQUEST_PATH"] == "/textmate"
formatting = '-f h' if env["REQUEST_PATH"] == "/html"

out = `spec.bat spec #{formatting} 2>&1`
format = out=~/\ [ 200, {'Content-Type' => format}, out ]
end

run app


How to use it



Install rack if you haven't already. gem install rack


  • To start it, save this code into a file called 'spec_rack.ru'.
  • Change the '...' to the path to your textmate bundles... (or just ignore it if you don't use E or textmate)
  • Run rackup spec_rack.ru
  • visit http://localhost:8765 http://localhost:8765/html or http://localhost:8765/textmate


Why Bother?


I'm using RSpec for testing, but the integration with E (the windows clone of Textmate) seems to hang sometimes when there is an error in my code.
Having explorer open with the results so I can just press refresh is working well for me.

I wanted to post a simple rack application as there don't seem to be many examples around.

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