Skip to content

Commit 75ed584

Browse files
Merge pull request #70 from graphql-devise/fix-namespaced-models-bug
Fix mounting models inside another module
2 parents 50a704d + 2a4e7c2 commit 75ed584

9 files changed

Lines changed: 91 additions & 6 deletions

File tree

graphql_devise.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
3535
spec.add_development_dependency 'faker'
3636
spec.add_development_dependency 'generator_spec'
3737
spec.add_development_dependency 'github_changelog_generator'
38-
spec.add_development_dependency 'pry'
38+
spec.add_development_dependency 'pry', '~> 0.12.2'
3939
spec.add_development_dependency 'pry-byebug'
4040
spec.add_development_dependency 'rake', '~> 10.0'
4141
spec.add_development_dependency 'rspec-rails'

lib/graphql_devise/rails/routes.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ def mount_graphql_devise_for(resource, opts = {})
3737

3838
devise_for(
3939
resource.pluralize.underscore.tr('/', '_').to_sym,
40-
module: :devise,
41-
skip: [:sessions, :registrations, :passwords, :confirmations, :omniauth_callbacks, :unlocks, :invitations]
40+
module: :devise,
41+
class_name: resource,
42+
skip: [:sessions, :registrations, :passwords, :confirmations, :omniauth_callbacks, :unlocks, :invitations]
4243
)
4344

4445
authenticatable_type = opts[:authenticatable_type] ||
@@ -55,7 +56,7 @@ def mount_graphql_devise_for(resource, opts = {})
5556
custom_operations[action]
5657
else
5758
new_mutation = Class.new(mutation)
58-
new_mutation.graphql_name("#{resource}#{action.to_s.camelize(:upper)}")
59+
new_mutation.graphql_name("#{resource.gsub('::', '')}#{action.to_s.camelize(:upper)}")
5960
new_mutation.field(:authenticatable, authenticatable_type, null: true)
6061

6162
new_mutation
@@ -83,7 +84,7 @@ def mount_graphql_devise_for(resource, opts = {})
8384
custom_operations[action]
8485
else
8586
new_query = Class.new(query)
86-
new_query.graphql_name("#{resource}#{action.to_s.camelize(:upper)}")
87+
new_query.graphql_name("#{resource.gsub('::', '')}#{action.to_s.camelize(:upper)}")
8788
new_query.type(authenticatable_type, null: true)
8889

8990
new_query

spec/dummy/app/models/users.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Users
2+
def self.table_name_prefix
3+
'users_'
4+
end
5+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Users
2+
class Customer < ApplicationRecord
3+
devise :database_authenticatable, :validatable
4+
5+
include GraphqlDevise::Concerns::Model
6+
7+
validates :name, presence: true
8+
end
9+
end

spec/dummy/config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@
2121
at: '/api/v1/guest/graphql_auth'
2222
)
2323

24+
mount_graphql_devise_for(
25+
'Users::Customer',
26+
only: [:login],
27+
at: '/api/v1/user_customer/graphql_auth'
28+
)
29+
2430
post '/api/v1/graphql', to: 'api/v1/graphql#graphql'
2531
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class CreateUsersCustomers < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :users_customers do |t|
4+
## Required
5+
t.string :provider, null: false, default: 'email'
6+
t.string :uid, null: false, default: ''
7+
8+
## Database authenticatable
9+
t.string :encrypted_password, null: false, default: ''
10+
11+
## User Info
12+
t.string :email
13+
14+
## Tokens
15+
t.text :tokens
16+
17+
t.string :name, null: false
18+
19+
t.timestamps
20+
end
21+
end
22+
end

spec/dummy/db/schema.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_10_13_213045) do
13+
ActiveRecord::Schema.define(version: 2020_03_21_121807) do
1414

1515
create_table "admins", force: :cascade do |t|
1616
t.string "provider", default: "email", null: false
@@ -87,4 +87,15 @@
8787
t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
8888
end
8989

90+
create_table "users_customers", force: :cascade do |t|
91+
t.string "provider", default: "email", null: false
92+
t.string "uid", default: "", null: false
93+
t.string "encrypted_password", default: "", null: false
94+
t.string "email"
95+
t.text "tokens"
96+
t.string "name", null: false
97+
t.datetime "created_at", precision: 6, null: false
98+
t.datetime "updated_at", precision: 6, null: false
99+
end
100+
90101
end

spec/factories/users_customers.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :users_customer, class: 'Users::Customer' do
3+
name { Faker::FunnyName.two_word_name }
4+
email { Faker::Internet.unique.email }
5+
password { Faker::Internet.password }
6+
end
7+
end

spec/requests/mutations/login_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,28 @@
146146
)
147147
end
148148
end
149+
150+
context 'when using the Users::Customer model' do
151+
let(:customer) { create(:users_customer, password: password) }
152+
let(:query) do
153+
<<-GRAPHQL
154+
mutation {
155+
usersCustomerLogin(
156+
email: "#{customer.email}",
157+
password: "#{password}"
158+
) {
159+
authenticatable { email }
160+
}
161+
}
162+
GRAPHQL
163+
end
164+
165+
before { post_request }
166+
167+
it 'works alongside the user mount point' do
168+
expect(json_response[:data][:usersCustomerLogin]).to include(
169+
authenticatable: { email: customer.email }
170+
)
171+
end
172+
end
149173
end

0 commit comments

Comments
 (0)