Browse Source

Fix missing parent id/resource id on nested resources

Maarten van Grootel 11 years ago
parent
commit
d48853d495

+ 6 - 0
lib/active_resource/disable_prefix_check.rb

@@ -17,6 +17,12 @@ module DisablePrefixCheck
 
         options[resource_id].nil? ? "/admin/" : "/admin/#{resource_type}/#{options[resource_id]}/"
       end
+
+      define_singleton_method :instantiate_record do |record, prefix_options = {}|
+        new(record, true).tap do |resource|
+          resource.prefix_options = prefix_options unless prefix_options.blank?
+        end
+      end
     end
   end
 end

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

@@ -52,6 +52,10 @@ module ShopifyAPI
 
       def init_prefix_explicit(resource_type, resource_id)
         self.prefix = "/admin/#{resource_type}/:#{resource_id}/"
+
+        define_method resource_id.to_sym do
+          @prefix_options[resource_id]
+        end
       end
     end
 

+ 4 - 0
test/article_test.rb

@@ -6,24 +6,28 @@ class ArticleTest < Test::Unit::TestCase
     fake "articles/6242736", :method => :get, :body => load_fixture('article')
     article = ShopifyAPI::Article.find(6242736)
     assert_equal "First Post", article.title
+    assert_equal 1008414260, article.blog_id
   end
 
   def test_get_articles
     fake "articles", :method => :get, :body => load_fixture('articles')
     articles = ShopifyAPI::Article.all
     assert_equal 3, articles.length
+    assert_equal 1008414260, articles.first.blog_id
   end
 
   def test_get_articles_namespaced
     fake "blogs/1008414260/articles", :method => :get, :body => load_fixture('articles')
     articles = ShopifyAPI::Article.find(:all, :params => {:blog_id => 1008414260})
     assert_equal 3, articles.length
+    assert_equal 1008414260, articles.first.blog_id
   end
 
   def test_get_article_namespaced
     fake "blogs/1008414260/articles/6242736", :method => :get, :body => load_fixture('article')
     article = ShopifyAPI::Article.find(6242736, :params => {:blog_id => 1008414260})
     assert_equal "First Post", article.title
+    assert_equal 1008414260, article.blog_id
   end
 
   def test_get_authors

+ 2 - 1
test/fixtures/variant.json

@@ -17,6 +17,7 @@
     "option1": "Pink",
     "option2": null,
     "fulfillment_service": "manual",
-    "option3": null
+    "option3": null,
+    "product_id": 632910392
   }
 }

+ 8 - 4
test/fixtures/variants.json

@@ -18,7 +18,8 @@
       "option1": "Pink",
       "option2": null,
       "fulfillment_service": "manual",
-      "option3": null
+      "option3": null,
+      "product_id": 632910392
     },
     {
       "price": "199.00",
@@ -38,7 +39,8 @@
       "option1": "Red",
       "option2": null,
       "fulfillment_service": "manual",
-      "option3": null
+      "option3": null,
+      "product_id": 632910392
     },
     {
       "price": "199.00",
@@ -58,7 +60,8 @@
       "option1": "Green",
       "option2": null,
       "fulfillment_service": "manual",
-      "option3": null
+      "option3": null,
+      "product_id": 632910392
     },
     {
       "price": "199.00",
@@ -78,7 +81,8 @@
       "option1": "Black",
       "option2": null,
       "fulfillment_service": "manual",
-      "option3": null
+      "option3": null,
+      "product_id": 632910392
     }
   ]
 }

+ 14 - 0
test/variant_test.rb

@@ -19,4 +19,18 @@ class VariantTest < Test::Unit::TestCase
 
     v = ShopifyAPI::Variant.find(808950810)
   end
+
+  def test_product_id_should_be_accessible_if_via_product_endpoint
+    fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
+
+    v = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
+    assert_equal 632910392, v.product_id
+  end
+
+  def test_product_id_should_be_accessible_if_via_variant_endpoint
+    fake "variants/808950810", :method => :get, :body => load_fixture('variant')
+
+    v = ShopifyAPI::Variant.find(808950810)
+    assert_equal 632910392, v.product_id
+  end
 end