class Shoulda::Matchers::ActionController::PermitMatcher

@private

Attributes

action[R]
context[R]
controller[R]
double_collections_by_parameter_name[R]
expected_permitted_parameter_names[R]
parameters_double_registry[R]
request_params[R]
stubbed_params[W]
subparameter_name[R]
verb[R]

Public Class Methods

new(expected_permitted_parameter_names) click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 211
def initialize(expected_permitted_parameter_names)
  @expected_permitted_parameter_names =
    expected_permitted_parameter_names
  @action = nil
  @verb = nil
  @request_params = {}
  @subparameter_name = nil
  @parameters_double_registry = CompositeParametersDoubleRegistry.new
end

Public Instance Methods

add_params(params) click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 228
def add_params(params)
  request_params.merge!(params)
  self
end
description() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 243
def description
  "(for #{verb.upcase} ##{action}) " + expectation
end
failure_message() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 262
def failure_message
  "Expected #{verb.upcase} ##{action} to #{expectation},"\
  "\nbut #{reality}."
end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 267
def failure_message_when_negated
  "Expected #{verb.upcase} ##{action} not to #{expectation},"\
  "\nbut it did."
end
for(action, options = {}) click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 221
def for(action, options = {})
  @action = action
  @verb = options.fetch(:verb, default_verb)
  @request_params = options.fetch(:params, {})
  self
end
in_context(context) click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 238
def in_context(context)
  @context = context
  self
end
matches?(controller) click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 247
def matches?(controller)
  @controller = controller
  ensure_action_and_verb_present!

  parameters_double_registry.register

  Doublespeak.with_doubles_activated do
    params = { params: request_params }

    context.__send__(verb, action, **params)
  end

  unpermitted_parameter_names.empty?
end
on(subparameter_name) click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 233
def on(subparameter_name)
  @subparameter_name = subparameter_name
  self
end

Protected Instance Methods

actual_permitted_parameter_names() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 305
def actual_permitted_parameter_names
  @_actual_permitted_parameter_names ||= begin
    options =
      if subparameter_name
        { for: subparameter_name }
      else
        {}
      end
    parameters_double_registry.permitted_parameter_names(options)
  end
end
default_verb() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 331
def default_verb
  case action
  when :create then :post
  when :update then RailsShim.verb_for_update
  end
end
ensure_action_and_verb_present!() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 321
def ensure_action_and_verb_present!
  if action.blank?
    raise ActionNotDefinedError
  end

  if verb.blank?
    raise VerbNotDefinedError
  end
end
expectation() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 278
def expectation
  message = 'restrict parameters '

  if subparameter_name
    message << "on #{subparameter_name.inspect} "
  end

  message << 'to '\
    "#{format_parameter_names(expected_permitted_parameter_names)}"

  message
end
format_parameter_names(parameter_names) click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 301
def format_parameter_names(parameter_names)
  parameter_names.map(&:inspect).to_sentence
end
parameter_names_as_sentence() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 338
def parameter_names_as_sentence
  expected_permitted_parameter_names.map(&:inspect).to_sentence
end
reality() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 291
def reality
  if actual_permitted_parameter_names.empty?
    'it did not restrict any parameters'
  else
    'the restricted parameters were '\
    "#{format_parameter_names(actual_permitted_parameter_names)}"\
    ' instead'
  end
end
unpermitted_parameter_names() click to toggle source
# File lib/shoulda/matchers/action_controller/permit_matcher.rb, line 317
def unpermitted_parameter_names
  expected_permitted_parameter_names - actual_permitted_parameter_names
end