Permalink
Browse files

allow setting guid relations on unsaved objects

used to set routes on an unsaved app

Change-Id: Ia3bc85bc32aa8145c3876174c84f4be9312209cb
  • Loading branch information...
1 parent e21b8f0 commit 15f35ab39efe83118180db2e03e724512dcf4ea0 @pbozeman pbozeman committed Aug 24, 2012
Showing with 32 additions and 1 deletion.
  1. +10 −1 lib/sequel_plugins/vcap_relations.rb
  2. +22 −0 spec/models/app_spec.rb
@@ -1,6 +1,11 @@
# Copyright (c) 2009-2012 VMware, Inc.
module Sequel::Plugins::VcapRelations
+ # Depend on the instance_hooks plugin.
+ def self.apply(model)
+ model.plugin(:instance_hooks)
+ end
+
module ClassMethods
# Override many_to_one in order to add <relation>_guid
# and <relation>_guid= methods.
@@ -101,7 +106,11 @@ def define_to_many_methods(name, singular_name, ids_attr, guids_attr)
other = ar.associated_class[:guid => guid]
# FIXME: better error reporting
return if other.nil?
- send("add_#{singular_name}", other)
+ if pk
+ send("add_#{singular_name}", other)
+ else
+ after_save_hook { send("add_#{singular_name}", other) }
+ end
end
define_method("#{ids_attr}=") do |ids|
View
@@ -214,4 +214,26 @@
app.uris.should == [route.fqdn]
end
end
+
+ describe "adding routes to unsaved apps" do
+ it "should set a route by guid on a new but unsaved app" do
+ app = Models::App.new(:name => Sham.name,
+ :framework => Models::Framework.make,
+ :runtime => Models::Runtime.make,
+ :space => space)
+ app.add_route_by_guid(route.guid)
+ app.save
+ app.routes.should == [route]
+ end
+
+ it "should not allow a route on a domain from another org" do
+ app = Models::App.new(:name => Sham.name,
+ :framework => Models::Framework.make,
+ :runtime => Models::Runtime.make,
+ :space => space)
+ app.add_route_by_guid(Models::Route.make.guid)
+ expect { app.save }.should raise_error(Models::App::InvalidRouteRelation)
+ app.routes.should be_empty
+ end
+ end
end

0 comments on commit 15f35ab

Please sign in to comment.