Преглед изворни кода

Avoid unnecessary monkey patching.

* Removing the root on decoding was added to ActiveResource 3.1, so
  only needs to be patched for 3.0.
* The encode patch isn't needed for ActiveModel 3.2, since ActiveResource
  passes :root to as_json, and in 3.2 :root is used even when
  include_root_in_json is false.
* The as_json patch was compensating for the encode patch for ActiveModel
  3.2, so this patch isn't necessary by avoiding patching encode for this
  version.
* The encode patch is ShopifyAPI::Base specific, since we set
  include_root_in_json to false and add the root manually. We should
  avoid side-effects for other APIs.
Dylan Smith пре 12 година
родитељ
комит
e52f30a816
2 измењених фајлова са 19 додато и 27 уклоњено
  1. 10 27
      lib/shopify_api/json_format.rb
  2. 9 0
      lib/shopify_api/resources/base.rb

+ 10 - 27
lib/shopify_api/json_format.rb

@@ -1,31 +1,14 @@
 module ActiveResource
-  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
-
-    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
-
-  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
+  if ActiveResource::VERSION::MAJOR == 3 && ActiveResource::VERSION::MINOR == 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
         end
       end
     end

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

@@ -8,6 +8,15 @@ module ShopifyAPI
                                   "ActiveResource/#{ActiveResource::VERSION::STRING}",
                                   "Ruby/#{RUBY_VERSION}"].join(' ')
 
+    if ActiveResource::VERSION::MAJOR == 3 && ActiveResource::VERSION::MINOR <= 1
+      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
+
     class << self
       def headers
         if defined?(@headers)