devise_token_auth 0.2.0 → 1.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of devise_token_auth might be problematic. Click here for more details.

Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -2
  3. data/Rakefile +9 -4
  4. data/app/controllers/devise_token_auth/application_controller.rb +5 -7
  5. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +2 -2
  6. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +18 -10
  7. data/app/controllers/devise_token_auth/confirmations_controller.rb +2 -2
  8. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +17 -23
  9. data/app/controllers/devise_token_auth/passwords_controller.rb +23 -27
  10. data/app/controllers/devise_token_auth/registrations_controller.rb +17 -15
  11. data/app/controllers/devise_token_auth/sessions_controller.rb +8 -12
  12. data/app/controllers/devise_token_auth/token_validations_controller.rb +3 -3
  13. data/app/controllers/devise_token_auth/unlocks_controller.rb +10 -12
  14. data/app/models/devise_token_auth/concerns/user.rb +27 -36
  15. data/config/initializers/devise.rb +1 -1
  16. data/lib/devise_token_auth.rb +6 -6
  17. data/lib/devise_token_auth/controllers/helpers.rb +14 -4
  18. data/lib/devise_token_auth/engine.rb +11 -13
  19. data/lib/devise_token_auth/errors.rb +1 -1
  20. data/lib/devise_token_auth/rails/routes.rb +29 -29
  21. data/lib/devise_token_auth/url.rb +6 -4
  22. data/lib/devise_token_auth/version.rb +1 -1
  23. data/lib/generators/devise_token_auth/install_generator.rb +52 -39
  24. data/lib/generators/devise_token_auth/install_views_generator.rb +5 -5
  25. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +1 -1
  26. data/lib/generators/devise_token_auth/templates/{user.rb → user.rb.erb} +0 -0
  27. data/test/controllers/custom/custom_confirmations_controller_test.rb +3 -1
  28. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +2 -0
  29. data/test/controllers/custom/custom_passwords_controller_test.rb +4 -2
  30. data/test/controllers/custom/custom_registrations_controller_test.rb +6 -8
  31. data/test/controllers/custom/custom_sessions_controller_test.rb +5 -5
  32. data/test/controllers/custom/custom_token_validations_controller_test.rb +3 -3
  33. data/test/controllers/demo_group_controller_test.rb +2 -6
  34. data/test/controllers/demo_mang_controller_test.rb +2 -5
  35. data/test/controllers/demo_user_controller_test.rb +8 -17
  36. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +2 -2
  37. data/test/controllers/devise_token_auth/passwords_controller_test.rb +13 -9
  38. data/test/controllers/devise_token_auth/registrations_controller_test.rb +8 -8
  39. data/test/controllers/devise_token_auth/sessions_controller_test.rb +22 -33
  40. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +2 -6
  41. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +1 -1
  42. data/test/controllers/overrides/confirmations_controller_test.rb +3 -1
  43. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +3 -1
  44. data/test/controllers/overrides/passwords_controller_test.rb +25 -29
  45. data/test/controllers/overrides/registrations_controller_test.rb +4 -2
  46. data/test/controllers/overrides/sessions_controller_test.rb +4 -4
  47. data/test/controllers/overrides/token_validations_controller_test.rb +3 -3
  48. data/test/dummy/app/controllers/custom/confirmations_controller.rb +0 -2
  49. data/test/dummy/app/controllers/custom/passwords_controller.rb +1 -4
  50. data/test/dummy/app/controllers/custom/registrations_controller.rb +1 -2
  51. data/test/dummy/app/controllers/custom/sessions_controller.rb +1 -3
  52. data/test/dummy/app/controllers/custom/token_validations_controller.rb +1 -3
  53. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +3 -3
  54. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +3 -3
  55. data/test/dummy/app/controllers/overrides/passwords_controller.rb +5 -5
  56. data/test/dummy/app/controllers/overrides/registrations_controller.rb +2 -2
  57. data/test/dummy/app/controllers/overrides/sessions_controller.rb +8 -10
  58. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +3 -5
  59. data/test/dummy/app/helpers/application_helper.rb +1027 -1036
  60. data/test/dummy/app/models/scoped_user.rb +2 -2
  61. data/test/dummy/app/models/unregisterable_user.rb +2 -2
  62. data/test/dummy/app/models/user.rb +1 -1
  63. data/test/dummy/config.ru +5 -3
  64. data/test/dummy/config/application.rb +2 -2
  65. data/test/dummy/config/boot.rb +2 -2
  66. data/test/dummy/config/environment.rb +1 -1
  67. data/test/dummy/config/environments/development.rb +3 -3
  68. data/test/dummy/config/environments/test.rb +1 -1
  69. data/test/dummy/config/initializers/omniauth.rb +2 -2
  70. data/test/dummy/config/routes.rb +10 -29
  71. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +8 -8
  72. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +7 -7
  73. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +4 -4
  74. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +7 -7
  75. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +7 -7
  76. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +7 -7
  77. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +6 -6
  78. data/test/dummy/db/schema.rb +2 -60
  79. data/test/factories/users.rb +40 -0
  80. data/test/lib/devise_token_auth/url_test.rb +6 -6
  81. data/test/lib/generators/devise_token_auth/install_generator_test.rb +22 -14
  82. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +30 -22
  83. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +1 -1
  84. data/test/models/user_test.rb +15 -46
  85. data/test/support/controllers/routes.rb +43 -0
  86. data/test/test_helper.rb +12 -12
  87. metadata +8 -14
  88. data/test/dummy/app/models/evil_user.rb +0 -5
  89. data/test/dummy/app/models/nice_user.rb +0 -9
  90. data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +0 -66
  91. data/test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb +0 -63
  92. data/test/integration/navigation_test.rb +0 -12
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "devise"
4
- require "devise_token_auth/engine"
5
- require "devise_token_auth/controllers/helpers"
6
- require "devise_token_auth/controllers/url_helpers"
7
- require "devise_token_auth/url"
8
- require "devise_token_auth/errors"
3
+ require 'devise'
4
+ require 'devise_token_auth/engine'
5
+ require 'devise_token_auth/controllers/helpers'
6
+ require 'devise_token_auth/controllers/url_helpers'
7
+ require 'devise_token_auth/url'
8
+ require 'devise_token_auth/errors'
9
9
 
10
10
  module DeviseTokenAuth
11
11
  end
@@ -28,8 +28,8 @@ module DeviseTokenAuth
28
28
  # before_action ->{ authenticate_blogger! :admin } # Redirects to the admin login page
29
29
  # current_blogger :user # Preferably returns a User if one is signed in
30
30
  #
31
- def devise_token_auth_group(group_name, opts={})
32
- mappings = "[#{ opts[:contains].map { |m| ":#{m}" }.join(',') }]"
31
+ def devise_token_auth_group(group_name, opts = {})
32
+ mappings = "[#{opts[:contains].map { |m| ":#{m}" }.join(',')}]"
33
33
 
34
34
  class_eval <<-METHODS, __FILE__, __LINE__ + 1
35
35
  def authenticate_#{group_name}!(favourite=nil, opts={})
@@ -75,7 +75,12 @@ module DeviseTokenAuth
75
75
  end
76
76
 
77
77
  if respond_to?(:helper_method)
78
- helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?", "render_authenticate_error"
78
+ helper_method(
79
+ "current_#{group_name}",
80
+ "current_#{group_name.to_s.pluralize}",
81
+ "#{group_name}_signed_in?",
82
+ "render_authenticate_error"
83
+ )
79
84
  end
80
85
  METHODS
81
86
  end
@@ -142,7 +147,12 @@ module DeviseTokenAuth
142
147
 
143
148
  ActiveSupport.on_load(:action_controller) do
144
149
  if respond_to?(:helper_method)
145
- helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "render_authenticate_error"
150
+ helper_method(
151
+ "current_#{mapping}",
152
+ "#{mapping}_signed_in?",
153
+ "#{mapping}_session",
154
+ 'render_authenticate_error'
155
+ )
146
156
  end
147
157
  end
148
158
  end
@@ -6,7 +6,7 @@ module DeviseTokenAuth
6
6
  class Engine < ::Rails::Engine
7
7
  isolate_namespace DeviseTokenAuth
8
8
 
9
- initializer "devise_token_auth.url_helpers" do
9
+ initializer 'devise_token_auth.url_helpers' do
10
10
  Devise.helpers << DeviseTokenAuth::Controllers::Helpers
11
11
  end
12
12
  end
@@ -38,11 +38,11 @@ module DeviseTokenAuth
38
38
  self.enable_standard_devise_support = false
39
39
  self.remove_tokens_after_password_reset = false
40
40
  self.default_callbacks = true
41
- self.headers_names = {:'access-token' => 'access-token',
42
- :'client' => 'client',
43
- :'expiry' => 'expiry',
44
- :'uid' => 'uid',
45
- :'token-type' => 'token-type' }
41
+ self.headers_names = { 'access-token': 'access-token',
42
+ 'client': 'client',
43
+ 'expiry': 'expiry',
44
+ 'uid': 'uid',
45
+ 'token-type': 'token-type' }
46
46
  self.bypass_sign_in = true
47
47
 
48
48
  def self.setup(&block)
@@ -50,23 +50,21 @@ module DeviseTokenAuth
50
50
 
51
51
  Rails.application.config.after_initialize do
52
52
  if defined?(::OmniAuth)
53
- ::OmniAuth::config.path_prefix = Devise.omniauth_path_prefix = self.omniauth_prefix
54
-
53
+ ::OmniAuth::config.path_prefix = Devise.omniauth_path_prefix = omniauth_prefix
55
54
 
56
55
  # Omniauth currently does not pass along omniauth.params upon failure redirect
57
56
  # see also: https://github.com/intridea/omniauth/issues/626
58
57
  OmniAuth::FailureEndpoint.class_eval do
59
58
  def redirect_to_failure
60
59
  message_key = env['omniauth.error.type']
61
- origin_query_param = env['omniauth.origin'] ? "&origin=#{CGI.escape(env['omniauth.origin'])}" : ""
62
- strategy_name_query_param = env['omniauth.error.strategy'] ? "&strategy=#{env['omniauth.error.strategy'].name}" : ""
63
- extra_params = env['omniauth.params'] ? "&#{env['omniauth.params'].to_query}" : ""
60
+ origin_query_param = env['omniauth.origin'] ? "&origin=#{CGI.escape(env['omniauth.origin'])}" : ''
61
+ strategy_name_query_param = env['omniauth.error.strategy'] ? "&strategy=#{env['omniauth.error.strategy'].name}" : ''
62
+ extra_params = env['omniauth.params'] ? "&#{env['omniauth.params'].to_query}" : ''
64
63
  new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}#{extra_params}"
65
- Rack::Response.new(["302 Moved"], 302, 'Location' => new_path).finish
64
+ Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish
66
65
  end
67
66
  end
68
67
 
69
-
70
68
  # Omniauth currently removes omniauth.params during mocked requests
71
69
  # see also: https://github.com/intridea/omniauth/pull/812
72
70
  OmniAuth::Strategy.class_eval do
@@ -2,6 +2,6 @@
2
2
 
3
3
  module DeviseTokenAuth
4
4
  module Errors
5
- class NoResourceDefinedError < StandardError ; end
5
+ class NoResourceDefinedError < StandardError; end
6
6
  end
7
7
  end
@@ -8,31 +8,31 @@ module ActionDispatch::Routing
8
8
  opts[:skip] ||= []
9
9
 
10
10
  # check for ctrl overrides, fall back to defaults
11
- sessions_ctrl = opts[:controllers][:sessions] || "devise_token_auth/sessions"
12
- registrations_ctrl = opts[:controllers][:registrations] || "devise_token_auth/registrations"
13
- passwords_ctrl = opts[:controllers][:passwords] || "devise_token_auth/passwords"
14
- confirmations_ctrl = opts[:controllers][:confirmations] || "devise_token_auth/confirmations"
15
- token_validations_ctrl = opts[:controllers][:token_validations] || "devise_token_auth/token_validations"
16
- omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || "devise_token_auth/omniauth_callbacks"
17
- unlocks_ctrl = opts[:controllers][:unlocks] || "devise_token_auth/unlocks"
11
+ sessions_ctrl = opts[:controllers][:sessions] || 'devise_token_auth/sessions'
12
+ registrations_ctrl = opts[:controllers][:registrations] || 'devise_token_auth/registrations'
13
+ passwords_ctrl = opts[:controllers][:passwords] || 'devise_token_auth/passwords'
14
+ confirmations_ctrl = opts[:controllers][:confirmations] || 'devise_token_auth/confirmations'
15
+ token_validations_ctrl = opts[:controllers][:token_validations] || 'devise_token_auth/token_validations'
16
+ omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || 'devise_token_auth/omniauth_callbacks'
17
+ unlocks_ctrl = opts[:controllers][:unlocks] || 'devise_token_auth/unlocks'
18
18
 
19
19
  # define devise controller mappings
20
- controllers = {:sessions => sessions_ctrl,
21
- :registrations => registrations_ctrl,
22
- :passwords => passwords_ctrl,
23
- :confirmations => confirmations_ctrl}
20
+ controllers = { sessions: sessions_ctrl,
21
+ registrations: registrations_ctrl,
22
+ passwords: passwords_ctrl,
23
+ confirmations: confirmations_ctrl }
24
24
 
25
25
  controllers[:unlocks] = unlocks_ctrl if unlocks_ctrl
26
26
 
27
27
  # remove any unwanted devise modules
28
- opts[:skip].each{|item| controllers.delete(item)}
28
+ opts[:skip].each{ |item| controllers.delete(item) }
29
29
 
30
30
  devise_for resource.pluralize.underscore.gsub('/', '_').to_sym,
31
- :class_name => resource,
32
- :module => :devise,
33
- :path => "#{opts[:at]}",
34
- :controllers => controllers,
35
- :skip => opts[:skip] + [:omniauth_callbacks]
31
+ class_name: resource,
32
+ module: :devise,
33
+ path: opts[:at].to_s,
34
+ controllers: controllers,
35
+ skip: opts[:skip] + [:omniauth_callbacks]
36
36
 
37
37
  unnest_namespace do
38
38
  # get full url path as if it were namespaced
@@ -43,8 +43,8 @@ module ActionDispatch::Routing
43
43
 
44
44
  # clear scope so controller routes aren't namespaced
45
45
  @scope = ActionDispatch::Routing::Mapper::Scope.new(
46
- path: "",
47
- shallow_path: "",
46
+ path: '',
47
+ shallow_path: '',
48
48
  constraints: {},
49
49
  defaults: {},
50
50
  options: {},
@@ -56,29 +56,29 @@ module ActionDispatch::Routing
56
56
 
57
57
  devise_scope mapping_name.to_sym do
58
58
  # path to verify token validity
59
- get "#{full_path}/validate_token", controller: "#{token_validations_ctrl}", action: "validate_token"
59
+ get "#{full_path}/validate_token", controller: token_validations_ctrl.to_s, action: 'validate_token'
60
60
 
61
61
  # omniauth routes. only define if omniauth is installed and not skipped.
62
62
  if defined?(::OmniAuth) && !opts[:skip].include?(:omniauth_callbacks)
63
- match "#{full_path}/failure", controller: omniauth_ctrl, action: "omniauth_failure", via: [:get]
64
- match "#{full_path}/:provider/callback", controller: omniauth_ctrl, action: "omniauth_success", via: [:get]
63
+ match "#{full_path}/failure", controller: omniauth_ctrl, action: 'omniauth_failure', via: [:get]
64
+ match "#{full_path}/:provider/callback", controller: omniauth_ctrl, action: 'omniauth_success', via: [:get]
65
65
 
66
- match "#{DeviseTokenAuth.omniauth_prefix}/:provider/callback", controller: omniauth_ctrl, action: "redirect_callbacks", via: [:get, :post]
67
- match "#{DeviseTokenAuth.omniauth_prefix}/failure", controller: omniauth_ctrl, action: "omniauth_failure", via: [:get, :post]
66
+ match "#{DeviseTokenAuth.omniauth_prefix}/:provider/callback", controller: omniauth_ctrl, action: 'redirect_callbacks', via: [:get, :post]
67
+ match "#{DeviseTokenAuth.omniauth_prefix}/failure", controller: omniauth_ctrl, action: 'omniauth_failure', via: [:get, :post]
68
68
 
69
69
  # preserve the resource class thru oauth authentication by setting name of
70
70
  # resource as "resource_class" param
71
- match "#{full_path}/:provider", to: redirect{|params, request|
71
+ match "#{full_path}/:provider", to: redirect{ |params, request|
72
72
  # get the current querystring
73
- qs = CGI::parse(request.env["QUERY_STRING"])
73
+ qs = CGI::parse(request.env['QUERY_STRING'])
74
74
 
75
75
  # append name of current resource
76
- qs["resource_class"] = [resource]
77
- qs["namespace_name"] = [namespace_name] if namespace_name
76
+ qs['resource_class'] = [resource]
77
+ qs['namespace_name'] = [namespace_name] if namespace_name
78
78
 
79
79
  set_omniauth_path_prefix!(DeviseTokenAuth.omniauth_prefix)
80
80
 
81
- redirect_params = {}.tap {|hash| qs.each{|k, v| hash[k] = v.first}}
81
+ redirect_params = {}.tap { |hash| qs.each{ |k, v| hash[k] = v.first } }
82
82
 
83
83
  if DeviseTokenAuth.redirect_whitelist
84
84
  redirect_url = request.params['auth_origin_url']
@@ -7,19 +7,21 @@ module DeviseTokenAuth::Url
7
7
 
8
8
  res = "#{uri.scheme}://#{uri.host}"
9
9
  res += ":#{uri.port}" if (uri.port && uri.port != 80 && uri.port != 443)
10
- res += "#{uri.path}" if uri.path
10
+ res += uri.path.to_s if uri.path
11
11
  query = [uri.query, params.to_query].reject(&:blank?).join('&')
12
12
  res += "?#{query}"
13
13
  res += "##{uri.fragment}" if uri.fragment
14
14
 
15
- return res
15
+ res
16
16
  end
17
17
 
18
18
  def self.whitelisted?(url)
19
- url.nil? || !!DeviseTokenAuth.redirect_whitelist.find { |pattern| !!Wildcat.new(pattern).match(url) }
19
+ url.nil? || \
20
+ !!DeviseTokenAuth.redirect_whitelist.find do |pattern|
21
+ !!Wildcat.new(pattern).match(url)
22
+ end
20
23
  end
21
24
 
22
-
23
25
  # wildcard convenience class
24
26
  class Wildcat
25
27
  def self.parse_to_regex(str)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeviseTokenAuth
4
- VERSION = '0.2.0'
4
+ VERSION = '1.0.0.rc1'.freeze
5
5
  end
@@ -4,54 +4,56 @@ module DeviseTokenAuth
4
4
  class InstallGenerator < Rails::Generators::Base
5
5
  include Rails::Generators::Migration
6
6
 
7
- source_root File.expand_path('../templates', __FILE__)
7
+ class_option :primary_key_type, type: :string, desc: 'The type for primary key'
8
8
 
9
- argument :user_class, type: :string, default: "User"
9
+ source_root File.expand_path('templates', __dir__)
10
+
11
+ argument :user_class, type: :string, default: 'User'
10
12
  argument :mount_path, type: :string, default: 'auth'
11
13
 
12
14
  def create_initializer_file
13
- copy_file("devise_token_auth.rb", "config/initializers/devise_token_auth.rb")
15
+ copy_file('devise_token_auth.rb', 'config/initializers/devise_token_auth.rb')
14
16
  end
15
17
 
16
18
  def copy_migrations
17
- if self.class.migration_exists?("db/migrate", "devise_token_auth_create_#{ user_class.pluralize.gsub("::","").underscore }")
18
- say_status("skipped", "Migration 'devise_token_auth_create_#{ user_class.pluralize.gsub("::","").underscore }' already exists")
19
+ if self.class.migration_exists?('db/migrate', "devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}")
20
+ say_status('skipped', "Migration 'devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}' already exists")
19
21
  else
20
22
  migration_template(
21
- "devise_token_auth_create_users.rb.erb",
22
- "db/migrate/devise_token_auth_create_#{ user_class.pluralize.gsub("::","").underscore }.rb"
23
+ 'devise_token_auth_create_users.rb.erb',
24
+ "db/migrate/devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}.rb"
23
25
  )
24
26
  end
25
27
  end
26
28
 
27
29
  def create_user_model
28
- fname = "app/models/#{ user_class.underscore }.rb"
29
- unless File.exist?(File.join(destination_root, fname))
30
- template("user.rb", fname)
31
- else
32
- inclusion = "include DeviseTokenAuth::Concerns::User"
30
+ fname = "app/models/#{user_class.underscore}.rb"
31
+ if File.exist?(File.join(destination_root, fname))
32
+ inclusion = 'include DeviseTokenAuth::Concerns::User'
33
33
  unless parse_file_for_line(fname, inclusion)
34
34
 
35
35
  active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
36
36
  inject_into_file fname, after: "class #{user_class} < #{active_record_needle}\n" do <<-'RUBY'
37
- # Include default devise modules.
38
- devise :database_authenticatable, :registerable,
39
- :recoverable, :rememberable, :trackable, :validatable,
40
- :confirmable, :omniauthable
41
- include DeviseTokenAuth::Concerns::User
42
- RUBY
37
+ # Include default devise modules.
38
+ devise :database_authenticatable, :registerable,
39
+ :recoverable, :rememberable, :trackable, :validatable,
40
+ :confirmable, :omniauthable
41
+ include DeviseTokenAuth::Concerns::User
42
+ RUBY
43
43
  end
44
44
  end
45
+ else
46
+ template('user.rb.erb', fname)
45
47
  end
46
48
  end
47
49
 
48
50
  def include_controller_concerns
49
- fname = "app/controllers/application_controller.rb"
50
- line = "include DeviseTokenAuth::Concerns::SetUserByToken"
51
+ fname = 'app/controllers/application_controller.rb'
52
+ line = 'include DeviseTokenAuth::Concerns::SetUserByToken'
51
53
 
52
54
  if File.exist?(File.join(destination_root, fname))
53
55
  if parse_file_for_line(fname, line)
54
- say_status("skipped", "Concern is already included in the application controller.")
56
+ say_status('skipped', 'Concern is already included in the application controller.')
55
57
  elsif is_rails_api?
56
58
  inject_into_file fname, after: "class ApplicationController < ActionController::API\n" do <<-'RUBY'
57
59
  include DeviseTokenAuth::Concerns::SetUserByToken
@@ -64,46 +66,46 @@ module DeviseTokenAuth
64
66
  end
65
67
  end
66
68
  else
67
- say_status("skipped", "app/controllers/application_controller.rb not found. Add 'include DeviseTokenAuth::Concerns::SetUserByToken' to any controllers that require authentication.")
69
+ say_status('skipped', "app/controllers/application_controller.rb not found. Add 'include DeviseTokenAuth::Concerns::SetUserByToken' to any controllers that require authentication.")
68
70
  end
69
71
  end
70
72
 
71
73
  def add_route_mount
72
- f = "config/routes.rb"
74
+ f = 'config/routes.rb'
73
75
  str = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
74
76
 
75
77
  if File.exist?(File.join(destination_root, f))
76
- line = parse_file_for_line(f, "mount_devise_token_auth_for")
78
+ line = parse_file_for_line(f, 'mount_devise_token_auth_for')
77
79
 
78
- unless line
79
- line = "Rails.application.routes.draw do"
80
- existing_user_class = false
81
- else
80
+ if line
82
81
  existing_user_class = true
82
+ else
83
+ line = 'Rails.application.routes.draw do'
84
+ existing_user_class = false
83
85
  end
84
86
 
85
87
  if parse_file_for_line(f, str)
86
- say_status("skipped", "Routes already exist for #{user_class} at #{mount_path}")
88
+ say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
87
89
  else
88
90
  insert_after_line(f, line, str)
89
91
 
90
92
  if existing_user_class
91
- scoped_routes = ""+
92
- "as :#{user_class.underscore} do\n"+
93
- " # Define routes for #{user_class} within this block.\n"+
93
+ scoped_routes = ''\
94
+ "as :#{user_class.underscore} do\n"\
95
+ " # Define routes for #{user_class} within this block.\n"\
94
96
  " end\n"
95
97
  insert_after_line(f, str, scoped_routes)
96
98
  end
97
99
  end
98
100
  else
99
- say_status("skipped", "config/routes.rb not found. Add \"mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'\" to your routes file.")
101
+ say_status('skipped', "config/routes.rb not found. Add \"mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'\" to your routes file.")
100
102
  end
101
103
  end
102
104
 
103
105
  private
104
106
 
105
107
  def self.next_migration_number(path)
106
- Time.zone.now.utc.strftime("%Y%m%d%H%M%S")
108
+ Time.zone.now.utc.strftime('%Y%m%d%H%M%S')
107
109
  end
108
110
 
109
111
  def insert_after_line(filename, line, str)
@@ -117,17 +119,15 @@ module DeviseTokenAuth
117
119
 
118
120
  File.open(File.join(destination_root, filename)) do |f|
119
121
  f.each_line do |line|
120
- if line =~ /(#{Regexp.escape(str)})/mi
121
- match = line
122
- end
122
+ match = line if line =~ /(#{Regexp.escape(str)})/mi
123
123
  end
124
124
  end
125
125
  match
126
126
  end
127
127
 
128
128
  def is_rails_api?
129
- fname = "app/controllers/application_controller.rb"
130
- line = "class ApplicationController < ActionController::API"
129
+ fname = 'app/controllers/application_controller.rb'
130
+ line = 'class ApplicationController < ActionController::API'
131
131
  parse_file_for_line(fname, line)
132
132
  end
133
133
 
@@ -158,5 +158,18 @@ module DeviseTokenAuth
158
158
  def database_version
159
159
  ActiveRecord::Base.connection.select_value('SELECT VERSION()')
160
160
  end
161
+
162
+ def rails5?
163
+ Rails.version.start_with? '5'
164
+ end
165
+
166
+ def primary_key_type
167
+ primary_key_string if rails5?
168
+ end
169
+
170
+ def primary_key_string
171
+ key_string = options[:primary_key_type]
172
+ ", id: :#{key_string}" if key_string
173
+ end
161
174
  end
162
175
  end
@@ -2,16 +2,16 @@
2
2
 
3
3
  module DeviseTokenAuth
4
4
  class InstallViewsGenerator < Rails::Generators::Base
5
- source_root File.expand_path('../../../../app/views/devise/mailer', __FILE__)
5
+ source_root File.expand_path('../../../app/views/devise/mailer', __dir__)
6
6
 
7
7
  def copy_mailer_templates
8
8
  copy_file(
9
- "confirmation_instructions.html.erb",
10
- "app/views/devise/mailer/confirmation_instructions.html.erb"
9
+ 'confirmation_instructions.html.erb',
10
+ 'app/views/devise/mailer/confirmation_instructions.html.erb'
11
11
  )
12
12
  copy_file(
13
- "reset_password_instructions.html.erb",
14
- "app/views/devise/mailer/reset_password_instructions.html.erb"
13
+ 'reset_password_instructions.html.erb',
14
+ 'app/views/devise/mailer/reset_password_instructions.html.erb'
15
15
  )
16
16
  end
17
17
  end