Class: FactoryBot::Instrumentation::RootController

Inherits:
ApplicationController show all
Defined in:
app/controllers/factory_bot/instrumentation/root_controller.rb

Overview

The Instrumentation engine controller with frontend and API actions.

Instance Method Summary collapse

Methods inherited from ApplicationController

#basic_auth, #groups, #instrumentation, #scenario_group, #scenarios

Instance Method Details

#createObject

Create a new entity with the given factory settings to create on demand dependencies for your testing needs. You can pass in requests without authentication in the following JSON format:

{
  "factory": "user",
  "traits": ["confirmed"],
  "overwrite": {
    "first_name": "Bernd",
    "last_name": "Schulze",
    "email": "bernd.schulze@example.com",
    "password": "secret"
  }
}

The result is the API v1 representation of the created entity.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/factory_bot/instrumentation/root_controller.rb', line 40

def create
  # Reload the factories to improve the test development experience.
  # In parallel request conditions this may lead to +Factory already
  # registered+ errors as this call is not thread safe as it seems,
  # so we retry it multiple times.
  with_retries(max_tries: 15) { FactoryBot.reload }
  # Call the factory construction with the user given parameters
  entity = FactoryBot.create(*factory_params)
  # Render the resulting entity with the configured rendering block
  FactoryBot::Instrumentation.configuration.render_entity.call(
    self, entity
  )
rescue StandardError => e
  # Handle any error gracefully with the configured error handler
  FactoryBot::Instrumentation.configuration.render_error.call(self, e)
end

#factory_paramsArray<Mixed>

Parse the given parameters from the request and build a valid FactoryBot options set.

Returns:

  • (Array<Mixed>)

    the FactoryBot options



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/factory_bot/instrumentation/root_controller.rb', line 61

def factory_params
  # Read the open-ended +overwrite+ hash unfiltered, as its keys and
  # values are arbitrary and cannot be described with strong
  # parameters.
  overwrite = params.to_unsafe_h.fetch(:overwrite, {})
                    .deep_symbolize_keys
  # Strip +overwrite+ before permitting the rest, otherwise it would
  # be reported as a spurious "Unpermitted parameters: :overwrite"
  # log warning.
  data = params.except(:overwrite).permit(:factory, traits: [])

  [
    data.fetch(:factory).to_sym,
    *data.fetch(:traits, []).map(&:to_sym),
    { **overwrite }
  ]
end

#indexObject

Show the instrumentation frontend which features the output of configured dynamic seeds scenarios. The frontend allows humans to generate new seed data on the fly.



17
18
19
20
21
22
# File 'app/controllers/factory_bot/instrumentation/root_controller.rb', line 17

def index
  @instrumentation = instrumentation
  @scenarios = scenarios
  @config = FactoryBot::Instrumentation.configuration
  render :index, layout: true
end