class Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedError

Implements {#have_a_translated_error}.

Public Class Methods

new(error) click to toggle source

@param error [String|Symbol] the error to test.

Calls superclass method
# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 13
def initialize(error)
  super()
  @error = error
end

Public Instance Methods

description() click to toggle source

@return [String] a description of this matcher.

# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 38
def description
  description = "have a translated error message for '#{@error}'"
  description << " on '#{@attribute}'" if @attribute.present?
  description << " in #{locales_as_string(@tested_locales)}"
  description
end
failure_message() click to toggle source

@return [String] details about the failure of this matcher.

# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 46
def failure_message
  message = "expected '#{subject_as_string}' to have a translated error message for '#{@error}'\n"
  message << "but none of the following keys was found:\n"
  message << "#{translation_keys.map { |l| "  - #{l}" }.join("\n")}\n"
  message << "for the locales: #{locales_as_string(@failed_locales)}"
  message
end
matches?(model) click to toggle source

@param model [Object] the model being tested. @return [Boolean] true if the error has an associated translation, false otherwise.

# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 28
def matches?(model)
  @model = model
  @failed_locales = []
  @tested_locales.each do |tested_locale|
    @failed_locales << tested_locale unless translated_in?(tested_locale)
  end
  @failed_locales.empty?
end
on_attribute(attribute) click to toggle source

Set the attribute of the error to test. @param attribute [String|Symbol] the attribute associated to the error to test. @return self

# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 21
def on_attribute(attribute)
  @attribute = attribute
  self
end

Private Instance Methods

subject_as_string() click to toggle source

@return [String] a human readable string of the subject of the error, being a class or an attribute.

# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 58
def subject_as_string
  @attribute.present? ? "#{@model.class}##{@attribute}" : @model.class.to_s
end
translated_in?(tested_locale) click to toggle source
# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 89
def translated_in?(tested_locale)
  translation_keys.each do |translation_key|
    return true if translated?(tested_locale, translation_key)
  end
  false
end
translation_keys() click to toggle source
# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 62
def translation_keys
  @attribute.present? ? translation_keys_with_attribute : translation_keys_without_attribute
end
translation_keys_with_attribute() click to toggle source
# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 66
def translation_keys_with_attribute
  model_key = @model.class.name.underscore
  error_key = @error.to_s
  attribute_key = @attribute.to_s
  %W[
    activerecord.errors.models.#{model_key}.attributes.#{attribute_key}.#{error_key}
    activerecord.errors.models.#{model_key}.#{error_key}
    activerecord.errors.messages.#{error_key}
    errors.attributes.#{attribute_key}.#{error_key}
    errors.messages.#{error_key}
  ]
end
translation_keys_without_attribute() click to toggle source
# File lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb, line 79
def translation_keys_without_attribute
  model_key = @model.class.name.underscore
  error_key = @error.to_s
  %W[
    activerecord.errors.models.#{model_key}.#{error_key}
    activerecord.errors.messages.#{error_key}
    errors.messages.#{error_key}
  ]
end