Class: SimpleCrud::RSpec::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_crud/rspec/config.rb

Overview

Configures how the shared examples create records, authenticate requests and introspect policies/serializers. The defaults match the gem's own stack (Devise-JWT + FactoryBot + Pundit + ActiveModel Serializers), so apps on a different stack can override them via SimpleCrud::RSpec.configure instead of editing the examples.

Callable settings (authenticate, current_user, other_user, create_record, create_records, params_for, model_attributes, policy_class, serializer_class) run in the example-group context, so they can call helpers such as create, request and current_user.

Constant Summary collapse

DEFAULTS =
{
  authenticate: lambda {
    headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
    request.headers.merge!(Devise::JWT::TestHelpers.auth_headers(headers, current_user))
  },
  current_user: -> { create(:user) },
  other_user: -> { create(:user) },
  create_record: ->(klass, attributes) { create(klass, **attributes) },
  create_records: ->(klass, count, attributes) { create_list(klass, count, **attributes) },
  params_for: ->(klass) { attributes_for(klass) },
  owner_association: :user,
  model_attributes: -> { owner_association ? { owner_association => current_user } : {} },
  scoped_attributes: -> { model_attributes },
  other_scoped_attributes: lambda {
    attributes = model_attributes
    if owner_association && !attributes.is_a?(Hash)
      raise ArgumentError,
            'model_attributes must resolve to a Hash when owner_association is set; ' \
            'override other_scoped_attributes instead'
    end

    owner_association ? attributes.merge(owner_association => other_user) : {}
  },
  required_attribute: :name,
  required_error: "Name can't be blank",
  finder_key: :slug,
  params_key: nil,
  route_params: -> { {} },
  invalid_status: :ok,
  unauthenticated_status: :unauthorized,
  assert_html_template: true,
  policy_class: ->(klass) { "#{klass}Policy".constantize },
  serializer_class: ->(model) { "#{model.class}_serializer".classify.constantize },
  created_record_check: nil
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



64
65
66
# File 'lib/simple_crud/rspec/config.rb', line 64

def initialize
  @values = {}
end

Class Method Details

.instanceObject



59
60
61
# File 'lib/simple_crud/rspec/config.rb', line 59

def instance
  @instance ||= new
end