class Sanitize::Rails::Matchers::SanitizeFieldsMatcher

Actual matcher class

Attributes

fields[RW]
instance[RW]
options[RW]
sanitizer[RW]

Public Class Methods

new(*fields) click to toggle source

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

# File lib/sanitize/rails/matchers.rb, line 63
def initialize(*fields)
  self.options = fields.extract_options!
  self.sanitizer = ::Sanitize::Rails::Engine.method_for(fields)
  self.fields = fields
end

Public Instance Methods

description() click to toggle source
# File lib/sanitize/rails/matchers.rb, line 101
def description
  "sanitize #{should_helper}"
end
matches?(instance) click to toggle source

Actual match code

# File lib/sanitize/rails/matchers.rb, line 84
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) click to toggle source

Used to specify invalid text assigned to fields

# File lib/sanitize/rails/matchers.rb, line 70
def replacing(invalid)
  @invalid_changed = true
  @invalid = invalid
  self
end
with(valid) click to toggle source

Used to specify expected output for the invalid text

# File lib/sanitize/rails/matchers.rb, line 77
def with(valid)
  @valid_changed = true
  @valid = valid
  self
end

Private Instance Methods

attribute_values() click to toggle source
# File lib/sanitize/rails/matchers.rb, line 131
def attribute_values
  instance.attributes.slice(*fields.map(&:to_s))
end
custom_values?() click to toggle source
# File lib/sanitize/rails/matchers.rb, line 117
def custom_values?
  @invalid_changed && @invalid_changed
end
field_helper() click to toggle source
# File lib/sanitize/rails/matchers.rb, line 121
def field_helper
  "#{'field'.pluralize(fields.count)} #{fields.to_sentence}"
end
invalid_value() click to toggle source
# File lib/sanitize/rails/matchers.rb, line 109
def invalid_value
  @invalid ||= '<b>valid<br>'
end
should_helper() click to toggle source
# File lib/sanitize/rails/matchers.rb, line 125
def should_helper
  field_helper.tap do |desc|
    desc << " by replacing '#{invalid_value}' with '#{valid_value}'" if custom_values?
  end
end
valid_value() click to toggle source
# File lib/sanitize/rails/matchers.rb, line 113
def valid_value
  @valid ||= '<b>valid<br></b>'
end