Class: Sanitize::Rails::Matchers::SanitizeFieldsMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/sanitize/rails/matchers.rb

Overview

Actual matcher class

Instance Method Summary collapse

Constructor Details

#initialize(*fields) ⇒ SanitizeFieldsMatcher

Take an array of fields to check, they must respect the same order given in model ‘sanitize` call



37
38
39
40
41
# File 'lib/sanitize/rails/matchers.rb', line 37

def initialize(*fields)
  self.options = fields.extract_options!
  self.sanitizer = ::Sanitize::Rails::Engine.method_for(fields)
  self.fields = fields
end

Instance Method Details

#descriptionObject



76
77
78
# File 'lib/sanitize/rails/matchers.rb', line 76

def description
  "sanitize #{should_helper}"
end

#failure_message_for_shouldObject



68
69
70
# File 'lib/sanitize/rails/matchers.rb', line 68

def failure_message_for_should
  "Expected #{should_helper} to return sanitized value '#{valid_value}', got '#{attribute_values}'"
end

#failure_message_for_should_notObject



72
73
74
# File 'lib/sanitize/rails/matchers.rb', line 72

def failure_message_for_should_not
  "Expected #{field_helper} not to be sanitized"
end

#matches?(instance) ⇒ Boolean

Actual match code

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
# File 'lib/sanitize/rails/matchers.rb', line 58

def matches?(instance)
  self.instance = instance
  # assign invalid value to each field
  fields.each { |field| instance.send("#{field}=", invalid_value) }
  # sanitize the object calling the method
  instance.send(sanitizer) rescue nil
  # check expectation on results
  fields.all? { |field| valid_value == instance.send(field) }
end

#replacing(invalid) ⇒ Object

Used to specify invalid text assigned to fields



44
45
46
47
48
# File 'lib/sanitize/rails/matchers.rb', line 44

def replacing(invalid)
  @invalid_changed = true
  @invalid = invalid
  self
end

#with(valid) ⇒ Object

Used to specify expected output for the invalid text



51
52
53
54
55
# File 'lib/sanitize/rails/matchers.rb', line 51

def with(valid)
  @valid_changed = true
  @valid = valid
  self
end