Skip to content

Commit dbed72d

Browse files
committed
mount method receives model constant instead of String
1 parent 29bcb0e commit dbed72d

7 files changed

Lines changed: 23 additions & 28 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Will do the following:
8282
- Add `devise` modules to `Admin` model
8383
- Other changes that you can find [here](https://devise-token-auth.gitbook.io/devise-token-auth/config)
8484
- Add the route to `config/routes.rb`
85-
- `mount_graphql_devise_for 'Admin', at: 'api/auth'`
85+
- `mount_graphql_devise_for Admin, at: 'api/auth'`
8686

8787
`Admin` could be any model name you are going to be using for authentication,
8888
and `api/auth` could be any mount path you would like to use for auth.
@@ -102,7 +102,7 @@ You can mount this gem's GraphQL auth schema in your routes file like this:
102102

103103
Rails.application.routes.draw do
104104
mount_graphql_devise_for(
105-
'User',
105+
User,
106106
at: 'api/v1',
107107
authenticatable_type: Types::MyCustomUserType,
108108
operations: {

app/controllers/graphql_devise/concerns/set_user_by_token.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Concerns
33
SetUserByToken = DeviseTokenAuth::Concerns::SetUserByToken
44

55
SetUserByToken.module_eval do
6+
attr_accessor :client_id, :token, :resource
7+
68
def build_redirect_headers(access_token, client, redirect_header_options = {})
79
{
810
DeviseTokenAuth.headers_names[:"access-token"] => access_token,
@@ -12,6 +14,12 @@ def build_redirect_headers(access_token, client, redirect_header_options = {})
1214
:token => access_token
1315
}.merge(redirect_header_options)
1416
end
17+
18+
private
19+
20+
def resource_class(mapping)
21+
GraphqlDevise.mapping_by_name(mapping)
22+
end
1523
end
1624
end
1725
end

lib/generators/graphql_devise/install_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def execute_dta_installer
2929

3030
def mount_resource_route
3131
routes_file = 'config/routes.rb'
32-
gem_route = "mount_graphql_devise_for '#{user_class}', at: '#{mount_path}'"
32+
gem_route = "mount_graphql_devise_for #{user_class}, at: '#{mount_path}'"
3333
dta_route = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
3434

3535
if file_contains_str?(routes_file, gem_route)

lib/graphql_devise.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def self.resource_mounted?(resource)
2323
end
2424

2525
def self.add_mapping(name, klass)
26+
Devise.add_mapping(
27+
name.to_s.pluralize.to_sym,
28+
module: :devise, class_name: klass.to_s
29+
)
2630
@mappings[name] = klass
2731
end
2832

lib/graphql_devise/rails/routes.rb

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
module ActionDispatch::Routing
22
class Mapper
3-
DEVISE_OPERATIONS = [
4-
:sessions,
5-
:registrations,
6-
:passwords,
7-
:confirmations,
8-
:omniauth_callbacks,
9-
:unlocks,
10-
:invitations
11-
].freeze
12-
133
def mount_graphql_devise_for(resource, options = {})
144
default_operations = GraphqlDevise::DefaultOperations::MUTATIONS.merge(GraphqlDevise::DefaultOperations::QUERIES)
15-
mapping_name = resource.underscore.tr('/', '_').to_sym
5+
mapping_name = resource.to_s.underscore.tr('/', '_').to_sym
166

177
# clean_options responds to all keys defined in GraphqlDevise::MountMethod::SUPPORTED_OPTIONS
188
clean_options = GraphqlDevise::MountMethod::OptionSanitizer.new(options).call!
@@ -26,13 +16,6 @@ def mount_graphql_devise_for(resource, options = {})
2616
]
2717
).validate!
2818

29-
devise_for(
30-
resource.pluralize.underscore.tr('/', '_').to_sym,
31-
module: :devise,
32-
class_name: resource,
33-
skip: DEVISE_OPERATIONS
34-
)
35-
3619
post clean_options.at, to: 'graphql_devise/graphql#auth'
3720
get clean_options.at, to: 'graphql_devise/graphql#auth'
3821

@@ -71,7 +54,7 @@ def mount_graphql_devise_for(resource, options = {})
7154
GraphqlDevise::Types::QueryType.field(action, resolver: resolver)
7255
end
7356

74-
GraphqlDevise.add_mapping(mapping_name, resource.constantize)
57+
GraphqlDevise.add_mapping(mapping_name, resource)
7558

7659
Devise.mailer.helper(GraphqlDevise::MailerHelper)
7760
end

spec/dummy/config/routes.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Rails.application.routes.draw do
2-
mount_graphql_devise_for 'User', at: '/api/v1/graphql_auth', operations: {
2+
mount_graphql_devise_for User, at: '/api/v1/graphql_auth', operations: {
33
login: Mutations::Login,
44
sign_up: Mutations::SignUp
55
}, additional_mutations: {
@@ -9,20 +9,20 @@
99
}
1010

1111
mount_graphql_devise_for(
12-
'Admin',
12+
Admin,
1313
authenticatable_type: Types::CustomAdminType,
1414
skip: [:sign_up, :check_password_token],
1515
at: '/api/v1/admin/graphql_auth'
1616
)
1717

1818
mount_graphql_devise_for(
19-
'Guest',
19+
Guest,
2020
only: [:login, :logout, :sign_up],
2121
at: '/api/v1/guest/graphql_auth'
2222
)
2323

2424
mount_graphql_devise_for(
25-
'Users::Customer',
25+
Users::Customer,
2626
only: [:login],
2727
at: '/api/v1/user_customer/graphql_auth'
2828
)

spec/generators/graphql_devise/install_generator_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
let(:args) { [] }
2222

2323
it 'creates and updated required files' do
24-
assert_file 'config/routes.rb', /^\s{2}mount_graphql_devise_for 'User', at: 'auth'/
24+
assert_file 'config/routes.rb', /^\s{2}mount_graphql_devise_for User, at: 'auth'/
2525
expect(routes_content).not_to match(dta_route)
2626

2727
assert_file 'config/initializers/devise.rb'
@@ -40,7 +40,7 @@
4040
let(:args) { %w[Admin api] }
4141

4242
it 'creates and updated required files' do
43-
assert_file 'config/routes.rb', /^\s{2}mount_graphql_devise_for 'Admin', at: 'api'/
43+
assert_file 'config/routes.rb', /^\s{2}mount_graphql_devise_for Admin, at: 'api'/
4444
expect(routes_content).not_to match(dta_route)
4545

4646
assert_file 'config/initializers/devise.rb'

0 commit comments

Comments
 (0)