Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphql_devise.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'faker'
spec.add_development_dependency 'generator_spec'
spec.add_development_dependency 'github_changelog_generator'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'pry', '~> 0.12.2'
spec.add_development_dependency 'pry-byebug'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec-rails'
Expand Down
9 changes: 5 additions & 4 deletions lib/graphql_devise/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def mount_graphql_devise_for(resource, opts = {})

devise_for(
resource.pluralize.underscore.tr('/', '_').to_sym,
module: :devise,
skip: [:sessions, :registrations, :passwords, :confirmations, :omniauth_callbacks, :unlocks, :invitations]
module: :devise,
class_name: resource,
skip: [:sessions, :registrations, :passwords, :confirmations, :omniauth_callbacks, :unlocks, :invitations]
)

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

new_mutation
Expand Down Expand Up @@ -83,7 +84,7 @@ def mount_graphql_devise_for(resource, opts = {})
custom_operations[action]
else
new_query = Class.new(query)
new_query.graphql_name("#{resource}#{action.to_s.camelize(:upper)}")
new_query.graphql_name("#{resource.gsub('::', '')}#{action.to_s.camelize(:upper)}")
new_query.type(authenticatable_type, null: true)

new_query
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/app/models/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Users
def self.table_name_prefix
'users_'
end
end
9 changes: 9 additions & 0 deletions spec/dummy/app/models/users/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Users
class Customer < ApplicationRecord
devise :database_authenticatable, :validatable

include GraphqlDevise::Concerns::Model

validates :name, presence: true
end
end
6 changes: 6 additions & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@
at: '/api/v1/guest/graphql_auth'
)

mount_graphql_devise_for(
'Users::Customer',
only: [:login],
at: '/api/v1/user_customer/graphql_auth'
)

post '/api/v1/graphql', to: 'api/v1/graphql#graphql'
end
22 changes: 22 additions & 0 deletions spec/dummy/db/migrate/20200321121807_create_users_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CreateUsersCustomers < ActiveRecord::Migration[6.0]
def change
create_table :users_customers do |t|
## Required
t.string :provider, null: false, default: 'email'
t.string :uid, null: false, default: ''

## Database authenticatable
t.string :encrypted_password, null: false, default: ''

## User Info
t.string :email

## Tokens
t.text :tokens

t.string :name, null: false

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_10_13_213045) do
ActiveRecord::Schema.define(version: 2020_03_21_121807) do

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

create_table "users_customers", force: :cascade do |t|
t.string "provider", default: "email", null: false
t.string "uid", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "email"
t.text "tokens"
t.string "name", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

end
7 changes: 7 additions & 0 deletions spec/factories/users_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :users_customer, class: 'Users::Customer' do
name { Faker::FunnyName.two_word_name }
email { Faker::Internet.unique.email }
password { Faker::Internet.password }
end
end
24 changes: 24 additions & 0 deletions spec/requests/mutations/login_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,28 @@
)
end
end

context 'when using the Users::Customer model' do
let(:customer) { create(:users_customer, password: password) }
let(:query) do
<<-GRAPHQL
mutation {
usersCustomerLogin(
email: "#{customer.email}",
password: "#{password}"
) {
authenticatable { email }
}
}
GRAPHQL
end

before { post_request }

it 'works alongside the user mount point' do
expect(json_response[:data][:usersCustomerLogin]).to include(
authenticatable: { email: customer.email }
)
end
end
end