Browse Source

Revert "Merge branch 'master' of github.com:Shopify/shopify_api"

This reverts commit 855fe5d8f95bb7f4361f2c5b0d85e9ab1710aebd, reversing
changes made to cbe5b167977e32e2793fd4db83796b5e411028c2.
Gareth du Plooy 12 years ago
parent
commit
0769cdd5ec
2 changed files with 34 additions and 25 deletions
  1. 34 11
      lib/shopify_api/json_format.rb
  2. 0 14
      lib/shopify_api/resources/base.rb

+ 34 - 11
lib/shopify_api/json_format.rb

@@ -1,16 +1,39 @@
 module ActiveResource
-  if ActiveResource::VERSION::STRING =~ /^3\.0\./
-    module Formats
-      module JsonFormat
-        def decode(json)
-          data = ActiveSupport::JSON.decode(json)
-          if data.is_a?(Hash) && data.keys.size == 1
-            data.values.first
-          else
-            data
-          end
+  class Base
+    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)
+    end
+  end
+
+  module Formats
+    module JsonFormat
+      def decode(json)
+        data = ActiveSupport::JSON.decode(json)
+        if data.is_a?(Hash) && data.keys.size == 1
+          data.values.first
+        else
+          data
+        end
+      end
+    end
+  end
+end
+
+module ActiveModel
+  module Serializers
+    module JSON
+      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
     end
   end
-end
+end

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

@@ -34,23 +34,9 @@ module ShopifyAPI
       !id.nil?
     end
 
-    def encode(options = {})
-      obj = dup
-
-      if self.class.format.extension == 'json' && !already_has_root_element?(obj.attributes)
-        obj.attributes = {self.class.element_name => obj.attributes}
-      end
-
-      obj.send("to_#{self.class.format.extension}", options)
-    end
     private
     def only_id
       encode(:only => :id, :include => [], :methods => [])
     end
-
-    def already_has_root_element?(attributes)
-      attributes.keys.size == 1 && attributes.values.first.kind_of?(Hash)
-    end
   end
 end
-