Class: Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedAttribute

Inherits:
TranslationMatcher show all
Defined in:
lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb

Overview

Instance Method Summary collapse

Methods inherited from TranslationMatcher

#in_available_locales, #in_default_locale

Constructor Details

#initialize(attribute) ⇒ HaveATranslatedAttribute

Returns a new instance of HaveATranslatedAttribute.

Parameters:

  • attribute (String|Symbol)

    the attribute to test.


13
14
15
16
# File 'lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb', line 13

def initialize(attribute)
  super()
  @attribute = attribute
end

Instance Method Details

#descriptionString

Returns a description of this matcher.

Returns:

  • (String)

    a description of this matcher.


30
31
32
# File 'lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb', line 30

def description
  "have a translated attribute name for '#{@attribute}' in #{locales_as_string(@tested_locales)}"
end

#failure_messageString

Returns details about the failure of this matcher.

Returns:

  • (String)

    details about the failure of this matcher.


35
36
37
38
39
40
41
# File 'lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb', line 35

def failure_message
  message = ''
  message << "expected '#{@model.class}' to have a translated attribute name for '#{@attribute}'\n"
  message << "but the '#{translation_key}' key was not found\n"
  message << "for the locales: #{locales_as_string(@failed_locales)}"
  message
end

#matches?(model) ⇒ Boolean

Returns true if the attribute has an associated translation, false otherwise.

Parameters:

  • model (Object)

    the model being tested.

Returns:

  • (Boolean)

    true if the attribute has an associated translation, false otherwise.


20
21
22
23
24
25
26
27
# File 'lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb', line 20

def matches?(model)
  @model = model
  @failed_locales = []
  @tested_locales.each do |tested_locale|
    @failed_locales << tested_locale unless translated?(tested_locale, translation_key)
  end
  @failed_locales.empty?
end