Daniel Hoelbling-Inzko talks about programming

Restful-Authentication on Rails 3.1 RC1

While working on a small side project I decided to go with the bleeding edge and use Rails 3.1 RC1. One issue I ran into with this though is that they changed the generator scripts so the most instructions for the restful-authentication plugin didn’t work.

First of all the original plugin isn’t yet working on Rails 3 so there is a fork by vinsol that you can use instead.

After running: rails generate authenticated user session I did try to load /signup and was greeted by the following error:

uninitialized constant ApplicationController::AuthenticatedSystem

Looking throuhg sessions_controller.rb and users_controller.rb i found that both include AuthenticatedSystem with instructions to move that into application_controller.rb

Doing so unfortunately doesn’t work out of the box since rails apparently changed search-paths for libs so make sure to include the following in your application_controller.rb:

require File.join(Rails.root, 'lib', 'authenticated_system.rb')
include AuthenticatedSystem

At first I only tried require, but as it turns out require only loads a file and executes it (like include does in C) while include actually copies methods from another module to the current one.

Both are required for restful-authentication to work obviously, or your sessions_controller will throw errors like this one: undefined method `logout_keeping_session!' for #<UsersController:0x8fc409c>.

Hope this helps, it took me some time to figure this out, but I guess this is mostly because I am a total stranger to the Rails platform.

Filed under programmierung, rails, ruby

Sinatra vs WCF WebAPI

Yesterday I decided that there has to be a better way to expose data through JSon than to spin up MVC applications and abuse it to write a JSon returning service.

Since I was listening to Glenn Block on Hanselminutes talk about the WCF WebAPI I decided to give it a try and followed the hello-world article on codeplex to see what all the fuss is about.

It took about an hour to make the example work on my machine, and I completely failed in returning a clr-object as JSon. So after two hours of trying I gave up and decided to go to bed.

Today I had a few minutes of free time so I decided to have a look at Sinatra. 35 seconds later and I had a working “Hello World” running on my machine, with no configuration in 4 lines of code!

I then spent another 2 minutes figuring out how to return JSon from Sinatra (another 2 lines of code) and then decided to write this blog post.

Even though WebAPI is still a work-in-progress (that’s why it took so long to figure stuff out), it’s obcene that a WCF WebAPI “Hello World” requires more lines in web.config than Sinatra requires to do the whole sample.

In Sinatra the WCF sample literally boils down to these 10 lines:

require 'sinatra'
people = []
get "/hello/" do
	"Hello #{people.join(", ")}"
post "/hello/:person" do
	people << params[:person]

It’s tragic that you need to reference 6 .NET assemblies to achieve the things other platforms can do in 10 lines of code.. And the ruby code is also cleaner.

Filed under net, programmierung, ruby

Mappin Sql-Time to TimeSpan with NHibernate

Funny how long you can use NHibernate + Fluent NHibernate on greenfield applications and not use all of it’s mapping features. But one little project that needs to talk to a legacy database and you run into all kinds of troubles.

Today I had to map the time datatype present in MsSql2008. According to official Microsoft documentation the preferred clr-type for time is TimeSpan.

Unfortunately, by default NHibernate believes that the best way to map TimeSpan is to transform it to a bigint.

Fortunately the solution is rather trivial:

Map(p => p.CreatedOn, "CreationTime")

Filed under net, programmierung, nhibernate

NHibernate Composide-Id mapping gotcha

I am currently working on a new system that populates a legacy database, so I am knee deep in relational SQL weirdness, trying to fight it with arcane NHibernate mappings.

This gem just cost me over an hour of work:

//Inside a ClassMap
    .KeyProperty(p => p.Id)
    .KeyProperty(p => p.POSITION);
Map(p => p.Id, "Id");
Map(p => p.POSITION, "Position");

Trying to save this entity results in a Exception stating: Invalid Index 3 for SqlParameterCollection with Count=3

What happens here is that not even NHibernate Profiler can detect that you are doing something wrong because NHibernate fails to construct the SqlCommand to send to the database before any profiler can pick up on this.

What happened here is that I was mapping Id and Position twice, since the ComposideId already counts as one mapping.

So in order to make this work I had to remove the Map() instructions and specify the column name in the KeyProperty of ComposideId:

CompositeId() .KeyProperty(p => p.Id, "Id") .KeyProperty(p => p.POSITION, "Position");

Hope this helps.

Filed under net, programmierung, nhibernate

JavaScript scope and how to leverage it

Care to guess what this will output?

function() {
var x = 1;
	if (x < 5) {
		var y = 3;

If you expect y to be undefined you fell for the same trap as everyone who comes from a more traditional background.

The output is of course y = 3

JavaScript has no block scope

I’d suggest you tattoo that on the inside of your eyelids since it’s so important to remember..

Whenever JavaScript behaves totally irrational for you, take a hard look at your code and make sure you are not using block scope like you would in most other languages.

But how does JavaScript scope then?

JavaScript is a really nice functional language someone threw a lot of Java ugliness on to make it appeal to the unwashed masses.

But at it’s core it remains this beautiful functional language, so naturally: JavaScript scopes by functions

And since functions are objects, you can leverage this scoping to create your own little hidden namespaces whenever you like.

(function () {
var x = 1;
console.log(x); //undefined

Since functions in JavaScript are objects, you can define one and immediately call it afterwards, thus creating a block that hides everything inside from the outside. This becomes very important when you try to avoid naming collisions with other libraries when writing your business logic.

Since it’s advisable to group your logic, you can use the above function syntax to create objects with their own little shared namespace you can then call from your page:

var util = (function () { var x,y; //private variables nobody sees return { method1: function() { return x; }, method2: function() { return y; }, }; }());

util.method1() //returns value of x

Interesting here is that although we used a function to define util, since that function was executed right on the spot, util now points to the return value of that function. In this case an object with two methods on it.

And now things get weird: Closure.

If may strike you that at the time we call util.method1() the function that created util has already returned (a long time ago), so in theory we should not be able to access x and y.

But like it would with block-scope in a traditional language, JavaScript also looks at it’s parent scopes if it can’t resolve an identifier inside it’s current one.

And in functional scoping this means it has to look in it’s parent scope that is a already returned function.

In short: JavaScript keeps the scopes around as long as there are references to them, so even if a function returned in the past, it’s scope is still available to functions that can access it’s scope.

This also means you can do cool stuff like this function that allows another function to be executed exactly once:

var once = function ( fn ) { var callCount = 0; return function () { callCount += 1; if ( callCount > 1) { throw "Function can only be called once"; } fn(); }; };

var f = function() { alert("hi"); }; f = once(f); f(); f(); //this call will throw

Did I mention that programming JavaScript is totally addicting?

Filed under programmierung, javascript

My Photography business


dynamic css for .NET