Module: RailsAgents::Compat

Defined in:
lib/rails_agents/compat.rb

Overview

Compatibility helpers so the engine can run on Rails 6.1+ (and 7/8). Rails 3–5 are not supported: they cannot run on Ruby 3.2+, which this gem requires.

Class Method Summary collapse

Class Method Details

.application_nameObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rails_agents/compat.rb', line 32

def application_name
  return "App" unless defined?(::Rails)

  klass = ::Rails.application.class
  if klass.respond_to?(:module_parent_name)
    klass.module_parent_name
  elsif klass.respond_to?(:parent_name)
    klass.parent_name
  else
    klass.name.to_s.split("::").first
  end
rescue StandardError
  "App"
end

.at_least?(major, minor = 0) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rails_agents/compat.rb', line 21

def at_least?(major, minor = 0)
  rails_major > major || (rails_major == major && rails_minor >= minor)
end

.rails_majorObject



9
10
11
12
13
# File 'lib/rails_agents/compat.rb', line 9

def rails_major
  ::Rails::VERSION::MAJOR
rescue StandardError
  0
end

.rails_minorObject



15
16
17
18
19
# File 'lib/rails_agents/compat.rb', line 15

def rails_minor
  ::Rails::VERSION::MINOR
rescue StandardError
  0
end

.redirect_optionsObject

Rails 7+ open-redirect protection requires allow_other_host: true.



48
49
50
# File 'lib/rails_agents/compat.rb', line 48

def redirect_options
  at_least?(7, 0) ? { allow_other_host: true } : {}
end

.skip_csrf!(controller_class) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rails_agents/compat.rb', line 52

def skip_csrf!(controller_class)
  if controller_class.respond_to?(:skip_forgery_protection)
    controller_class.skip_forgery_protection
  elsif controller_class.respond_to?(:skip_before_action)
    controller_class.skip_before_action :verify_authenticity_token, raise: false
  end
end

.zeitwerk?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/rails_agents/compat.rb', line 25

def zeitwerk?
  defined?(::Rails) &&
    ::Rails.respond_to?(:autoloaders) &&
    ::Rails.autoloaders.respond_to?(:main) &&
    ::Rails.autoloaders.main
end