ndd-rspec-rails 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3fdeed587f4e9c6e3f07ee32e556e327d98a45ba
4
- data.tar.gz: 11a74da50a412f8a3fe174af4c95b72431a2beaa
3
+ metadata.gz: fb94dd2da1098be2a2f3d48c83368ff5f1602926
4
+ data.tar.gz: df64d93cc0963f6e481a63825354cfd3560d83a6
5
5
  SHA512:
6
- metadata.gz: e00104c957bb678794811759e82beea6c4954edd08348a5f8b1e6b65c47e1fbaa7a64a31d57fbb618debac176a33d1e6bd783f1295f9eb577263d468722200f8
7
- data.tar.gz: 39656673e2e03d69bb350582cfee512482116a66f924edd112a1818577f7686d128350acaa34ce7e8fad8fc0ff917b4edfcb188c07664eaa2faa233a229c8104
6
+ metadata.gz: 4d662d5a3ebb2700794504f5fc15b8c24fee69e0a6e3ad929926df404e595affa908b8129ffaf4be74352a048714187d09274d599da7b8365898f3e09cb1f8c7
7
+ data.tar.gz: 405873989113b8c9ea54ded246968ff8433359a3252c8066c1c9ed926d38169e239457c00e81ede37e1736723df75797fc3aaaa34e0788d570db897fd33318c8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
- # NDD RSpec Rails
1
+ # NDD RSpec Rails changelog
2
+
3
+ ## Version 0.2.0
4
+
5
+ - add `have_a_translated_flash` matcher
2
6
 
3
7
  ## Version 0.1.0
4
8
 
5
- - initial commit
9
+ - initial commit with matchers:
10
+ - `have_a_translated_attribute`
11
+ - `have_a_translated_error`
12
+ - `have_a_translated_model`
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Ndd RSpec Rails
1
+ # NDD RSpec Rails
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/ddidier/ndd-rspec-rails.png)](http://travis-ci.org/ddidier/ndd-rspec-rails)
4
4
  [![Dependency Status](https://gemnasium.com/ddidier/ndd-rspec-rails.png)](https://gemnasium.com/ddidier/ndd-rspec-rails)
data/bin/yard-server ADDED
@@ -0,0 +1,9 @@
1
+ #!/bin/bash
2
+
3
+ APPLICATION_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
4
+
5
+ function _yard_server() {
6
+ yard server --reload
7
+ }
8
+
9
+ _yard_server $@
@@ -0,0 +1,100 @@
1
+ require 'ndd/rspec/rails/matchers/translation_matcher'
2
+
3
+ module Ndd
4
+ module RSpec
5
+ module Rails
6
+ module Matchers
7
+ module Controller
8
+
9
+ # Implements {#have_a_translated_flash}.
10
+ class HaveATranslatedFlash < TranslationMatcher
11
+
12
+ # @param message [String|Symbol] the message to test.
13
+ def initialize(message)
14
+ super()
15
+ @message = message
16
+ end
17
+
18
+ # Set the action of the message to test.
19
+ # @param action [String|Symbol] the action associated to the message to test.
20
+ # @return self
21
+ def on_action(action)
22
+ @action = action
23
+ self
24
+ end
25
+
26
+ # @param controller [Object] the controller being tested.
27
+ # @return [Boolean] true if the message has an associated translation, false otherwise.
28
+ def matches?(controller)
29
+ @controller = controller
30
+ @failed_locales = []
31
+ @tested_locales.each do |tested_locale|
32
+ @failed_locales << tested_locale unless translated_in?(tested_locale)
33
+ end
34
+ @failed_locales.empty?
35
+ end
36
+
37
+ # @return [String] a description of this matcher.
38
+ def description
39
+ description = "have a translated flash message for '#{@message}'"
40
+ description << " on '#{@action}'" if @action.present?
41
+ description << " in #{locales_as_string(@tested_locales)}"
42
+ description
43
+ end
44
+
45
+ # @return [String] details about the failure of this matcher.
46
+ def failure_message
47
+ message = "expected '#{subject_as_string}' to have a translated flash message for '#{@message}'\n"
48
+ message << "but none of the following keys was found:\n"
49
+ message << "#{translation_keys.map { |l| " - #{l}" }.join("\n")}\n"
50
+ message << "for the locales: #{locales_as_string(@failed_locales)}"
51
+ message
52
+ end
53
+
54
+ # -------------------- private
55
+ private
56
+
57
+ # @return [String] a human readable string of the subject of the error.
58
+ def subject_as_string
59
+ @action.present? ? "#{@controller.class}##{@action}" : @controller.class.to_s
60
+ end
61
+
62
+ def translation_keys
63
+ @action.present? ? translation_keys_with_action : translation_keys_without_action
64
+ end
65
+
66
+ def translation_keys_with_action
67
+ controller_key = @controller.class.name.underscore
68
+ message_key = @message.to_s
69
+ action_key = @action.to_s
70
+ %W[
71
+ actioncontroller.#{controller_key}.#{action_key}.flash.#{message_key}
72
+ actioncontroller.#{controller_key}.flash.#{message_key}
73
+ actioncontroller.#{action_key}.flash.#{message_key}
74
+ actioncontroller.flash.#{message_key}
75
+ ]
76
+ end
77
+
78
+ def translation_keys_without_action
79
+ controller_key = @controller.class.name.underscore
80
+ message_key = @message.to_s
81
+ %W[
82
+ actioncontroller.#{controller_key}.flash.#{message_key}
83
+ actioncontroller.flash.#{message_key}
84
+ ]
85
+ end
86
+
87
+ def translated_in?(tested_locale)
88
+ translation_keys.each do |translation_key|
89
+ return true if translated?(tested_locale, translation_key)
90
+ end
91
+ false
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,52 @@
1
+ require_relative 'controller/have_a_translated_flash'
2
+
3
+ module Ndd
4
+ module RSpec
5
+ module Rails
6
+ module Matchers
7
+ # RSpec matchers for Rails controllers.
8
+ module Controller
9
+
10
+ # Ensure that a controller has a translated flash message for the given key.
11
+ #
12
+ # More precisely, ensure that (with all parameters being snake case) one of
13
+ # I18n.t(locale, "actioncontroller.#{controller_key}.#{action_key}.flash.#{message_key}")
14
+ # I18n.t(locale, "actioncontroller.#{controller_key}.flash.#{message_key}")
15
+ # I18n.t(locale, "actioncontroller.#{action_key}.flash.#{message_key}")
16
+ # I18n.t(locale, "actioncontroller.flash.#{message_key}")
17
+ # returns a value for the default locale (i.e. +I18n.default_locale+)
18
+ # or all the available locales (i.e. +I18n.available_locales+).
19
+ #
20
+ # @example
21
+ #
22
+ # RSpec.describe MyController, type: :controller do
23
+ #
24
+ # # both are equivalent
25
+ # it { is_expected.to have_a_translated_flash(:out_of_range) }
26
+ # it { is_expected.to have_a_translated_flash(:out_of_range).in_available_locales }
27
+ #
28
+ # it { is_expected.to have_a_translated_flash(:out_of_range).in_default_locale }
29
+ #
30
+ # # both are equivalent
31
+ # it { is_expected.to have_a_translated_flash(:out_of_range).on_action(:index) }
32
+ # it { is_expected.to have_a_translated_flash(:out_of_range).on_action(:index).in_available_locales }
33
+ #
34
+ # it { is_expected.to have_a_translated_flash(:out_of_range).on_action(:index).in_default_locale }
35
+ # end
36
+ #
37
+ # @param message [String|Symbol] the message to test.
38
+ # @return [Ndd::RSpec::Rails::Matchers::Controller::HaveATranslatedFlash]
39
+ #
40
+ def have_a_translated_flash(message) # rubocop:disable Style/PredicateName
41
+ HaveATranslatedFlash.new(message)
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ RSpec.configure do |config|
51
+ config.include Ndd::RSpec::Rails::Matchers::Controller, type: :controller
52
+ end
@@ -1,37 +1,12 @@
1
- require 'active_support/core_ext/string/inflections'
2
- require 'i18n'
3
- require 'ndd/rspec/rails/matchers/model/translation_matcher'
1
+ require 'ndd/rspec/rails/matchers/translation_matcher'
4
2
 
5
3
  module Ndd
6
4
  module RSpec
7
5
  module Rails
8
6
  module Matchers
9
- module Model #:nodoc:
7
+ module Model
10
8
 
11
- # Ensure that an attribute has an associated translation.
12
- #
13
- # More precisely, ensure that
14
- # I18n.t(locale, "activerecord.attributes.{snake_case_class_name}.{snake_case_attribute_name}")
15
- # returns a value for the default locale (i.e. +I18n.default_locale+)
16
- # or all the available locales (i.e. +I18n.available_locales+).
17
- #
18
- # For example:
19
- #
20
- # RSpec.describe MyModel, type: :model do
21
- # # both are equivalent
22
- # it { is_expected.to have_a_translated_attribute(:comment) }
23
- # it { is_expected.to have_a_translated_attribute(:comment).in_available_locales }
24
- #
25
- # it { is_expected.to have_a_translated_attribute(:comment).in_default_locale }
26
- # end
27
- #
28
- def have_a_translated_attribute(attribute) # rubocop:disable Style/PredicateName
29
- HaveATranslatedAttribute.new(attribute)
30
- end
31
-
32
- # ------------------------------------------------------------------------------------------------------------
33
9
  # Implements {#have_a_translated_attribute}.
34
- #
35
10
  class HaveATranslatedAttribute < TranslationMatcher
36
11
 
37
12
  # @param attribute [String|Symbol] the attribute to test.
@@ -1,47 +1,12 @@
1
- require 'active_support/core_ext/string/inflections'
2
- require 'i18n'
3
- require 'ndd/rspec/rails/matchers/model/translation_matcher'
1
+ require 'ndd/rspec/rails/matchers/translation_matcher'
4
2
 
5
3
  module Ndd
6
4
  module RSpec
7
5
  module Rails
8
6
  module Matchers
9
- module Model #:nodoc:
7
+ module Model
10
8
 
11
- # Ensure that an error on a model or an attribute has an associated translation.
12
- #
13
- # More precisely, ensure that (with all parameters being snake case) one of
14
- # I18n.t(locale, "activerecord.errors.models.{class_name}.attributes.{attribute_name}.{error_key}")
15
- # I18n.t(locale, "activerecord.errors.models.{class_name}.{error_key}")
16
- # I18n.t(locale, "activerecord.errors.messages.{error_key}")
17
- # I18n.t(locale, "errors.attributes.{attribute_name}.{error_key}")
18
- # I18n.t(locale, "errors.messages.{error_key}")
19
- # returns a value for the default locale (i.e. +I18n.default_locale+)
20
- # or all the available locales (i.e. +I18n.available_locales+).
21
- #
22
- # For example:
23
- #
24
- # RSpec.describe MyModel, type: :model do
25
- # # both are equivalent
26
- # it { is_expected.to have_a_translated_error(:duplicate) }
27
- # it { is_expected.to have_a_translated_error(:duplicate).in_available_locales }
28
- #
29
- # it { is_expected.to have_a_translated_error(:duplicate).in_default_locale }
30
- #
31
- # # both are equivalent
32
- # it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments) }
33
- # it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_available_locales }
34
- #
35
- # it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_default_locale }
36
- # end
37
- #
38
- def have_a_translated_error(message) # rubocop:disable Style/PredicateName
39
- HaveATranslatedError.new(message)
40
- end
41
-
42
- # ------------------------------------------------------------------------------------------------------------
43
9
  # Implements {#have_a_translated_error}.
44
- #
45
10
  class HaveATranslatedError < TranslationMatcher
46
11
 
47
12
  # @param error [String|Symbol] the error to test.
@@ -51,6 +16,7 @@ module Ndd
51
16
  end
52
17
 
53
18
  # Set the attribute of the error to test.
19
+ # @param attribute [String|Symbol] the attribute associated to the error to test.
54
20
  # @return self
55
21
  def on_attribute(attribute)
56
22
  @attribute = attribute
@@ -99,24 +65,24 @@ module Ndd
99
65
 
100
66
  def translation_keys_with_attribute
101
67
  model_key = @model.class.name.underscore
102
- message_key = @error.to_s
68
+ error_key = @error.to_s
103
69
  attribute_key = @attribute.to_s
104
70
  %W[
105
- activerecord.errors.models.#{model_key}.attributes.#{attribute_key}.#{message_key}
106
- activerecord.errors.models.#{model_key}.#{message_key}
107
- activerecord.errors.messages.#{message_key}
108
- errors.attributes.#{attribute_key}.#{message_key}
109
- errors.messages.#{message_key}
71
+ activerecord.errors.models.#{model_key}.attributes.#{attribute_key}.#{error_key}
72
+ activerecord.errors.models.#{model_key}.#{error_key}
73
+ activerecord.errors.messages.#{error_key}
74
+ errors.attributes.#{attribute_key}.#{error_key}
75
+ errors.messages.#{error_key}
110
76
  ]
111
77
  end
112
78
 
113
79
  def translation_keys_without_attribute
114
80
  model_key = @model.class.name.underscore
115
- message_key = @error.to_s
81
+ error_key = @error.to_s
116
82
  %W[
117
- activerecord.errors.models.#{model_key}.#{message_key}
118
- activerecord.errors.messages.#{message_key}
119
- errors.messages.#{message_key}
83
+ activerecord.errors.models.#{model_key}.#{error_key}
84
+ activerecord.errors.messages.#{error_key}
85
+ errors.messages.#{error_key}
120
86
  ]
121
87
  end
122
88
 
@@ -1,37 +1,12 @@
1
- require 'active_support/core_ext/string/inflections'
2
- require 'i18n'
3
- require 'ndd/rspec/rails/matchers/model/translation_matcher'
1
+ require 'ndd/rspec/rails/matchers/translation_matcher'
4
2
 
5
3
  module Ndd
6
4
  module RSpec
7
5
  module Rails
8
6
  module Matchers
9
- module Model #:nodoc:
7
+ module Model
10
8
 
11
- # Ensure that a model has an associated translation.
12
- #
13
- # More precisely, ensure that
14
- # I18n.t(locale, "activerecord.models.{snake_case_class_name}")
15
- # returns a value for the default locale (i.e. +I18n.default_locale+)
16
- # or all the available locales (i.e. +I18n.available_locales+).
17
- #
18
- # For example:
19
- #
20
- # RSpec.describe MyModel, type: :model do
21
- # # both are equivalent
22
- # it { is_expected.to have_a_translated_model }
23
- # it { is_expected.to have_a_translated_model.in_available_locales }
24
- #
25
- # it { is_expected.to have_a_translated_model.in_default_locale }
26
- # end
27
- #
28
- def have_a_translated_model # rubocop:disable Style/PredicateName
29
- HaveATranslatedModel.new
30
- end
31
-
32
- # ------------------------------------------------------------------------------------------------------------
33
9
  # Implements {#have_a_translated_model}.
34
- #
35
10
  class HaveATranslatedModel < TranslationMatcher
36
11
 
37
12
  # @param model [Object] the model being tested.
@@ -2,6 +2,100 @@ require_relative 'model/have_a_translated_attribute'
2
2
  require_relative 'model/have_a_translated_error'
3
3
  require_relative 'model/have_a_translated_model'
4
4
 
5
+ module Ndd
6
+ module RSpec
7
+ module Rails
8
+ module Matchers
9
+ # RSpec matchers for Rails models.
10
+ module Model
11
+
12
+ # Ensure that an attribute has an associated translation.
13
+ #
14
+ # More precisely, ensure that
15
+ # I18n.t(locale, "activerecord.attributes.{snake_case_class_name}.{snake_case_attribute_name}")
16
+ # returns a value for the default locale (i.e. +I18n.default_locale+)
17
+ # or all the available locales (i.e. +I18n.available_locales+).
18
+ #
19
+ # @example
20
+ #
21
+ # RSpec.describe MyModel, type: :model do
22
+ # # both are equivalent
23
+ # it { is_expected.to have_a_translated_attribute(:comment) }
24
+ # it { is_expected.to have_a_translated_attribute(:comment).in_available_locales }
25
+ #
26
+ # it { is_expected.to have_a_translated_attribute(:comment).in_default_locale }
27
+ # end
28
+ #
29
+ # @param attribute [String|Symbol] the attribute to test.
30
+ # @return [Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedAttribute]
31
+ #
32
+ def have_a_translated_attribute(attribute) # rubocop:disable Style/PredicateName
33
+ HaveATranslatedAttribute.new(attribute)
34
+ end
35
+
36
+ # Ensure that an error on a model or an attribute has an associated translation.
37
+ #
38
+ # More precisely, ensure that (with all parameters being snake case) one of
39
+ # I18n.t(locale, "activerecord.errors.models.{class_name}.attributes.{attribute_name}.{error_key}")
40
+ # I18n.t(locale, "activerecord.errors.models.{class_name}.{error_key}")
41
+ # I18n.t(locale, "activerecord.errors.messages.{error_key}")
42
+ # I18n.t(locale, "errors.attributes.{attribute_name}.{error_key}")
43
+ # I18n.t(locale, "errors.messages.{error_key}")
44
+ # returns a value for the default locale (i.e. +I18n.default_locale+)
45
+ # or all the available locales (i.e. +I18n.available_locales+).
46
+ #
47
+ # @example
48
+ #
49
+ # RSpec.describe MyModel, type: :model do
50
+ # # both are equivalent
51
+ # it { is_expected.to have_a_translated_error(:duplicate) }
52
+ # it { is_expected.to have_a_translated_error(:duplicate).in_available_locales }
53
+ #
54
+ # it { is_expected.to have_a_translated_error(:duplicate).in_default_locale }
55
+ #
56
+ # # both are equivalent
57
+ # it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments) }
58
+ # it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_available_locales }
59
+ #
60
+ # it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_default_locale }
61
+ # end
62
+ #
63
+ # @param error [String|Symbol] the error to test.
64
+ # @return [Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedError]
65
+ #
66
+ def have_a_translated_error(error) # rubocop:disable Style/PredicateName
67
+ HaveATranslatedError.new(error)
68
+ end
69
+
70
+ # Ensure that a model has an associated translation.
71
+ #
72
+ # More precisely, ensure that
73
+ # I18n.t(locale, "activerecord.models.{snake_case_class_name}")
74
+ # returns a value for the default locale (i.e. +I18n.default_locale+)
75
+ # or all the available locales (i.e. +I18n.available_locales+).
76
+ #
77
+ # @example
78
+ #
79
+ # RSpec.describe MyModel, type: :model do
80
+ # # both are equivalent
81
+ # it { is_expected.to have_a_translated_model }
82
+ # it { is_expected.to have_a_translated_model.in_available_locales }
83
+ #
84
+ # it { is_expected.to have_a_translated_model.in_default_locale }
85
+ # end
86
+ #
87
+ # @return [Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedModel]
88
+ #
89
+ def have_a_translated_model # rubocop:disable Style/PredicateName
90
+ HaveATranslatedModel.new
91
+ end
92
+
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+
5
99
  RSpec.configure do |config|
6
100
  config.include Ndd::RSpec::Rails::Matchers::Model, type: :model
7
101
  end
@@ -0,0 +1,57 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'i18n'
3
+
4
+ module Ndd
5
+ module RSpec
6
+ module Rails
7
+ module Matchers
8
+
9
+ # Base class for matchers dealing with translation.
10
+ class TranslationMatcher
11
+
12
+ # Set the locales to test to +I18n.available_locales+.
13
+ def initialize
14
+ @tested_locales = I18n.available_locales
15
+ end
16
+
17
+ # Set the locales to test to all the available locales (i.e. +I18n.available_locales+).
18
+ # @return self
19
+ def in_available_locales
20
+ @tested_locales = I18n.available_locales
21
+ self
22
+ end
23
+
24
+ # Set the locales to test to the default locale (i.e. +I18n.default_locale+) only.
25
+ # @return self
26
+ def in_default_locale
27
+ @tested_locales = [I18n.default_locale]
28
+ self
29
+ end
30
+
31
+ # -------------------------------------------------------------------------------------------- private -----
32
+ private
33
+
34
+ # Convert an array of locales to a human readable list, i.e. ':en, :fr, :jp'.
35
+ # @param locales [Array<String|Symbol>] the locales to convert.
36
+ # @return [String] the converted locales.
37
+ def locales_as_string(locales)
38
+ locales.map { |locale| ":#{locale}" }.join(', ')
39
+ end
40
+
41
+ # Check that a translation exists for the given key in the given locale.
42
+ # @param locale [Symbol] the locale of the translation to lookup.
43
+ # @param key [String] the key of the translation to lookup.
44
+ # @return [Boolean] true if a translation exists for the given key in the given locale, false otherwise.
45
+ def translated?(locale, key)
46
+ I18n.with_locale(locale) { I18n.t(key, raise: true) }
47
+ return true
48
+ rescue I18n::MissingTranslationData
49
+ return false
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1 +1,14 @@
1
+ require_relative 'matchers/controller'
1
2
  require_relative 'matchers/model'
3
+
4
+ module Ndd
5
+ module RSpec
6
+ module Rails
7
+
8
+ # RSpec matchers for Rails.
9
+ module Matchers
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -1,7 +1,7 @@
1
1
  module Ndd
2
2
  module RSpec
3
3
  module Rails
4
- VERSION = '0.1.0'.freeze
4
+ VERSION = '0.2.0'.freeze
5
5
  end
6
6
  end
7
7
  end
@@ -1,2 +1,12 @@
1
1
  require 'ndd/rspec/rails/version'
2
2
  require 'ndd/rspec/rails/matchers'
3
+
4
+ module Ndd #:nodoc:
5
+ module RSpec #:nodoc:
6
+
7
+ # RSpec extensions for Rails.
8
+ module Rails
9
+ end
10
+
11
+ end
12
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ndd-rspec-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David DIDIER
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-11 00:00:00.000000000 Z
11
+ date: 2017-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -205,13 +205,16 @@ files:
205
205
  - bin/console
206
206
  - bin/rubocop
207
207
  - bin/setup
208
+ - bin/yard-server
208
209
  - lib/ndd/rspec/rails.rb
209
210
  - lib/ndd/rspec/rails/matchers.rb
211
+ - lib/ndd/rspec/rails/matchers/controller.rb
212
+ - lib/ndd/rspec/rails/matchers/controller/have_a_translated_flash.rb
210
213
  - lib/ndd/rspec/rails/matchers/model.rb
211
214
  - lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb
212
215
  - lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb
213
216
  - lib/ndd/rspec/rails/matchers/model/have_a_translated_model.rb
214
- - lib/ndd/rspec/rails/matchers/model/translation_matcher.rb
217
+ - lib/ndd/rspec/rails/matchers/translation_matcher.rb
215
218
  - lib/ndd/rspec/rails/version.rb
216
219
  homepage: http://github.com/ddidier/ndd-rspec-rails
217
220
  licenses:
@@ -1,59 +0,0 @@
1
- require 'active_support/core_ext/string/inflections'
2
- require 'i18n'
3
-
4
- module Ndd
5
- module RSpec
6
- module Rails
7
- module Matchers
8
- module Model #:nodoc:
9
-
10
- # Base class for matchers dealing with translation.
11
- class TranslationMatcher
12
-
13
- # Set the locales to test to +I18n.available_locales+.
14
- def initialize
15
- @tested_locales = I18n.available_locales
16
- end
17
-
18
- # Set the locales to test to all the available locales (i.e. +I18n.available_locales+).
19
- # @return self
20
- def in_available_locales
21
- @tested_locales = I18n.available_locales
22
- self
23
- end
24
-
25
- # Set the locales to test to the default locale (i.e. +I18n.default_locale+) only.
26
- # @return self
27
- def in_default_locale
28
- @tested_locales = [I18n.default_locale]
29
- self
30
- end
31
-
32
- # -------------------------------------------------------------------------------------------- private -----
33
- private
34
-
35
- # Convert an array of locales to a human readable list, i.e. ':en, :fr, :jp'.
36
- # @param locales [Array<String|Symbol>] the locales to convert.
37
- # @return [String] the converted locales.
38
- def locales_as_string(locales)
39
- locales.map { |locale| ":#{locale}" }.join(', ')
40
- end
41
-
42
- # Check that a translation exists for the given key in the given locale.
43
- # @param locale [Symbol] the locale of the translation to lookup.
44
- # @param key [String] the key of the translation to lookup.
45
- # @return [Boolean] true if a translation exists for the given key in the given locale, false otherwise.
46
- def translated?(locale, key)
47
- I18n.with_locale(locale) { I18n.t(key, raise: true) }
48
- return true
49
- rescue I18n::MissingTranslationData
50
- return false
51
- end
52
-
53
- end
54
-
55
- end
56
- end
57
- end
58
- end
59
- end