api_guard icon indicating copy to clipboard operation
api_guard copied to clipboard

How to map to Users::User?

Open joemasilotti opened this issue 4 years ago • 3 comments

Our app scopes our user model to Users::User. How can we configure API Guard's routes to work with this? (We are also using Devise.)

This seems to be the only combination of for:, as:, and path: options that doesn't raise an error when starting the server. But when POSTing to /api/users/sign_in I get the following error.

NameError (`@api_guard_Users::User' is not allowed as an instance variable name)

  # API Guard Routes
  scope path: :api do
    api_guard_routes for: "Users::User", as: "users", path: "users", controller: {
      authentication: "users/authentication"
    }
  end

  # Devise / Auth Routes
  devise_for :users, class_name: "Users::User", controllers: {
    sessions: "users/sessions",
    registrations: "users/registrations",
    masquerades: "users/admin/masquerades"
  }

I'm assuming (hoping!) this can be fixed in the route configuration. Any help would be greatly appreciated!

joemasilotti avatar Mar 22 '21 16:03 joemasilotti

FWIW, hardcoding the instance variable does work.

---
 lib/api_guard/resource_mapper.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/api_guard/resource_mapper.rb b/lib/api_guard/resource_mapper.rb
index ad4ed04..b320b45 100644
--- a/lib/api_guard/resource_mapper.rb
+++ b/lib/api_guard/resource_mapper.rb
@@ -7,7 +7,7 @@ class ResourceMapper
     def initialize(routes_for, class_name)
       @resource_name = routes_for.singularize
       @resource_class = class_name.constantize
-      @resource_instance_name = "@api_guard_#{routes_for}"
+      @resource_instance_name = "@api_guard_user"
     end
   end

https://github.com/jeanpaulsio/api_guard/commit/358a34f2b1a60220814837fa9b37d3835f9ded34#diff-d35c3a8e79baffe631963359f9cca892b4ed3dc713a84f7e13c019b7ee1291f6R10

joemasilotti avatar Mar 22 '21 19:03 joemasilotti

@joemasilotti Thanks for reporting. We need an option to specify the class name in the route configuration as available in devise to support class with namespace. I will work on this.

Gokul595 avatar Mar 23 '21 05:03 Gokul595

Amazing, thank you!

joemasilotti avatar Mar 23 '21 20:03 joemasilotti