@@ -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
6676end
0 commit comments