Skip to content

Commit d19e73e

Browse files
Merge pull request #91 from graphql-devise/generators-use-our-concerns
Generator. Use our modules, change defaults
2 parents 1076191 + 988e318 commit d19e73e

6 files changed

Lines changed: 97 additions & 90 deletions

File tree

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
--format documentation
22
--color
3+
--order random

.travis.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ jobs:
4747
rvm: 2.7.1
4848
- gemfile: gemfiles/rails6.0_graphql_edge.gemfile
4949
rvm: 2.6.6
50-
env: SKIP_COVERALLS=true
50+
env:
51+
- SKIP_COVERALLS=true
52+
- EAGER_LOAD=true
5153
- gemfile: gemfiles/rails6.0_graphql_edge.gemfile
5254
rvm: 2.7.1
53-
env: SKIP_COVERALLS=true
55+
env:
56+
- SKIP_COVERALLS=true
57+
- EAGER_LOAD=true
5458
- gemfile: gemfiles/rails_edge_graphql_edge.gemfile
5559
rvm: 2.7.1
56-
env: SKIP_COVERALLS=true
60+
env:
61+
- SKIP_COVERALLS=true
62+
- EAGER_LOAD=true
5763
exclude:
5864
- gemfile: gemfiles/rails4.2_graphql1.8.gemfile
5965
rvm: 2.7.1

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,12 @@ class User < ApplicationRecord
193193
:confirmable
194194

195195
# including after calling the `devise` method is important.
196-
# include DeviseTokenAuth::Concerns::User # is also valid (generator includes this one).
197196
include GraphqlDevise::Concerns::Model
198197
end
199198
```
200199

201200
The install generator can do this for you if you specify the `user_class` option.
202201
See [Installation](#installation) for details.
203-
The generator will include a different module in your model, `DeviseTokenAuth::Concerns::User` which is also correct,
204-
we just made an alias on our namespace for consistency and possible extension.
205-
Generators have to be updated to generate our module.
206202

207203
### Customizing Email Templates
208204
The approach of this gem is a bit different from DeviseTokenAuth. We have placed our templates in `app/views/graphql_devise/mailer`,
@@ -226,7 +222,6 @@ In our example our model is `User`, so it would look like this:
226222
# app/controllers/my_controller.rb
227223

228224
class MyController < ApplicationController
229-
# include DeviseTokenAuth::Concerns::SetUserByToken # is also valid (generator includes this one).
230225
include GraphqlDevise::Concerns::SetUserByToken
231226

232227
before_action :authenticate_user!
@@ -239,9 +234,6 @@ end
239234

240235
The install generator can do this for you because it executes DTA installer.
241236
See [Installation](#Installation) for details.
242-
The generator will include a different module in your model, `DeviseTokenAuth::Concerns::SetUserByToken` which is also correct,
243-
we just made an alias on our namespace for consistency and possible extension.
244-
Generators have to be updated to generate our module.
245237

246238
### Making Requests
247239
Here is a list of the available mutations and queries assuming your mounted model is `User`.
@@ -290,10 +282,10 @@ as comments. You can also use
290282
**[DTA's docs](https://devise-token-auth.gitbook.io/devise-token-auth/config/initialization)** as a reference.
291283
In this section the most important configurations will be highlighted.
292284

293-
- **change_headers_on_each_request:** This configurations defaults to `true`. This means that tokens will change on
294-
each request you make, and the new values will be returned in the headers. So your client needs to handle this.
295-
Setting this to `false` will allow you to store the credentials for as long as the token life_span permits. And
296-
you can send the same credentials in each request.
285+
- **change_headers_on_each_request:** This configurations defaults to `false`. This will allow you to store the
286+
credentials for as long as the token life_span permits. And you can send the same credentials in each request.
287+
Setting this to `true` means that tokens will change on each request you make, and the new values will be returned
288+
in the headers. So your client needs to handle this.
297289
- **batch_request_buffer_throttle:** When change_headers_on_each_request is set to true, you might still want your
298290
credentials to be valid more than once as you might send parallel request. The duration you set here will
299291
determine how long the same credentials work after the first request is received.

lib/generators/graphql_devise/install_generator.rb

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,67 @@ def execute_devise_installer
1010
end
1111

1212
def execute_dta_installer
13+
# Necessary in case of a re-run of the generator, for DTA to detect concerns already included
14+
if File.exist?(File.expand_path("app/models/#{user_class.underscore}.rb", destination_root))
15+
gsub_file(
16+
"app/models/#{user_class.underscore}.rb",
17+
'GraphqlDevise::Concerns::Model',
18+
'DeviseTokenAuth::Concerns::User'
19+
)
20+
end
21+
gsub_file(
22+
'app/controllers/application_controller.rb',
23+
'GraphqlDevise::Concerns::SetUserByToken',
24+
'DeviseTokenAuth::Concerns::SetUserByToken'
25+
)
26+
1327
generate 'devise_token_auth:install', "#{user_class} #{mount_path}"
1428
end
1529

1630
def mount_resource_route
1731
routes_file = 'config/routes.rb'
18-
routes_path = File.join(destination_root, routes_file)
19-
gem_helper = 'mount_graphql_devise_for'
20-
gem_route = "#{gem_helper} '#{user_class}', at: '#{mount_path}'"
32+
gem_route = "mount_graphql_devise_for '#{user_class}', at: '#{mount_path}'"
2133
dta_route = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
22-
file_start = 'Rails.application.routes.draw do'
23-
24-
if File.exist?(routes_path)
25-
current_route = parse_file_for_line(routes_path, gem_route)
2634

27-
if current_route.present?
28-
say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
29-
else
30-
current_dta_route = parse_file_for_line(routes_path, dta_route)
35+
if file_contains_str?(routes_file, gem_route)
36+
gsub_file(routes_file, /^\s+#{Regexp.escape(dta_route + "\n")}/i, '')
3137

32-
if current_dta_route.present?
33-
replace_line(routes_path, dta_route, gem_route)
34-
else
35-
insert_text_after_line(routes_path, file_start, gem_route)
36-
end
37-
end
38+
say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
3839
else
39-
say_status('skipped', "#{routes_file} not found. Add \"#{gem_route}\" to your routes file.")
40+
gsub_file(routes_file, /#{Regexp.escape(dta_route)}/i, gem_route)
4041
end
4142
end
4243

43-
private
44+
def replace_model_concern
45+
gsub_file(
46+
"app/models/#{user_class.underscore}.rb",
47+
/^\s+include DeviseTokenAuth::Concerns::User/,
48+
' include GraphqlDevise::Concerns::Model'
49+
)
50+
end
4451

45-
def insert_text_after_line(filename, line, str)
46-
gsub_file(filename, /(#{Regexp.escape(line)})/mi) do |match|
47-
"#{match}\n #{str}"
48-
end
52+
def replace_controller_concern
53+
gsub_file(
54+
'app/controllers/application_controller.rb',
55+
/^\s+include DeviseTokenAuth::Concerns::SetUserByToken/,
56+
' include GraphqlDevise::Concerns::SetUserByToken'
57+
)
4958
end
5059

51-
def replace_line(filename, old_line, new_line)
52-
gsub_file(filename, /(#{Regexp.escape(old_line)})/mi, " #{new_line}")
60+
def set_change_headers_on_each_request_false
61+
gsub_file(
62+
'config/initializers/devise_token_auth.rb',
63+
'# config.change_headers_on_each_request = true',
64+
'config.change_headers_on_each_request = false'
65+
)
5366
end
5467

55-
def parse_file_for_line(filename, str)
56-
match = false
68+
private
69+
70+
def file_contains_str?(filename, regex_str)
71+
path = File.join(destination_root, filename)
5772

58-
File.open(filename) do |f|
59-
f.each_line do |line|
60-
match = line if line =~ /(#{Regexp.escape(str)})/mi
61-
end
62-
end
63-
match
73+
File.read(path) =~ /(#{Regexp.escape(regex_str)})/i
6474
end
6575
end
6676
end

spec/fixtures/rails

Lines changed: 0 additions & 7 deletions
This file was deleted.

spec/generators/graphql_devise/install_generator_spec.rb

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,61 @@
33
require 'generators/graphql_devise/install_generator'
44

55
RSpec.describe GraphqlDevise::InstallGenerator, type: :generator do
6-
destination File.expand_path('../../../tmp/dummy', __dir__)
6+
destination File.expand_path('../../../../gqld_dummy', __dir__)
7+
8+
let(:routes_path) { "#{destination_root}/config/routes.rb" }
9+
let(:routes_content) { File.read(routes_path) }
10+
let(:dta_route) { 'mount_devise_token_auth_for' }
11+
12+
after(:all) { FileUtils.rm_rf(destination_root) }
713

814
before do
915
prepare_destination
10-
copy_rails_bin
16+
create_rails_project
17+
run_generator(args)
1118
end
1219

13-
let(:routes_path) { "#{destination_root}/config/routes.rb" }
14-
let(:routes_content) { File.read(routes_path) }
15-
let(:dta_route) { "mount_devise_token_auth_for 'User', at: 'auth'" }
16-
17-
context 'when the file exists' do
18-
before do
19-
create_file_with_content(
20-
routes_path,
21-
"Rails.application.routes.draw do\n#{dta_route}\nend"
22-
)
23-
end
20+
context 'when passing no params to the generator' do
21+
let(:args) { [] }
2422

25-
context 'when passing no params to the generator' do
26-
before { run_generator }
23+
it 'creates and updated required files' do
24+
assert_file 'config/routes.rb', /^\s{2}mount_graphql_devise_for 'User', at: 'auth'/
25+
expect(routes_content).not_to match(dta_route)
2726

28-
it 'replaces dta route using the default values for class and path' do
29-
generator_added_route = / mount_graphql_devise_for 'User', at: 'auth'/
30-
expect(routes_content).to match(generator_added_route)
31-
expect(routes_content).not_to match(dta_route)
32-
end
33-
end
27+
assert_file 'config/initializers/devise.rb'
28+
assert_file 'config/initializers/devise_token_auth.rb', /^\s{2}#{Regexp.escape('config.change_headers_on_each_request = false')}/
29+
assert_file 'config/locales/devise.en.yml'
30+
31+
assert_migration 'db/migrate/devise_token_auth_create_users.rb'
3432

35-
context 'when passing custom params to the generator' do
36-
before { run_generator %w[Admin api] }
33+
assert_file 'app/models/user.rb', /^\s{2}devise :.+include GraphqlDevise::Concerns::Model/m
3734

38-
it 'add the routes using the provided values for class and path and keeps dta route' do
39-
generator_added_route = / mount_graphql_devise_for 'Admin', at: 'api'/
40-
expect(routes_content).to match(generator_added_route)
41-
expect(routes_content).to match(dta_route)
42-
end
35+
assert_file 'app/controllers/application_controller.rb', /^\s{2}include GraphqlDevise::Concerns::SetUserByToken/
4336
end
4437
end
4538

46-
context 'when file does *NOT* exist' do
47-
before { run_generator }
39+
context 'when passing custom params to the generator' do
40+
let(:args) { %w[Admin api] }
41+
42+
it 'creates and updated required files' do
43+
assert_file 'config/routes.rb', /^\s{2}mount_graphql_devise_for 'Admin', at: 'api'/
44+
expect(routes_content).not_to match(dta_route)
45+
46+
assert_file 'config/initializers/devise.rb'
47+
assert_file 'config/initializers/devise_token_auth.rb', /^\s{2}#{Regexp.escape('config.change_headers_on_each_request = false')}/
48+
assert_file 'config/locales/devise.en.yml'
4849

49-
it 'does *NOT* create the file and throw no exception' do
50-
expect(File).not_to exist(routes_path)
50+
assert_migration 'db/migrate/devise_token_auth_create_admins.rb'
51+
52+
assert_file 'app/models/admin.rb', /^\s{2}devise :.+include GraphqlDevise::Concerns::Model/m
53+
54+
assert_file 'app/controllers/application_controller.rb', /^\s{2}include GraphqlDevise::Concerns::SetUserByToken/
5155
end
5256
end
5357

54-
def copy_rails_bin
55-
FileUtils.mkdir_p(File.join(destination_root, 'bin'))
56-
FileUtils.copy_file('spec/fixtures/rails', File.join(destination_root, 'bin/rails'))
58+
def create_rails_project
59+
FileUtils.cd(File.join(destination_root, '..')) do
60+
`rails new gqld_dummy -S -C --skip-action-mailbox --skip-action-text -T --skip-spring --skip-bundle --skip-keeps -G --skip-active-storage -J --skip-listen --skip-bootsnap`
61+
end
5762
end
5863
end

0 commit comments

Comments
 (0)