From 37ab5dd2c5f6ae07ca3c8a6adcee8b520ef69501 Mon Sep 17 00:00:00 2001 From: nathanvda Date: Mon, 21 May 2012 00:55:33 +0200 Subject: [PATCH] Make sure it still works if we are working with mongo (use the old code then). We use the class-name so we do not get any issues when Mongoid is not known. Fixes #65. --- lib/cocoon/view_helpers.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index 3e7a6cf..dca2979 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -86,10 +86,16 @@ module Cocoon def create_object(f, association) assoc = f.object.class.reflect_on_association(association) - if assoc.collection? - f.object.send(association).build + if assoc.class.name == "Mongoid::Relations::Metadata" + conditions = assoc.respond_to?(:conditions) ? assoc.conditions.flatten : [] + assoc.klass.new(*conditions) else - f.object.send("build_#{association}") + # assume ActiveRecord or compatible + if assoc.collection? + f.object.send(association).build + else + f.object.send("build_#{association}") + end end end