1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #1389 from brightbox/adds_persisted

[core] Adds #persisted? to Fog models
This commit is contained in:
Paul Thornthwaite 2012-12-19 08:03:10 -08:00
commit 0786e57faa
8 changed files with 33 additions and 16 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -175,7 +175,7 @@ module Fog
end
def save
if new_record?
unless persisted?
#Lame ...
raise RuntimeError, "Should not be here"
else