Browse Source

override encode & as_json in ShopifyAPI::Base

Neil Costford 11 years ago
parent
commit
84bf0411f3
1 changed files with 14 additions and 9 deletions
  1. 14 9
      lib/shopify_api/resources/base.rb

+ 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