[PATCH aeolus-image 1/1] Compatibility with ActiveResource >= 3.1

jstransk at redhat.com jstransk at redhat.com
Wed Aug 22 11:40:47 UTC 2012


From: Jiri Stransky <jstransk at redhat.com>

This makes aeolus-image compatible with ActiveResource 3.1 and
higher while maintaining compatibility with ActiveResource 3.0.

This patch ensures that when an Aeolus::Image::Factory::* resource is
created with :id key set in hash of parameters passed into the
constructor, ActiveResource treats it like it's an existing record, not
a one to be newly created.  When calling #save on such resource
instance, ActiveResource makes a PUT request (this is how it was with
ARes 3.0 and it's the behavior expected by ImageFactory) instead of a
POST request, which would be made if the resource was treated as newly
created.
---
 lib/aeolus_image/model/factory/base.rb | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/lib/aeolus_image/model/factory/base.rb b/lib/aeolus_image/model/factory/base.rb
index d4f8a94..11f65c8 100644
--- a/lib/aeolus_image/model/factory/base.rb
+++ b/lib/aeolus_image/model/factory/base.rb
@@ -69,6 +69,18 @@ module Aeolus
           end
         end
 
+        def initialize(attributes = {}, persisted = nil)
+          # The !! operator is required to set modified_persisted to false when
+          # persisted == nil && attributes[:id] == nil
+          modified_persisted = (persisted.nil? && attributes[:id]) ? true : !!persisted
+
+          if ActiveResource::VERSION::MAJOR >= 3 && ActiveResource::VERSION::MINOR >= 1
+            super(attributes, modified_persisted)
+          else
+            super(attributes)
+          end
+        end
+
         ## Instance Methods: (modifying the ActiveRecord::CustomMethods).
         ## This modification is same as defined in above method
         def get(method_name, options = {})
-- 
1.7.11.4




More information about the aeolus-devel mailing list