Class: FactoryBot::Instrumentation::ApplicationController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- FactoryBot::Instrumentation::ApplicationController
- 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
Instance Method Summary collapse
-
#basic_auth(username:, password:, realm: 'Instrumentation') ⇒ Object
A simple shortcut for Basic Auth on Rails 4.2+.
-
#groups ⇒ Hash{Regexp => String}
Map all the configured scenario groups to a usable hash.
-
#instrumentation ⇒ Hash{String => Mixed}
Unfortunately
Rails.configuration.instrumentationis only read once and do not hot-reload on changes. -
#scenario_group(name) ⇒ String
Fetch the group name for a given scenario name.
-
#scenarios ⇒ Hash{String => Array}
Map all the instrumentation scenarios into groups and pass them back.
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.
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 |
#groups ⇒ Hash{Regexp => String}
Map all the configured scenario groups to a usable hash.
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 |
#instrumentation ⇒ Hash{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.
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.
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 |
#scenarios ⇒ Hash{String => Array}
Map all the instrumentation scenarios into groups and pass them back.
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 |