Class: Contrast::Framework::Rails::Support

Inherits:
BaseSupport show all
Extended by:
Patch::Support
Defined in:
lib/contrast/framework/rails/support.rb

Overview

Used when Rails is present to define framework specific behavior

Constant Summary collapse

RAILS_MODULE_NAME_VERSION =
Gem::Version.new('6.0.0')
AC_INSTANCE =
'action_controller.instance'

Class Method Summary collapse

Methods included from Patch::Support

after_load_patches, before_load_patches!

Methods inherited from BaseSupport

after_load_patches, before_load_patches!

Class Method Details

.application_nameObject



27
28
29
30
31
32
33
34
# File 'lib/contrast/framework/rails/support.rb', line 27

def application_name
  app_class = ::Rails.application.cs__class
  # Rails version 6.0.0 deprecated Rails::Application#parent_name, in Rails 6.1.0 that method will be removed entirely
  # and instead we need to use parent_module_name
  return app_class.parent_module_name if Gem::Version.new(::Rails.version) >= RAILS_MODULE_NAME_VERSION

  app_class.parent_name
end

.application_rootObject



36
37
38
# File 'lib/contrast/framework/rails/support.rb', line 36

def application_root
  ::Rails.root
end

.collect_routesObject



44
45
46
# File 'lib/contrast/framework/rails/support.rb', line 44

def collect_routes
  find_all_routes(::Rails.application, [])
end

.current_route(request) ⇒ Contrast::Api::Dtm::RouteCoverage

Find the current route, based on the provided Request wrapper



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/contrast/framework/rails/support.rb', line 51

def current_route request
  return unless ::Rails.cs__respond_to?(:application)

  # returns array of arrays [[match_data, path_parameters, route]], sorted by
  # precedence
  # match_data: ActionDispatch::Journey::Path::Pattern::MatchData
  # path_parameters: hash of various things
  # route: ActionDispatch::Journey::Route
  full_routes = ::Rails.application.routes.router.send(:find_routes, request.rack_request)
  return if full_routes.empty?

  full_route = full_routes[0]

  # the route is directly implemented within the application
  if direct_route?(full_route)
    route = full_route[2] # route w/ highest precedence
    Contrast::Api::Dtm::RouteCoverage.from_action_dispatch_journey(route)
  else
    engine_route(full_route, request)
  end
rescue StandardError => _e
  nil
end

.detection_classObject



19
20
21
# File 'lib/contrast/framework/rails/support.rb', line 19

def detection_class
  'Rails'
end

.retrieve_request(env) ⇒ Object



75
76
77
78
# File 'lib/contrast/framework/rails/support.rb', line 75

def retrieve_request env
  rails_env = ::Rails.application.env_config.merge(env)
  ::ActionDispatch::Request.new(rails_env || env)
end

.server_typeObject



40
41
42
# File 'lib/contrast/framework/rails/support.rb', line 40

def server_type
  'rails'
end

.streaming?(env) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/contrast/framework/rails/support.rb', line 81

def streaming? env
  return false unless defined?(::ActionController::Live)

  env[AC_INSTANCE].cs__class.included_modules.include?(::ActionController::Live)
end

.versionObject



23
24
25
# File 'lib/contrast/framework/rails/support.rb', line 23

def version
  ::Rails.version
end