Class: FactoryBot::Instrumentation::ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Includes:
ActionController::HttpAuthentication::Basic::ControllerMethods, ActionController::HttpAuthentication::Digest::ControllerMethods, ActionController::HttpAuthentication::Token::ControllerMethods
Defined in:
app/controllers/factory_bot/instrumentation/application_controller.rb

Overview

A base engine application controller.

Direct Known Subclasses

RootController

Instance Method Summary collapse

Instance Method Details

#basic_auth(username:, password:, realm: 'Instrumentation') ⇒ Object

A simple shortcut for Basic Auth on Rails 4.2+. Just configure the username and password and you’re ready to check the current request.

Parameters:

  • username (String)

    the required user name

  • password (String)

    the required password of the user

  • realm (String) (defaults to: 'Instrumentation')

    the authentication realm



34
35
36
37
38
39
40
41
42
# File 'app/controllers/factory_bot/instrumentation/application_controller.rb', line 34

def basic_auth(username:, password:, realm: 'Instrumentation')
  authenticate_or_request_with_http_basic(realm) \
    do |given_name, given_password|
    ActiveSupport::SecurityUtils.secure_compare(given_name, username) &
      ActiveSupport::SecurityUtils.secure_compare(
        given_password, password
      )
  end
end

#groupsHash{Regexp => String}

Map all the configured scenario groups to a usable hash.

Returns:

  • (Hash{Regexp => String})

    the group mapping



72
73
74
75
76
# File 'app/controllers/factory_bot/instrumentation/application_controller.rb', line 72

def groups
  (instrumentation['groups'] || {}).transform_keys do |key|
    Regexp.new(Regexp.quote(key))
  end
end

#instrumentationHash{String => Mixed}

Unfortunately Rails.configuration.instrumentation is only read once and do not hot-reload on changes. That’s why we read this file manually to get always a fresh state.

Returns:

  • (Hash{String => Mixed})

    the instrumentation scenarios



49
50
51
52
53
54
# File 'app/controllers/factory_bot/instrumentation/application_controller.rb', line 49

def instrumentation
  config_file = FactoryBot::Instrumentation.configuration.config_file.to_s
  template = ERB.new(Pathname.new(config_file).read)
  load_method = YAML.respond_to?(:unsafe_load) ? :unsafe_load : :load
  YAML.send(load_method, template.result(binding))[Rails.env] || {}
end

#scenario_group(name) ⇒ String

Fetch the group name for a given scenario name. This will utilize the SCENARIO_GROUPS map.

Parameters:

  • name (String)

    the scenario name

Returns:

  • (String)

    the group name



83
84
85
86
87
88
89
# File 'app/controllers/factory_bot/instrumentation/application_controller.rb', line 83

def scenario_group(name)
  groups.map do |pattern, group_name|
    next unless pattern.match? name

    group_name
  end.compact.first || 'Various'
end

#scenariosHash{String => Array}

Map all the instrumentation scenarios into groups and pass them back.

Returns:

  • (Hash{String => Array})

    the grouped scenarios



59
60
61
62
63
64
65
66
67
# File 'app/controllers/factory_bot/instrumentation/application_controller.rb', line 59

def scenarios
  res = instrumentation['scenarios'] || []
  res.each_with_object({}) do |scenario, memo|
    group = scenario_group(scenario['name'])
    scenario['group'] = group
    memo[group] = [] unless memo.key? group
    memo[group] << scenario
  end
end