Class: Shoulda::Matchers::ActiveModel::HaveModelTranslationsMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/beyond_the_api_test_helpers/shoulda/have_model_translations.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HaveModelTranslationsMatcher

Returns a new instance of HaveModelTranslationsMatcher.



10
11
12
# File 'lib/beyond_the_api_test_helpers/shoulda/have_model_translations.rb', line 10

def initialize(options = {})
  @options = options
end

Instance Method Details

#assert_translated_attribute(attribute) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/beyond_the_api_test_helpers/shoulda/have_model_translations.rb', line 36

def assert_translated_attribute(attribute)
  begin
    translated_attr = I18n.t('activerecord.attributes.' \
                             "#{model_class.to_s.underscore}.#{attribute}")
  rescue
    translated_attr = I18n.t("attributes.#{attribute}")
  end
  model_class.human_attribute_name(attribute) == translated_attr
end

#descriptionObject



56
57
58
# File 'lib/beyond_the_api_test_helpers/shoulda/have_model_translations.rb', line 56

def description
  'have model translations'
end

#failure_messageObject Also known as: failure_message_for_should



46
47
48
# File 'lib/beyond_the_api_test_helpers/shoulda/have_model_translations.rb', line 46

def failure_message
  "Expected #{expectation} (#{@missing})"
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



51
52
53
# File 'lib/beyond_the_api_test_helpers/shoulda/have_model_translations.rb', line 51

def failure_message_when_negated
  "Did not expect #{expectation}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/beyond_the_api_test_helpers/shoulda/have_model_translations.rb', line 14

def matches?(subject)
  @subject = subject
  matches_model_name? && matches_attributes?
end

#matches_attributes?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/beyond_the_api_test_helpers/shoulda/have_model_translations.rb', line 27

def matches_attributes?
  attributes = @options[:attributes]
  return true unless attributes
  attributes.each do |attribute|
    return false unless assert_translated_attribute(attribute)
  end
  true
end

#matches_model_name?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/beyond_the_api_test_helpers/shoulda/have_model_translations.rb', line 19

def matches_model_name?
  I18n.available_locales.each do |locale|
    I18n.locale = locale
    return false unless model_name.human == I18n.t("#{i18n_path}.one")
    return false unless model_name.human(count: 2) == I18n.t("#{i18n_path}.other")
  end
end