Browse Source

Fixed double root bug with ActiveSupport 3.2.0

Dennis O'Connor 13 years ago
parent
commit
f474f3d7b9

+ 17 - 1
lib/shopify_api/json_format.rb

@@ -3,7 +3,7 @@ module ActiveResource
     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
@@ -21,3 +21,19 @@ module ActiveResource
     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

+ 8 - 0
test/blog_test.rb

@@ -0,0 +1,8 @@
+require 'test_helper'
+class BlogTest < Test::Unit::TestCase
+  test "blog creation" do
+    fake "blogs", :method => :post, :status => 202
+    ShopifyAPI::Blog.create(:title => "Test Blog")
+    assert_equal '{"blog":{"title":"Test Blog"}}', FakeWeb.last_request.body
+  end
+end

+ 1 - 1
test/detailed_log_subscriber_test.rb

@@ -27,7 +27,7 @@ class LogSubscriberTest < Test::Unit::TestCase
     assert_equal "GET http://localhost:80/admin/pages/1.json",                  @logger.logged(:info)[0]
     assert_match /\-\-\> 200/,                                                  @logger.logged(:info)[1]
     assert_equal "Headers: {\"Accept\"=>\"application/json\"}",                 @logger.logged(:info)[2]
-    assert_equal "Response:\n{\"page\":{\"title\":\"Shopify API\",\"id\":1}}",  @logger.logged(:info)[3]
+    assert_equal "Response:\n{\"page\":{\"id\":1,\"title\":\"Shopify API\"}}",  @logger.logged(:info)[3]
   end
 
   test "logging on #find with an error" do

+ 13 - 0
test/fixtures/blogs.json

@@ -0,0 +1,13 @@
+{
+  "blog": {
+    "handle": "test-blog",
+    "created_at": "2012-01-10T17:45:19-05:00",
+    "title": "Test Blog",
+    "template_suffix": null,
+    "updated_at": "2012-01-10T17:45:19-05:00",
+    "feedburner_location": null,
+    "id": 1008414260,
+    "feedburner": null,
+    "commentable": "no"
+  }
+}

+ 1 - 1
test/product_test.rb

@@ -12,7 +12,7 @@ class ProductTest < Test::Unit::TestCase
     fake "products/632910392/metafields", :method => :post, :status => 201, :body => load_fixture('metafield')
 
     field = @product.add_metafield(ShopifyAPI::Metafield.new(:namespace => "contact", :key => "email", :value => "123@example.com", :value_type => "string"))
-
+    assert_equal '{"metafield":{"namespace":"contact","key":"email","value":"123@example.com","value_type":"string"}}', FakeWeb.last_request.body
     assert !field.new_record?
     assert_equal "contact", field.namespace
     assert_equal "email", field.key

+ 1 - 1
test/shop_test.rb

@@ -29,7 +29,7 @@ class ShopTest < Test::Unit::TestCase
     fake "metafields", :method => :post, :status => 201, :body =>load_fixture('metafield')
 
     field = @shop.add_metafield(ShopifyAPI::Metafield.new(:namespace => "contact", :key => "email", :value => "123@example.com", :value_type => "string"))
-
+    assert_equal '{"metafield":{"namespace":"contact","key":"email","value":"123@example.com","value_type":"string"}}', FakeWeb.last_request.body
     assert !field.new_record?
     assert_equal "contact", field.namespace
     assert_equal "email", field.key