Browse Source

Merge pull request #69 from Shopify/bugfix/encode_decode_activeresource_versions

Bugfix/encode decode activeresource versions
Neil Costford 11 years ago
parent
commit
a291fc837f
3 changed files with 18 additions and 16 deletions
  1. 3 6
      CHANGELOG
  2. 14 9
      lib/shopify_api/resources/base.rb
  3. 1 1
      lib/shopify_api/version.rb

+ 3 - 6
CHANGELOG

@@ -1,14 +1,11 @@
-== Version 3.1.1
+== Version 3.1.2
 
 * Includes port in domain URI (when other than http/80 or https/443)
 * Adds access to CustomerSavedSearch
 * Adds resources: Order::DefaultAddress, Client::ClientDetails, Announcement
 * Allows access to Articles without a blog_id
-* Qualifies and scopes encode/decode changes in ActiveResource 3.0, 3.1, 3.2, 4.0
-
-== Version 3.1.0
-
-* Expose the `order` action in SmartCollection for general use
+* Moves encode and as_json overrides to ShopifyAPI::Base scope
+* Exposes the `order` action in SmartCollection for general use
 
 == Version 3.0.3
 

+ 14 - 9
lib/shopify_api/resources/base.rb

@@ -3,20 +3,25 @@ require 'shopify_api/version'
 module ShopifyAPI
   class Base < ActiveResource::Base
     extend Countable
-    self.include_root_in_json = true
+    self.include_root_in_json = false
     self.headers['User-Agent'] = ["ShopifyAPI/#{ShopifyAPI::VERSION}",
                                   "ActiveResource/#{ActiveResource::VERSION::STRING}",
                                   "Ruby/#{RUBY_VERSION}"].join(' ')
 
-    # this patch only needed until 3.1 because ActiveResouce 3.2 passes :root to as_json (and encode regardless of include_root_in_json)
-    if ActiveResource::VERSION::MAJOR == 3 && ActiveResource::VERSION::MINOR <= 1
-      self.include_root_in_json = false
+    def encode(options = {})
+      same = dup
+      same.attributes = {self.class.element_name => same.attributes} if self.class.format.extension == 'json'
 
-      def encode(options = {})
-        same = dup
-        same.attributes = {self.class.element_name => same.attributes} if self.class.format.extension == 'json'
-
-        same.send("to_#{self.class.format.extension}", options)
+      same.send("to_#{self.class.format.extension}", options)
+    end
+    
+    def as_json(options = nil)
+      root = options[:root] if options.try(:key?, :root)
+      if include_root_in_json
+        root = self.class.model_name.element if root == true
+        { root => serializable_hash(options) }
+      else
+        serializable_hash(options)
       end
     end
 

+ 1 - 1
lib/shopify_api/version.rb

@@ -1,3 +1,3 @@
 module ShopifyAPI
-  VERSION = "3.1.1"
+  VERSION = "3.1.2"
 end