From bc09d7bbeedd751b25d68c70ffb67dd11986be5b Mon Sep 17 00:00:00 2001 From: Paul Thornthwaite Date: Mon, 17 Dec 2012 16:35:35 +0000 Subject: [PATCH 1/6] [core] Adds #persisted? to Fog models Fog models have been able to use #new_record? for years to use an ActiveRecord like interface to check for peristence of a resource. When Rails 3 switched to using ActiveModel API this changed to be #persisted? This deprecates #new_record? and adds #persisted? as the first little step to making fog models easier to use using ActiveModel API. As discussed on the tickes, ActiveModel is not planned to become a direct dependency for fog. Relates to #1276 --- lib/fog/core/attributes.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/fog/core/attributes.rb b/lib/fog/core/attributes.rb index ceadc8c3e..3abd63232 100644 --- a/lib/fog/core/attributes.rb +++ b/lib/fog/core/attributes.rb @@ -152,8 +152,25 @@ module Fog self end + # Returns true if a remote resource has been assigned an + # identity and we can assume it has been persisted. + # + # @return [Boolean] + def persisted? + !!identity + end + + # Returns true if a remote resource has not been assigned an + # identity. + # + # This was added for a ActiveRecord like feel but has been + # outdated by ActiveModel API using {#persisted?} + # + # @deprecated Use inverted form of {#persisted?} + # @return [Boolean] def new_record? - !identity + Fog::Logger.deprecation("#new_record? is deprecated, use !persisted? instead [light_black](#{caller.first})[/]") + !persisted? end # check that the attributes specified in args exist and is not nil From 92843efc1beba5d9836df37354e621e7bd5ade10 Mon Sep 17 00:00:00 2001 From: Paul Thornthwaite Date: Wed, 19 Dec 2012 14:50:41 +0000 Subject: [PATCH 2/6] [aws] Replaces #new_record? with #persisted? --- lib/fog/aws/models/compute/address.rb | 6 +++--- lib/fog/aws/models/compute/volume.rb | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/fog/aws/models/compute/address.rb b/lib/fog/aws/models/compute/address.rb index 34c636d91..6bb4b23f8 100644 --- a/lib/fog/aws/models/compute/address.rb +++ b/lib/fog/aws/models/compute/address.rb @@ -14,7 +14,7 @@ module Fog attribute :domain def initialize(attributes = {}) - # assign server first to prevent race condition with new_record? + # assign server first to prevent race condition with persisted? self.server = attributes.delete(:server) super end @@ -52,7 +52,7 @@ module Fog private def associate(new_server) - if new_record? + unless persisted? @server = new_server else @server = nil @@ -64,7 +64,7 @@ module Fog def disassociate @server = nil self.server_id = nil - unless new_record? + if persisted? connection.disassociate_address(public_ip) end end diff --git a/lib/fog/aws/models/compute/volume.rb b/lib/fog/aws/models/compute/volume.rb index acc9c78fa..825b05722 100644 --- a/lib/fog/aws/models/compute/volume.rb +++ b/lib/fog/aws/models/compute/volume.rb @@ -22,7 +22,7 @@ module Fog attribute :type, :aliases => 'volumeType' def initialize(attributes = {}) - # assign server first to prevent race condition with new_record? + # assign server first to prevent race condition with persisted? self.server = attributes.delete(:server) super end @@ -103,7 +103,7 @@ module Fog end def attach(new_server) - if new_record? + if !persisted? @server = new_server self.availability_zone = new_server.availability_zone elsif new_server @@ -118,7 +118,7 @@ module Fog def detach(force = false) @server = nil self.server_id = nil - unless new_record? + if persisted? connection.detach_volume(id, 'Force' => force) reload end From 8d478960100b5680bceff68305dc1c2b9b68a8fa Mon Sep 17 00:00:00 2001 From: Paul Thornthwaite Date: Wed, 19 Dec 2012 14:51:41 +0000 Subject: [PATCH 3/6] [ecloud] Replaces #new_record? with #persisted? --- lib/fog/ecloud/models/compute/internet_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/ecloud/models/compute/internet_service.rb b/lib/fog/ecloud/models/compute/internet_service.rb index f2ea6aa41..2551eebb0 100644 --- a/lib/fog/ecloud/models/compute/internet_service.rb +++ b/lib/fog/ecloud/models/compute/internet_service.rb @@ -27,7 +27,7 @@ module Fog end def save - if new_record? + unless persisted? result = connection.internet_service_create( collection.href, _compose_service_data ) merge_attributes(result.body) else From e020b8024992aebf2e4d734c5f42d8f9e77fbe9b Mon Sep 17 00:00:00 2001 From: Paul Thornthwaite Date: Wed, 19 Dec 2012 14:53:27 +0000 Subject: [PATCH 4/6] [hp] Replaces #new_record? with #persisted? --- lib/fog/hp/models/compute/address.rb | 6 +++--- lib/fog/hp/models/compute/server.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/fog/hp/models/compute/address.rb b/lib/fog/hp/models/compute/address.rb index d356ed12c..a42e8a727 100644 --- a/lib/fog/hp/models/compute/address.rb +++ b/lib/fog/hp/models/compute/address.rb @@ -13,7 +13,7 @@ module Fog attribute :instance_id def initialize(attributes = {}) - # assign server first to prevent race condition with new_record? + # assign server first to prevent race condition with persisted? self.server = attributes.delete(:server) super end @@ -47,7 +47,7 @@ module Fog private def associate(new_server) - if new_record? + unless persisted? @server = new_server else @server = nil @@ -58,7 +58,7 @@ module Fog def disassociate @server = nil - unless new_record? + if persisted? connection.disassociate_address(instance_id, ip) end self.instance_id = nil diff --git a/lib/fog/hp/models/compute/server.rb b/lib/fog/hp/models/compute/server.rb index 82197de09..8901fa278 100644 --- a/lib/fog/hp/models/compute/server.rb +++ b/lib/fog/hp/models/compute/server.rb @@ -34,7 +34,7 @@ module Fog attr_writer :image_id, :flavor_id def initialize(attributes = {}) - # assign these attributes first to prevent race condition with new_record? + # assign these attributes first to prevent race condition with persisted? self.security_groups = attributes.delete(:security_groups) self.min_count = attributes.delete(:min_count) self.max_count = attributes.delete(:max_count) From 89d71ab9632ba2b691f15605cf8f7a07abb75ada Mon Sep 17 00:00:00 2001 From: Paul Thornthwaite Date: Wed, 19 Dec 2012 14:58:15 +0000 Subject: [PATCH 5/6] [openstack] Replaces #new_record? with #persisted? --- lib/fog/openstack/models/compute/address.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fog/openstack/models/compute/address.rb b/lib/fog/openstack/models/compute/address.rb index f499255b5..631e79bc9 100644 --- a/lib/fog/openstack/models/compute/address.rb +++ b/lib/fog/openstack/models/compute/address.rb @@ -14,7 +14,7 @@ module Fog attribute :instance_id def initialize(attributes = {}) - # assign server first to prevent race condition with new_record? + # assign server first to prevent race condition with persisted? self.server = attributes.delete(:server) super end @@ -47,7 +47,7 @@ module Fog private def associate(new_server) - if new_record? + unless persisted? @server = new_server else @server = nil @@ -58,7 +58,7 @@ module Fog def disassociate @server = nil - unless new_record? + if persisted? connection.disassociate_address(instance_id, ip) end self.instance_id = nil From b0698648dca63241ae6a83614dec412a4a9d03c7 Mon Sep 17 00:00:00 2001 From: Paul Thornthwaite Date: Wed, 19 Dec 2012 15:00:26 +0000 Subject: [PATCH 6/6] [vcloud] Replaces #new_record? with #persisted? --- lib/fog/vcloud/models/compute/server.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/vcloud/models/compute/server.rb b/lib/fog/vcloud/models/compute/server.rb index 48c80e391..fc4c4f61a 100644 --- a/lib/fog/vcloud/models/compute/server.rb +++ b/lib/fog/vcloud/models/compute/server.rb @@ -175,7 +175,7 @@ module Fog end def save - if new_record? + unless persisted? #Lame ... raise RuntimeError, "Should not be here" else