Browse Source

Add errors and deprecation warnings around prefix= method.

Alex Aitken 6 years ago
parent
commit
44c08fe1c6
2 changed files with 29 additions and 4 deletions
  1. 13 2
      lib/shopify_api/resources/base.rb
  2. 16 2
      test/base_test.rb

+ 13 - 2
lib/shopify_api/resources/base.rb

@@ -101,8 +101,19 @@ module ShopifyAPI
         raise
       end
 
-      alias_method :set_prefix, :resource_prefix=
-      alias_method :prefix=, :resource_prefix=
+      def prefix=(value)
+        if value.start_with?('/admin')
+          raise ArgumentError, "'#{value}' can no longer start /admin/. Change to using resource_prefix="
+        end
+
+        warn(
+          '[DEPRECATED] ShopifyAPI::Base#prefix= is deprecated and will be removed in a future version. ' \
+            'Use `self.resource_prefix=` instead.'
+        )
+        self.resource_prefix = value
+      end
+
+      alias_method :set_prefix, :prefix=
 
       def init_prefix(resource)
         init_prefix_explicit(resource.to_s.pluralize, "#{resource}_id")

+ 16 - 2
test/base_test.rb

@@ -1,8 +1,7 @@
 require 'test_helper'
-
+require "active_support/log_subscriber/test_helper"
 
 class BaseTest < Test::Unit::TestCase
-
   def setup
     @session1 = ShopifyAPI::Session.new('shop1.myshopify.com', 'token1')
     @session2 = ShopifyAPI::Session.new('shop2.myshopify.com', 'token2')
@@ -86,6 +85,18 @@ class BaseTest < Test::Unit::TestCase
     thread.join
   end
 
+  test "prefix= will forward to resource when the value does not start with admin" do
+    TestResource.prefix = 'a/regular/path/'
+
+    assert_equal('/admin/a/regular/path/', TestResource.prefix)
+  end
+
+  test "prefix= will raise an error if value starts with with /admin" do
+    assert_raises ArgumentError do
+      TestResource.prefix = '/admin/old/prefix/structure/'
+    end
+  end
+
   if ActiveResource::VERSION::MAJOR >= 4
     test "#headers propagates changes to subclasses" do
       ShopifyAPI::Base.headers['X-Custom'] = "the value"
@@ -122,4 +133,7 @@ class BaseTest < Test::Unit::TestCase
       klass.headers.delete(header)
     end
   end
+
+  class TestResource < ShopifyAPI::Base
+  end
 end