class Ndd::RSpec::Rails::Matchers::Controller::HaveATranslatedFlash

Implements {#have_a_translated_flash}.

Public Class Methods

new(message) click to toggle source

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

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

Public Instance Methods

description() click to toggle source

@return [String] a description of this matcher.

# File lib/ndd/rspec/rails/matchers/controller/have_a_translated_flash.rb, line 38
def description
  description = "have a translated flash message for '#{@message}'"
  description << " on '#{@action}'" if @action.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/controller/have_a_translated_flash.rb, line 46
def failure_message
  message = "expected '#{subject_as_string}' to have a translated flash message for '#{@message}'\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?(controller) click to toggle source

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

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

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

# File lib/ndd/rspec/rails/matchers/controller/have_a_translated_flash.rb, line 21
def on_action(action)
  @action = action
  self
end

Private Instance Methods

subject_as_string() click to toggle source

@return [String] a human readable string of the subject of the error.

# File lib/ndd/rspec/rails/matchers/controller/have_a_translated_flash.rb, line 58
def subject_as_string
  @action.present? ? "#{@controller.class}##{@action}" : @controller.class.to_s
end
translated_in?(tested_locale) click to toggle source
# File lib/ndd/rspec/rails/matchers/controller/have_a_translated_flash.rb, line 87
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/controller/have_a_translated_flash.rb, line 62
def translation_keys
  @action.present? ? translation_keys_with_action : translation_keys_without_action
end
translation_keys_with_action() click to toggle source
# File lib/ndd/rspec/rails/matchers/controller/have_a_translated_flash.rb, line 66
def translation_keys_with_action
  controller_key = @controller.class.name.underscore
  message_key = @message.to_s
  action_key = @action.to_s
  %W[
    actioncontroller.#{controller_key}.#{action_key}.flash.#{message_key}
    actioncontroller.#{controller_key}.flash.#{message_key}
    actioncontroller.#{action_key}.flash.#{message_key}
    actioncontroller.flash.#{message_key}
  ]
end
translation_keys_without_action() click to toggle source
# File lib/ndd/rspec/rails/matchers/controller/have_a_translated_flash.rb, line 78
def translation_keys_without_action
  controller_key = @controller.class.name.underscore
  message_key = @message.to_s
  %W[
    actioncontroller.#{controller_key}.flash.#{message_key}
    actioncontroller.flash.#{message_key}
  ]
end