Module: SimpleCrud::RSpec::Helpers::Requests

Included in:
SimpleCrud::RSpec::Helpers
Defined in:
lib/simple_crud/rspec/helpers/requests.rb

Overview

Request params and format helpers shared by every action example.

Instance Method Summary collapse

Instance Method Details

#body_params(attrs) ⇒ Object

Wraps a request body under the model's strong-params key when params_key is configured (nested strong params), else passes it flat.



41
42
43
44
# File 'lib/simple_crud/rspec/helpers/requests.rb', line 41

def body_params(attrs)
  key = params_key
  key ? { key => attrs } : attrs
end

#format_param(action) ⇒ Object

Rails copies the controller-test format: kwarg into params, which collides with a model attribute named format. Only HTML requests need it, so leave params untouched for JSON.



17
18
19
# File 'lib/simple_crud/rspec/helpers/requests.rb', line 17

def format_param(action)
  check_html(action) ? :html : nil
end

#record_param(action, record, not_found: false) ⇒ Object

The params that identify a record for the given action, using the configured finder_key when the action has a custom finder.



33
34
35
36
37
# File 'lib/simple_crud/rspec/helpers/requests.rb', line 33

def record_param(action, record, not_found: false)
  key = check_finder(action) ? finder_key : :id
  value = not_found ? "nonexistent-#{key}" : record.public_send(key)
  { key => value }
end

#route_paramsObject

Extra params (e.g. a parent slug) added to every request, for nested resources like /projects/:project_slug/tasks.



23
24
25
# File 'lib/simple_crud/rspec/helpers/requests.rb', line 23

def route_params
  resolve(setting(:route_params))
end

#unprocessable_statusObject

:unprocessable_entity was renamed :unprocessable_content in Rack 3.1. Resolve the current Rack's canonical symbol for 422.



10
11
12
# File 'lib/simple_crud/rspec/helpers/requests.rb', line 10

def unprocessable_status
  Rack::Utils::SYMBOL_TO_STATUS_CODE.invert[422]
end

#with_route_params(attrs) ⇒ Object



27
28
29
# File 'lib/simple_crud/rspec/helpers/requests.rb', line 27

def with_route_params(attrs)
  route_params.merge(attrs)
end