rodauth-rails 1.3.1 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/README.md +10 -103
  4. data/lib/generators/rodauth/install_generator.rb +9 -10
  5. data/lib/generators/rodauth/migration/{account_expiration.erb → active_record/account_expiration.erb} +0 -0
  6. data/lib/generators/rodauth/migration/{active_sessions.erb → active_record/active_sessions.erb} +0 -0
  7. data/lib/generators/rodauth/migration/{audit_logging.erb → active_record/audit_logging.erb} +0 -0
  8. data/lib/generators/rodauth/migration/{base.erb → active_record/base.erb} +0 -0
  9. data/lib/generators/rodauth/migration/{disallow_password_reuse.erb → active_record/disallow_password_reuse.erb} +1 -1
  10. data/lib/generators/rodauth/migration/{email_auth.erb → active_record/email_auth.erb} +0 -0
  11. data/lib/generators/rodauth/migration/{jwt_refresh.erb → active_record/jwt_refresh.erb} +0 -0
  12. data/lib/generators/rodauth/migration/{lockout.erb → active_record/lockout.erb} +0 -0
  13. data/lib/generators/rodauth/migration/{otp.erb → active_record/otp.erb} +0 -0
  14. data/lib/generators/rodauth/migration/{password_expiration.erb → active_record/password_expiration.erb} +0 -0
  15. data/lib/generators/rodauth/migration/{recovery_codes.erb → active_record/recovery_codes.erb} +0 -0
  16. data/lib/generators/rodauth/migration/{remember.erb → active_record/remember.erb} +0 -0
  17. data/lib/generators/rodauth/migration/{reset_password.erb → active_record/reset_password.erb} +0 -0
  18. data/lib/generators/rodauth/migration/{single_session.erb → active_record/single_session.erb} +0 -0
  19. data/lib/generators/rodauth/migration/{sms_codes.erb → active_record/sms_codes.erb} +0 -0
  20. data/lib/generators/rodauth/migration/{verify_account.erb → active_record/verify_account.erb} +0 -0
  21. data/lib/generators/rodauth/migration/{verify_login_change.erb → active_record/verify_login_change.erb} +0 -0
  22. data/lib/generators/rodauth/migration/{webauthn.erb → active_record/webauthn.erb} +0 -0
  23. data/lib/generators/rodauth/migration/sequel/account_expiration.erb +7 -0
  24. data/lib/generators/rodauth/migration/sequel/active_sessions.erb +8 -0
  25. data/lib/generators/rodauth/migration/sequel/audit_logging.erb +17 -0
  26. data/lib/generators/rodauth/migration/sequel/base.erb +25 -0
  27. data/lib/generators/rodauth/migration/sequel/disallow_password_reuse.erb +6 -0
  28. data/lib/generators/rodauth/migration/sequel/email_auth.erb +7 -0
  29. data/lib/generators/rodauth/migration/sequel/jwt_refresh.erb +8 -0
  30. data/lib/generators/rodauth/migration/sequel/lockout.erb +11 -0
  31. data/lib/generators/rodauth/migration/sequel/otp.erb +7 -0
  32. data/lib/generators/rodauth/migration/sequel/password_expiration.erb +5 -0
  33. data/lib/generators/rodauth/migration/sequel/recovery_codes.erb +6 -0
  34. data/lib/generators/rodauth/migration/sequel/remember.erb +6 -0
  35. data/lib/generators/rodauth/migration/sequel/reset_password.erb +7 -0
  36. data/lib/generators/rodauth/migration/sequel/single_session.erb +5 -0
  37. data/lib/generators/rodauth/migration/sequel/sms_codes.erb +8 -0
  38. data/lib/generators/rodauth/migration/sequel/verify_account.erb +7 -0
  39. data/lib/generators/rodauth/migration/sequel/verify_login_change.erb +7 -0
  40. data/lib/generators/rodauth/migration/sequel/webauthn.erb +13 -0
  41. data/lib/generators/rodauth/migration_generator.rb +89 -9
  42. data/lib/generators/rodauth/templates/app/mailers/rodauth_mailer.rb +38 -28
  43. data/lib/generators/rodauth/templates/app/misc/rodauth_main.rb +6 -6
  44. data/lib/generators/rodauth/templates/app/models/account.rb +8 -0
  45. data/lib/generators/rodauth/templates/app/views/rodauth_mailer/verify_login_change.text.erb +2 -2
  46. data/lib/generators/rodauth/templates/db/migrate/create_rodauth.rb +8 -0
  47. data/lib/rodauth/rails/middleware.rb +9 -0
  48. data/lib/rodauth/rails/model.rb +2 -97
  49. data/lib/rodauth/rails/version.rb +1 -1
  50. data/lib/rodauth/rails.rb +2 -1
  51. data/rodauth-rails.gemspec +1 -0
  52. metadata +52 -22
  53. data/lib/generators/rodauth/migration_helpers.rb +0 -77
  54. data/lib/rodauth/rails/model/associations.rb +0 -195
@@ -1,54 +1,64 @@
1
1
  class RodauthMailer < ApplicationMailer
2
- def verify_account(account_id, key)
3
- @email_link = rodauth.verify_account_url(key: email_token(account_id, key))
4
- @account = Account.find(account_id)
2
+ def verify_account(name = nil, account_id, key)
3
+ @email_link = email_link(name, :verify_account, account_id, key)
4
+ @account = find_account(name, account_id)
5
5
 
6
- mail to: @account.email, subject: rodauth.verify_account_email_subject
6
+ mail to: @account.email, subject: rodauth(name).verify_account_email_subject
7
7
  end
8
8
 
9
- def reset_password(account_id, key)
10
- @email_link = rodauth.reset_password_url(key: email_token(account_id, key))
11
- @account = Account.find(account_id)
9
+ def reset_password(name = nil, account_id, key)
10
+ @email_link = email_link(name, :reset_password, account_id, key)
11
+ @account = find_account(name, account_id)
12
12
 
13
- mail to: @account.email, subject: rodauth.reset_password_email_subject
13
+ mail to: @account.email, subject: rodauth(name).reset_password_email_subject
14
14
  end
15
15
 
16
- def verify_login_change(account_id, old_login, new_login, key)
17
- @old_login = old_login
18
- @new_login = new_login
19
- @email_link = rodauth.verify_login_change_url(key: email_token(account_id, key))
20
- @account = Account.find(account_id)
16
+ def verify_login_change(name = nil, account_id, key)
17
+ @email_link = email_link(name, :verify_login_change, account_id, key)
18
+ @account = find_account(name, account_id)
19
+ @new_email = @account.login_change_key.login
21
20
 
22
- mail to: new_login, subject: rodauth.verify_login_change_email_subject
21
+ mail to: @new_email, subject: rodauth(name).verify_login_change_email_subject
23
22
  end
24
23
 
25
- def password_changed(account_id)
26
- @account = Account.find(account_id)
24
+ def password_changed(name = nil, account_id)
25
+ @account = find_account(name, account_id)
27
26
 
28
- mail to: @account.email, subject: rodauth.password_changed_email_subject
27
+ mail to: @account.email, subject: rodauth(name).password_changed_email_subject
29
28
  end
30
29
 
31
- # def email_auth(account_id, key)
32
- # @email_link = rodauth.email_auth_url(key: email_token(account_id, key))
33
- # @account = Account.find(account_id)
30
+ # def email_auth(name = nil, account_id, key)
31
+ # @email_link = email_link(name, :email_auth, account_id, key)
32
+ # @account = find_account(name, account_id)
34
33
 
35
- # mail to: @account.email, subject: rodauth.email_auth_email_subject
34
+ # mail to: @account.email, subject: rodauth(name).email_auth_email_subject
36
35
  # end
37
36
 
38
- # def unlock_account(account_id, key)
39
- # @email_link = rodauth.unlock_account_url(key: email_token(account_id, key))
40
- # @account = Account.find(account_id)
37
+ # def unlock_account(name = nil, account_id, key)
38
+ # @email_link = email_link(name, :unlock_account, account_id, key)
39
+ # @account = find_account(name, account_id)
41
40
 
42
- # mail to: @account.email, subject: rodauth.unlock_account_email_subject
41
+ # mail to: @account.email, subject: rodauth(name).unlock_account_email_subject
43
42
  # end
44
43
 
45
44
  private
46
45
 
47
- def email_token(account_id, key)
48
- "#{account_id}_#{rodauth.compute_hmac(key)}"
46
+ def find_account(_name, account_id)
47
+ <% if defined?(ActiveRecord::Railtie) -%>
48
+ Account.find(account_id)
49
+ <% else -%>
50
+ Account.with_pk!(account_id)
51
+ <% end -%>
49
52
  end
50
53
 
51
- def rodauth(name = nil)
54
+ def email_link(name, action, account_id, key)
55
+ instance = rodauth(name)
56
+ instance.instance_variable_set(:@account, { id: account_id })
57
+ instance.instance_variable_set(:"@#{action}_key_value", key)
58
+ instance.public_send(:"#{action}_email_link")
59
+ end
60
+
61
+ def rodauth(name)
52
62
  RodauthApp.rodauth(name).allocate
53
63
  end
54
64
  end
@@ -56,22 +56,22 @@ class RodauthMain < Rodauth::Rails::Auth
56
56
  # ==> Emails
57
57
  # Use a custom mailer for delivering authentication emails.
58
58
  create_reset_password_email do
59
- RodauthMailer.reset_password(account_id, reset_password_key_value)
59
+ RodauthMailer.reset_password(*self.class.configuration_name, account_id, reset_password_key_value)
60
60
  end
61
61
  create_verify_account_email do
62
- RodauthMailer.verify_account(account_id, verify_account_key_value)
62
+ RodauthMailer.verify_account(*self.class.configuration_name, account_id, verify_account_key_value)
63
63
  end
64
64
  create_verify_login_change_email do |_login|
65
- RodauthMailer.verify_login_change(account_id, verify_login_change_old_login, verify_login_change_new_login, verify_login_change_key_value)
65
+ RodauthMailer.verify_login_change(*self.class.configuration_name, account_id, verify_login_change_key_value)
66
66
  end
67
67
  create_password_changed_email do
68
- RodauthMailer.password_changed(account_id)
68
+ RodauthMailer.password_changed(*self.class.configuration_name, account_id)
69
69
  end
70
70
  # create_email_auth_email do
71
- # RodauthMailer.email_auth(account_id, email_auth_key_value)
71
+ # RodauthMailer.email_auth(*self.class.configuration_name, account_id, email_auth_key_value)
72
72
  # end
73
73
  # create_unlock_account_email do
74
- # RodauthMailer.unlock_account(account_id, unlock_account_key_value)
74
+ # RodauthMailer.unlock_account(*self.class.configuration_name, account_id, unlock_account_key_value)
75
75
  # end
76
76
  send_email do |email|
77
77
  # queue email delivery on the mailer after the transaction commits
@@ -1,3 +1,4 @@
1
+ <% if defined?(ActiveRecord::Railtie) -%>
1
2
  class Account < ApplicationRecord
2
3
  include Rodauth::Rails.model
3
4
  <% if ActiveRecord.version >= Gem::Version.new("7.0") -%>
@@ -6,3 +7,10 @@ class Account < ApplicationRecord
6
7
  enum status: { unverified: 1, verified: 2, closed: 3 }
7
8
  <% end -%>
8
9
  end
10
+ <% else -%>
11
+ class Account < Sequel::Model
12
+ include Rodauth::Rails.model
13
+ plugin :enum
14
+ enum :status, unverified: 1, verified: 2, closed: 3
15
+ end
16
+ <% end -%>
@@ -1,8 +1,8 @@
1
1
  Someone with an account has requested their login be changed to this email address:
2
2
 
3
- Old email: <%= @old_login %>
3
+ Old email: <%= @account.email %>
4
4
 
5
- New email: <%= @new_login %>
5
+ New email: <%= @new_email %>
6
6
 
7
7
  If you did not request this login change, please ignore this message. If you
8
8
  requested this login change, please go to
@@ -1,5 +1,13 @@
1
+ <% if defined?(::ActiveRecord::Railtie) -%>
1
2
  class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
3
  def change
3
4
  <%= migration_content -%>
4
5
  end
5
6
  end
7
+ <% else -%>
8
+ Sequel.migration do
9
+ change do
10
+ <%= migration_content -%>
11
+ end
12
+ end
13
+ <% end -%>
@@ -9,6 +9,8 @@ module Rodauth
9
9
  end
10
10
 
11
11
  def call(env)
12
+ return @app.call(env) if asset_request?(env)
13
+
12
14
  app = Rodauth::Rails.app.new(@app)
13
15
 
14
16
  # allow the Rails app to call Rodauth methods that throw :halt
@@ -16,6 +18,13 @@ module Rodauth
16
18
  app.call(env)
17
19
  end
18
20
  end
21
+
22
+ # Check whether it's a request to an asset managed by Sprockets or Propshaft.
23
+ def asset_request?(env)
24
+ return false unless ::Rails.application.config.respond_to?(:assets)
25
+
26
+ env["PATH_INFO"] =~ %r(\A/{0,2}#{::Rails.application.config.assets.prefix})
27
+ end
19
28
  end
20
29
  end
21
30
  end
@@ -1,101 +1,6 @@
1
1
  module Rodauth
2
2
  module Rails
3
- class Model < Module
4
- require "rodauth/rails/model/associations"
5
-
6
- def initialize(auth_class, association_options: {})
7
- @auth_class = auth_class
8
- @association_options = association_options
9
-
10
- define_methods
11
- end
12
-
13
- def included(model)
14
- fail Rodauth::Rails::Error, "must be an Active Record model" unless model < ActiveRecord::Base
15
-
16
- define_associations(model)
17
- end
18
-
19
- private
20
-
21
- def define_methods
22
- rodauth = @auth_class.allocate.freeze
23
-
24
- attr_reader :password
25
-
26
- define_method(:password=) do |password|
27
- @password = password
28
- password_hash = rodauth.send(:password_hash, password) if password
29
- set_password_hash(password_hash)
30
- end
31
-
32
- define_method(:set_password_hash) do |password_hash|
33
- if rodauth.account_password_hash_column
34
- public_send(:"#{rodauth.account_password_hash_column}=", password_hash)
35
- else
36
- if password_hash
37
- record = self.password_hash || build_password_hash
38
- record.public_send(:"#{rodauth.password_hash_column}=", password_hash)
39
- else
40
- self.password_hash&.mark_for_destruction
41
- end
42
- end
43
- end
44
- end
45
-
46
- def define_associations(model)
47
- define_password_hash_association(model) unless rodauth.account_password_hash_column
48
-
49
- feature_associations.each do |association|
50
- define_association(model, **association)
51
- end
52
- end
53
-
54
- def define_password_hash_association(model)
55
- password_hash_id_column = rodauth.password_hash_id_column
56
- scope = -> { select(password_hash_id_column) } if rodauth.send(:use_database_authentication_functions?)
57
-
58
- define_association model,
59
- type: :has_one,
60
- name: :password_hash,
61
- table: rodauth.password_hash_table,
62
- foreign_key: password_hash_id_column,
63
- scope: scope,
64
- autosave: true
65
- end
66
-
67
- def define_association(model, type:, name:, table:, foreign_key:, scope: nil, **options)
68
- associated_model = Class.new(model.superclass)
69
- associated_model.table_name = table
70
- associated_model.belongs_to :account,
71
- class_name: model.name,
72
- foreign_key: foreign_key,
73
- inverse_of: name
74
-
75
- model.const_set(name.to_s.singularize.camelize, associated_model)
76
-
77
- model.public_send type, name, scope,
78
- class_name: associated_model.name,
79
- foreign_key: foreign_key,
80
- dependent: type == :has_many ? :delete_all : :delete,
81
- inverse_of: :account,
82
- **options,
83
- **association_options(name)
84
- end
85
-
86
- def feature_associations
87
- Rodauth::Rails::Model::Associations.call(rodauth)
88
- end
89
-
90
- def association_options(name)
91
- options = @association_options
92
- options = options.call(name) if options.respond_to?(:call)
93
- options || {}
94
- end
95
-
96
- def rodauth
97
- @auth_class.allocate
98
- end
99
- end
3
+ Model = Rodauth::Model
4
+ deprecate_constant :Model
100
5
  end
101
6
  end
@@ -1,5 +1,5 @@
1
1
  module Rodauth
2
2
  module Rails
3
- VERSION = "1.3.1"
3
+ VERSION = "1.4.2"
4
4
  end
5
5
  end
data/lib/rodauth/rails.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "rodauth/rails/version"
2
2
  require "rodauth/rails/railtie"
3
+ require "rodauth/model"
3
4
 
4
5
  module Rodauth
5
6
  module Rails
@@ -43,7 +44,7 @@ module Rodauth
43
44
  end
44
45
 
45
46
  def model(name = nil, **options)
46
- Rodauth::Rails::Model.new(app.rodauth!(name), **options)
47
+ Rodauth::Model.new(app.rodauth!(name), **options)
47
48
  end
48
49
 
49
50
  # routing constraint that requires authentication
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_dependency "rodauth", "~> 2.23"
21
21
  spec.add_dependency "roda", "~> 3.55"
22
22
  spec.add_dependency "sequel-activerecord_connection", "~> 1.1"
23
+ spec.add_dependency "rodauth-model", "~> 0.2"
23
24
  spec.add_dependency "tilt"
24
25
  spec.add_dependency "bcrypt"
25
26
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodauth-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-22 00:00:00.000000000 Z
11
+ date: 2022-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -72,6 +72,20 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '1.1'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rodauth-model
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.2'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.2'
75
89
  - !ruby/object:Gem::Dependency
76
90
  name: tilt
77
91
  requirement: !ruby/object:Gem::Requirement
@@ -167,26 +181,43 @@ files:
167
181
  - LICENSE.txt
168
182
  - README.md
169
183
  - lib/generators/rodauth/install_generator.rb
170
- - lib/generators/rodauth/migration/account_expiration.erb
171
- - lib/generators/rodauth/migration/active_sessions.erb
172
- - lib/generators/rodauth/migration/audit_logging.erb
173
- - lib/generators/rodauth/migration/base.erb
174
- - lib/generators/rodauth/migration/disallow_password_reuse.erb
175
- - lib/generators/rodauth/migration/email_auth.erb
176
- - lib/generators/rodauth/migration/jwt_refresh.erb
177
- - lib/generators/rodauth/migration/lockout.erb
178
- - lib/generators/rodauth/migration/otp.erb
179
- - lib/generators/rodauth/migration/password_expiration.erb
180
- - lib/generators/rodauth/migration/recovery_codes.erb
181
- - lib/generators/rodauth/migration/remember.erb
182
- - lib/generators/rodauth/migration/reset_password.erb
183
- - lib/generators/rodauth/migration/single_session.erb
184
- - lib/generators/rodauth/migration/sms_codes.erb
185
- - lib/generators/rodauth/migration/verify_account.erb
186
- - lib/generators/rodauth/migration/verify_login_change.erb
187
- - lib/generators/rodauth/migration/webauthn.erb
184
+ - lib/generators/rodauth/migration/active_record/account_expiration.erb
185
+ - lib/generators/rodauth/migration/active_record/active_sessions.erb
186
+ - lib/generators/rodauth/migration/active_record/audit_logging.erb
187
+ - lib/generators/rodauth/migration/active_record/base.erb
188
+ - lib/generators/rodauth/migration/active_record/disallow_password_reuse.erb
189
+ - lib/generators/rodauth/migration/active_record/email_auth.erb
190
+ - lib/generators/rodauth/migration/active_record/jwt_refresh.erb
191
+ - lib/generators/rodauth/migration/active_record/lockout.erb
192
+ - lib/generators/rodauth/migration/active_record/otp.erb
193
+ - lib/generators/rodauth/migration/active_record/password_expiration.erb
194
+ - lib/generators/rodauth/migration/active_record/recovery_codes.erb
195
+ - lib/generators/rodauth/migration/active_record/remember.erb
196
+ - lib/generators/rodauth/migration/active_record/reset_password.erb
197
+ - lib/generators/rodauth/migration/active_record/single_session.erb
198
+ - lib/generators/rodauth/migration/active_record/sms_codes.erb
199
+ - lib/generators/rodauth/migration/active_record/verify_account.erb
200
+ - lib/generators/rodauth/migration/active_record/verify_login_change.erb
201
+ - lib/generators/rodauth/migration/active_record/webauthn.erb
202
+ - lib/generators/rodauth/migration/sequel/account_expiration.erb
203
+ - lib/generators/rodauth/migration/sequel/active_sessions.erb
204
+ - lib/generators/rodauth/migration/sequel/audit_logging.erb
205
+ - lib/generators/rodauth/migration/sequel/base.erb
206
+ - lib/generators/rodauth/migration/sequel/disallow_password_reuse.erb
207
+ - lib/generators/rodauth/migration/sequel/email_auth.erb
208
+ - lib/generators/rodauth/migration/sequel/jwt_refresh.erb
209
+ - lib/generators/rodauth/migration/sequel/lockout.erb
210
+ - lib/generators/rodauth/migration/sequel/otp.erb
211
+ - lib/generators/rodauth/migration/sequel/password_expiration.erb
212
+ - lib/generators/rodauth/migration/sequel/recovery_codes.erb
213
+ - lib/generators/rodauth/migration/sequel/remember.erb
214
+ - lib/generators/rodauth/migration/sequel/reset_password.erb
215
+ - lib/generators/rodauth/migration/sequel/single_session.erb
216
+ - lib/generators/rodauth/migration/sequel/sms_codes.erb
217
+ - lib/generators/rodauth/migration/sequel/verify_account.erb
218
+ - lib/generators/rodauth/migration/sequel/verify_login_change.erb
219
+ - lib/generators/rodauth/migration/sequel/webauthn.erb
188
220
  - lib/generators/rodauth/migration_generator.rb
189
- - lib/generators/rodauth/migration_helpers.rb
190
221
  - lib/generators/rodauth/templates/INSTRUCTIONS
191
222
  - lib/generators/rodauth/templates/app/controllers/rodauth_controller.rb
192
223
  - lib/generators/rodauth/templates/app/mailers/rodauth_mailer.rb
@@ -256,7 +287,6 @@ files:
256
287
  - lib/rodauth/rails/feature/render.rb
257
288
  - lib/rodauth/rails/middleware.rb
258
289
  - lib/rodauth/rails/model.rb
259
- - lib/rodauth/rails/model/associations.rb
260
290
  - lib/rodauth/rails/railtie.rb
261
291
  - lib/rodauth/rails/tasks.rake
262
292
  - lib/rodauth/rails/test.rb
@@ -1,77 +0,0 @@
1
- require "erb"
2
-
3
- module Rodauth
4
- module Rails
5
- module Generators
6
- module MigrationHelpers
7
- attr_reader :migration_class_name
8
-
9
- def migration_template(source, destination = File.basename(source))
10
- @migration_class_name = destination.chomp(".rb").camelize
11
-
12
- super source, File.join(db_migrate_path, destination)
13
- end
14
-
15
- private
16
-
17
- def migration_content
18
- migration_features
19
- .select { |feature| File.exist?("#{__dir__}/migration/#{feature}.erb") }
20
- .map { |feature| File.read("#{__dir__}/migration/#{feature}.erb") }
21
- .map { |content| erb_eval(content) }
22
- .join("\n")
23
- .indent(4)
24
- end
25
-
26
- def activerecord_adapter
27
- if ActiveRecord::Base.respond_to?(:connection_db_config)
28
- ActiveRecord::Base.connection_db_config.adapter
29
- else
30
- ActiveRecord::Base.connection_config.fetch(:adapter)
31
- end
32
- end
33
-
34
- def migration_version
35
- return unless ActiveRecord.version >= Gem::Version.new("5.0")
36
-
37
- "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
38
- end
39
-
40
- def db_migrate_path
41
- return "db/migrate" unless ActiveRecord.version >= Gem::Version.new("5.0")
42
-
43
- super
44
- end
45
-
46
- def primary_key_type(key = :id)
47
- generators = ::Rails.application.config.generators
48
- column_type = generators.options[:active_record][:primary_key_type]
49
-
50
- return unless column_type
51
-
52
- if key
53
- ", #{key}: :#{column_type}"
54
- else
55
- column_type
56
- end
57
- end
58
-
59
- def erb_eval(content)
60
- if ERB.version[/\d+\.\d+\.\d+/].to_s >= "2.2.0"
61
- ERB.new(content, trim_mode: "-").result(binding)
62
- else
63
- ERB.new(content, 0, "-").result(binding)
64
- end
65
- end
66
-
67
- def current_timestamp
68
- if ActiveRecord.version >= Gem::Version.new("5.0")
69
- %(-> { "CURRENT_TIMESTAMP" })
70
- else
71
- %(OpenStruct.new(quoted_id: "CURRENT_TIMESTAMP"))
72
- end
73
- end
74
- end
75
- end
76
- end
77
- end