Module: SimpleCrud::RSpec::Helpers
- Defined in:
- lib/simple_crud/rspec/helpers.rb
Overview
Helpers available to the shared examples (and to apps that include them). Everything app-specific is delegated to SimpleCrud::RSpec::Config so it can be overridden without editing the examples.
Instance Method Summary collapse
- #authenticate_request ⇒ Object
-
#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.
- #config ⇒ Object
- #create_record(klass, attributes) ⇒ Object
- #create_records(klass, count, attributes) ⇒ Object
- #current_user ⇒ Object
- #get_option(method, option) ⇒ Object
- #make_policies_fail(method) ⇒ Object
- #make_policies_succeed(method) ⇒ Object
- #model ⇒ Object
- #model_attributes ⇒ Object
- #model_class ⇒ Object
- #model_class_object ⇒ Object
- #model_params ⇒ Object
- #model_serializer ⇒ Object
- #owner_foreign_key ⇒ Object
-
#owner_params ⇒ Object
The owner association attributed to current_user, if the model has one.
- #params_for(klass) ⇒ Object
- #policy_class_object ⇒ Object
-
#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.
- #request_format(action) ⇒ Object
- #resolve(value) ⇒ Object
-
#route_params ⇒ Object
Extra params (e.g. a parent slug) added to every request, for nested resources like /classrooms/:classroom_slug/assignments.
-
#unprocessable_status ⇒ Object
:unprocessable_entity was renamed :unprocessable_content in Rack 3.1.
-
#with_config_override(setting, value) ⇒ Object
Runs the block with a config setting temporarily overridden, restoring the original value afterwards (even if the block raises).
- #with_route_params(attrs) ⇒ Object
Instance Method Details
#authenticate_request ⇒ Object
70 71 72 |
# File 'lib/simple_crud/rspec/helpers.rb', line 70 def authenticate_request instance_exec(&config.authenticate) end |
#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.
103 104 105 106 |
# File 'lib/simple_crud/rspec/helpers.rb', line 103 def body_params(attrs) key = params_key key ? { key => attrs } : attrs end |
#config ⇒ Object
9 10 11 |
# File 'lib/simple_crud/rspec/helpers.rb', line 9 def config SimpleCrud::RSpec::Config.instance end |
#create_record(klass, attributes) ⇒ Object
50 51 52 |
# File 'lib/simple_crud/rspec/helpers.rb', line 50 def create_record(klass, attributes) instance_exec(klass, attributes, &config.create_record) end |
#create_records(klass, count, attributes) ⇒ Object
54 55 56 |
# File 'lib/simple_crud/rspec/helpers.rb', line 54 def create_records(klass, count, attributes) instance_exec(klass, count, attributes, &config.create_records) end |
#current_user ⇒ Object
66 67 68 |
# File 'lib/simple_crud/rspec/helpers.rb', line 66 def current_user @current_user ||= instance_exec(&config.current_user) end |
#get_option(method, option) ⇒ Object
13 14 15 |
# File 'lib/simple_crud/rspec/helpers.rb', line 13 def get_option(method, option) described_class.instance_variable_get(:@simple_crud_metadata)[method][option] end |
#make_policies_fail(method) ⇒ Object
116 117 118 119 |
# File 'lib/simple_crud/rspec/helpers.rb', line 116 def make_policies_fail(method) allow(policy_class_object).to receive(:new) .and_return(instance_double(policy_class_object, "#{method}?" => false)) end |
#make_policies_succeed(method) ⇒ Object
121 122 123 124 |
# File 'lib/simple_crud/rspec/helpers.rb', line 121 def make_policies_succeed(method) allow(policy_class_object).to receive(:new) .and_return(instance_double(policy_class_object, "#{method}?" => true)) end |
#model ⇒ Object
41 42 43 |
# File 'lib/simple_crud/rspec/helpers.rb', line 41 def model @model ||= create_record(model_class, model_attributes) end |
#model_attributes ⇒ Object
45 46 47 48 |
# File 'lib/simple_crud/rspec/helpers.rb', line 45 def model_attributes association = resolve(config.owner_association) association ? { association => current_user } : {} end |
#model_class ⇒ Object
32 33 34 35 |
# File 'lib/simple_crud/rspec/helpers.rb', line 32 def model_class described_class.to_s.split('::') .last.sub('Controller', '').singularize.underscore end |
#model_class_object ⇒ Object
37 38 39 |
# File 'lib/simple_crud/rspec/helpers.rb', line 37 def model_class_object model_class.classify.constantize end |
#model_params ⇒ Object
62 63 64 |
# File 'lib/simple_crud/rspec/helpers.rb', line 62 def model_params @model_params ||= params_for(model_class) end |
#model_serializer ⇒ Object
112 113 114 |
# File 'lib/simple_crud/rspec/helpers.rb', line 112 def model_serializer defined?(serializer) ? serializer : instance_exec(model, &config.serializer_class) end |
#owner_foreign_key ⇒ Object
74 75 76 |
# File 'lib/simple_crud/rspec/helpers.rb', line 74 def owner_foreign_key :"#{owner_association}_id" end |
#owner_params ⇒ Object
The owner association attributed to current_user, if the model has one.
79 80 81 |
# File 'lib/simple_crud/rspec/helpers.rb', line 79 def owner_params owner_association ? { owner_foreign_key => current_user.id } : {} end |
#params_for(klass) ⇒ Object
58 59 60 |
# File 'lib/simple_crud/rspec/helpers.rb', line 58 def params_for(klass) instance_exec(klass, &config.params_for) end |
#policy_class_object ⇒ Object
108 109 110 |
# File 'lib/simple_crud/rspec/helpers.rb', line 108 def policy_class_object instance_exec(model_class_object, &config.policy_class) 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.
95 96 97 98 99 |
# File 'lib/simple_crud/rspec/helpers.rb', line 95 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 |
#request_format(action) ⇒ Object
126 127 128 |
# File 'lib/simple_crud/rspec/helpers.rb', line 126 def request_format(action) check_html(action) ? :html : :json end |
#resolve(value) ⇒ Object
130 131 132 |
# File 'lib/simple_crud/rspec/helpers.rb', line 130 def resolve(value) value.is_a?(Proc) ? instance_exec(&value) : value end |
#route_params ⇒ Object
Extra params (e.g. a parent slug) added to every request, for nested resources like /classrooms/:classroom_slug/assignments.
85 86 87 |
# File 'lib/simple_crud/rspec/helpers.rb', line 85 def route_params resolve(config.route_params) end |
#unprocessable_status ⇒ Object
:unprocessable_entity was renamed :unprocessable_content in Rack 3.1. Resolve the current Rack's canonical symbol for 422.
19 20 21 |
# File 'lib/simple_crud/rspec/helpers.rb', line 19 def unprocessable_status Rack::Utils::SYMBOL_TO_STATUS_CODE.invert[422] end |
#with_config_override(setting, value) ⇒ Object
Runs the block with a config setting temporarily overridden, restoring the original value afterwards (even if the block raises). Useful when a controller's spec needs a different setting than the app-wide default.
137 138 139 140 141 142 143 144 |
# File 'lib/simple_crud/rspec/helpers.rb', line 137 def with_config_override(setting, value) config_instance = SimpleCrud::RSpec::Config.instance original = config_instance.public_send(setting) SimpleCrud::RSpec.configure { |c| c.public_send("#{setting}=", value) } yield ensure SimpleCrud::RSpec.configure { |c| c.public_send("#{setting}=", original) } end |
#with_route_params(attrs) ⇒ Object
89 90 91 |
# File 'lib/simple_crud/rspec/helpers.rb', line 89 def with_route_params(attrs) route_params.merge(attrs) end |