Class: GraphqlDevise::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/graphql_devise/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#execute_devise_installerObject



12
13
14
# File 'lib/generators/graphql_devise/install_generator.rb', line 12

def execute_devise_installer
  generate 'devise:install'
end

#execute_dta_installerObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/graphql_devise/install_generator.rb', line 16

def execute_dta_installer
  # Necessary in case of a re-run of the generator, for DTA to detect concerns already included
  if File.exist?(File.expand_path("app/models/#{user_class.underscore}.rb", destination_root))
    gsub_file(
      "app/models/#{user_class.underscore}.rb",
      'GraphqlDevise::Authenticatable',
      'DeviseTokenAuth::Concerns::User'
    )
  end
  gsub_file(
    'app/controllers/application_controller.rb',
    'GraphqlDevise::SetUserByToken',
    'DeviseTokenAuth::Concerns::SetUserByToken'
  )

  generate 'devise_token_auth:install', "#{user_class} #{mount_path}"
end

#mount_in_schemaObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generators/graphql_devise/install_generator.rb', line 77

def mount_in_schema
  return if options['mount'] == 'separate_route'

  inject_into_file "app/graphql/#{options['mount'].underscore}.rb", after: "< GraphQL::Schema\n" do
<<-RUBY
  use GraphqlDevise::SchemaPlugin.new(
query:            Types::QueryType,
mutation:         Types::MutationType,
resource_loaders: [
  GraphqlDevise::ResourceLoader.new(#{user_class})
]
  )
RUBY
  end
end

#mount_resource_routeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/generators/graphql_devise/install_generator.rb', line 34

def mount_resource_route
  routes_file = 'config/routes.rb'
  dta_route   = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"

  if options['mount'] != 'separate_route'
    gsub_file(routes_file, /^\s+#{Regexp.escape(dta_route + "\n")}/i, '')
  else
    gem_route = "mount_graphql_devise_for #{user_class}, at: '#{mount_path}'"

    if file_contains_str?(routes_file, gem_route)
      gsub_file(routes_file, /^\s+#{Regexp.escape(dta_route + "\n")}/i, '')

      say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
    else
      gsub_file(routes_file, /#{Regexp.escape(dta_route)}/i, gem_route)
    end
  end
end

#replace_controller_concernObject



61
62
63
64
65
66
67
# File 'lib/generators/graphql_devise/install_generator.rb', line 61

def replace_controller_concern
  gsub_file(
    'app/controllers/application_controller.rb',
    /^\s+include DeviseTokenAuth::Concerns::SetUserByToken/,
    '  include GraphqlDevise::SetUserByToken'
  )
end

#replace_model_concernObject



53
54
55
56
57
58
59
# File 'lib/generators/graphql_devise/install_generator.rb', line 53

def replace_model_concern
  gsub_file(
    "app/models/#{user_class.underscore}.rb",
    /^\s+include DeviseTokenAuth::Concerns::User/,
    '  include GraphqlDevise::Authenticatable'
  )
end

#set_change_headers_on_each_request_falseObject



69
70
71
72
73
74
75
# File 'lib/generators/graphql_devise/install_generator.rb', line 69

def set_change_headers_on_each_request_false
  gsub_file(
    'config/initializers/devise_token_auth.rb',
    '# config.change_headers_on_each_request = true',
    'config.change_headers_on_each_request = false'
  )
end