ndd-rspec-rails 0.1.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/LICENSE +21 -0
- data/README.md +68 -0
- data/bin/console +14 -0
- data/bin/rubocop +22 -0
- data/bin/setup +8 -0
- data/lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb +83 -0
- data/lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb +136 -0
- data/lib/ndd/rspec/rails/matchers/model/have_a_translated_model.rb +76 -0
- data/lib/ndd/rspec/rails/matchers/model/translation_matcher.rb +59 -0
- data/lib/ndd/rspec/rails/matchers/model.rb +7 -0
- data/lib/ndd/rspec/rails/matchers.rb +1 -0
- data/lib/ndd/rspec/rails/version.rb +7 -0
- data/lib/ndd/rspec/rails.rb +2 -0
- metadata +240 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3fdeed587f4e9c6e3f07ee32e556e327d98a45ba
|
4
|
+
data.tar.gz: 11a74da50a412f8a3fe174af4c95b72431a2beaa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e00104c957bb678794811759e82beea6c4954edd08348a5f8b1e6b65c47e1fbaa7a64a31d57fbb618debac176a33d1e6bd783f1295f9eb577263d468722200f8
|
7
|
+
data.tar.gz: 39656673e2e03d69bb350582cfee512482116a66f924edd112a1818577f7686d128350acaa34ce7e8fad8fc0ff917b4edfcb188c07664eaa2faa233a229c8104
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at c_inconnu2@yahoo.fr. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 David DIDIER
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Ndd RSpec Rails
|
2
|
+
|
3
|
+
[](http://travis-ci.org/ddidier/ndd-rspec-rails)
|
4
|
+
[](https://gemnasium.com/ddidier/ndd-rspec-rails)
|
5
|
+
[](https://codeclimate.com/github/ddidier/ndd-rspec-rails)
|
6
|
+
[](https://codeclimate.com/github/ddidier/ndd-rspec-rails/coverage)
|
7
|
+
[](http://www.rubydoc.info/github/ddidier/ndd-rspec-rails)
|
8
|
+
[](https://inch-ci.org/github/ddidier/ndd-rspec-rails)
|
9
|
+
|
10
|
+
RSpec utilities for Rails.
|
11
|
+
|
12
|
+
The API documentation can be find at [RubyDoc](http://www.rubydoc.info/github/ddidier/ndd-rspec-rails).
|
13
|
+
|
14
|
+
## Prerequisites
|
15
|
+
|
16
|
+
This gem requires:
|
17
|
+
|
18
|
+
- `activesupport >= 4.0`
|
19
|
+
- `rspec >= 3.0`
|
20
|
+
|
21
|
+
And is tested with:
|
22
|
+
|
23
|
+
- `Ruby 2.4`
|
24
|
+
- `Ruby 2.3`
|
25
|
+
- `Ruby 2.2`
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Add this line to your application's Gemfile:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gem 'ndd-rspec-rails'
|
33
|
+
```
|
34
|
+
|
35
|
+
And then execute `bundle`
|
36
|
+
|
37
|
+
Or install it yourself with `gem install ndd-rspec-rails`
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
### Model matchers
|
42
|
+
|
43
|
+
See the [Ndd::RSpec::Rails::Matchers::Model documentation](http://www.rubydoc.info/github/ddidier/ndd-rspec-rails/Ndd/RSpec/Rails/Matchers/Model)
|
44
|
+
for more details.
|
45
|
+
|
46
|
+
- `have_a_translated_attribute`: ensure that a model has an associated translation;
|
47
|
+
- `have_a_translated_error`: ensure that an error on a model or an attribute has an associated translation.;
|
48
|
+
- `have_a_translated_model`: ensure that an attribute has an associated translation;
|
49
|
+
|
50
|
+
## Development
|
51
|
+
|
52
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
53
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
54
|
+
|
55
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
|
56
|
+
version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
|
57
|
+
push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ddidier/ndd-rspec-rails. This project is
|
62
|
+
intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the
|
63
|
+
[Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
68
|
+
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'ndd/rspec/rails'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rubocop
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
APPLICATION_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
|
4
|
+
|
5
|
+
function _rubocop() {
|
6
|
+
local txt_orange='\e[38;5;214m'
|
7
|
+
local txt_reset='\e[0m'
|
8
|
+
|
9
|
+
pushd $APPLICATION_DIR > /dev/null \
|
10
|
+
&& \
|
11
|
+
rubocop \
|
12
|
+
-D -R \
|
13
|
+
--format html -o tmp/rubocop.html \
|
14
|
+
--format progress \
|
15
|
+
--format offenses \
|
16
|
+
; \
|
17
|
+
echo -e "${txt_orange}HTML report was generated at:\n file://$APPLICATION_DIR/tmp/rubocop.html${txt_reset}" \
|
18
|
+
; \
|
19
|
+
popd > /dev/null
|
20
|
+
}
|
21
|
+
|
22
|
+
_rubocop $@
|
data/bin/setup
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
require 'i18n'
|
3
|
+
require 'ndd/rspec/rails/matchers/model/translation_matcher'
|
4
|
+
|
5
|
+
module Ndd
|
6
|
+
module RSpec
|
7
|
+
module Rails
|
8
|
+
module Matchers
|
9
|
+
module Model #:nodoc:
|
10
|
+
|
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
|
+
# Implements {#have_a_translated_attribute}.
|
34
|
+
#
|
35
|
+
class HaveATranslatedAttribute < TranslationMatcher
|
36
|
+
|
37
|
+
# @param attribute [String|Symbol] the attribute to test.
|
38
|
+
def initialize(attribute)
|
39
|
+
super()
|
40
|
+
@attribute = attribute
|
41
|
+
end
|
42
|
+
|
43
|
+
# @param model [Object] the model being tested.
|
44
|
+
# @return [Boolean] true if the attribute has an associated translation, false otherwise.
|
45
|
+
def matches?(model)
|
46
|
+
@model = model
|
47
|
+
@failed_locales = []
|
48
|
+
@tested_locales.each do |tested_locale|
|
49
|
+
@failed_locales << tested_locale unless translated?(tested_locale, translation_key)
|
50
|
+
end
|
51
|
+
@failed_locales.empty?
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [String] a description of this matcher.
|
55
|
+
def description
|
56
|
+
"have a translated attribute name for '#{@attribute}' in #{locales_as_string(@tested_locales)}"
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [String] details about the failure of this matcher.
|
60
|
+
def failure_message
|
61
|
+
message = ''
|
62
|
+
message << "expected '#{@model.class}' to have a translated attribute name for '#{@attribute}'\n"
|
63
|
+
message << "but the '#{translation_key}' key was not found\n"
|
64
|
+
message << "for the locales: #{locales_as_string(@failed_locales)}"
|
65
|
+
message
|
66
|
+
end
|
67
|
+
|
68
|
+
# -------------------------------------------------------------------------------------------- private -----
|
69
|
+
private
|
70
|
+
|
71
|
+
# @return [String] the translation key of the attribute.
|
72
|
+
def translation_key
|
73
|
+
model_key = @model.class.name.underscore
|
74
|
+
attribute_key = @attribute.to_s
|
75
|
+
"activerecord.attributes.#{model_key}.#{attribute_key}"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
require 'i18n'
|
3
|
+
require 'ndd/rspec/rails/matchers/model/translation_matcher'
|
4
|
+
|
5
|
+
module Ndd
|
6
|
+
module RSpec
|
7
|
+
module Rails
|
8
|
+
module Matchers
|
9
|
+
module Model #:nodoc:
|
10
|
+
|
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
|
+
# Implements {#have_a_translated_error}.
|
44
|
+
#
|
45
|
+
class HaveATranslatedError < TranslationMatcher
|
46
|
+
|
47
|
+
# @param error [String|Symbol] the error to test.
|
48
|
+
def initialize(error)
|
49
|
+
super()
|
50
|
+
@error = error
|
51
|
+
end
|
52
|
+
|
53
|
+
# Set the attribute of the error to test.
|
54
|
+
# @return self
|
55
|
+
def on_attribute(attribute)
|
56
|
+
@attribute = attribute
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param model [Object] the model being tested.
|
61
|
+
# @return [Boolean] true if the error has an associated translation, false otherwise.
|
62
|
+
def matches?(model)
|
63
|
+
@model = model
|
64
|
+
@failed_locales = []
|
65
|
+
@tested_locales.each do |tested_locale|
|
66
|
+
@failed_locales << tested_locale unless translated_in?(tested_locale)
|
67
|
+
end
|
68
|
+
@failed_locales.empty?
|
69
|
+
end
|
70
|
+
|
71
|
+
# @return [String] a description of this matcher.
|
72
|
+
def description
|
73
|
+
description = "have a translated error message for '#{@error}'"
|
74
|
+
description << " on '#{@attribute}'" if @attribute.present?
|
75
|
+
description << " in #{locales_as_string(@tested_locales)}"
|
76
|
+
description
|
77
|
+
end
|
78
|
+
|
79
|
+
# @return [String] details about the failure of this matcher.
|
80
|
+
def failure_message
|
81
|
+
message = "expected '#{subject_as_string}' to have a translated error message for '#{@error}'\n"
|
82
|
+
message << "but none of the following keys was found:\n"
|
83
|
+
message << "#{translation_keys.map { |l| " - #{l}" }.join("\n")}\n"
|
84
|
+
message << "for the locales: #{locales_as_string(@failed_locales)}"
|
85
|
+
message
|
86
|
+
end
|
87
|
+
|
88
|
+
# -------------------- private
|
89
|
+
private
|
90
|
+
|
91
|
+
# @return [String] a human readable string of the subject of the error, being a class or an attribute.
|
92
|
+
def subject_as_string
|
93
|
+
@attribute.present? ? "#{@model.class}##{@attribute}" : @model.class.to_s
|
94
|
+
end
|
95
|
+
|
96
|
+
def translation_keys
|
97
|
+
@attribute.present? ? translation_keys_with_attribute : translation_keys_without_attribute
|
98
|
+
end
|
99
|
+
|
100
|
+
def translation_keys_with_attribute
|
101
|
+
model_key = @model.class.name.underscore
|
102
|
+
message_key = @error.to_s
|
103
|
+
attribute_key = @attribute.to_s
|
104
|
+
%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}
|
110
|
+
]
|
111
|
+
end
|
112
|
+
|
113
|
+
def translation_keys_without_attribute
|
114
|
+
model_key = @model.class.name.underscore
|
115
|
+
message_key = @error.to_s
|
116
|
+
%W[
|
117
|
+
activerecord.errors.models.#{model_key}.#{message_key}
|
118
|
+
activerecord.errors.messages.#{message_key}
|
119
|
+
errors.messages.#{message_key}
|
120
|
+
]
|
121
|
+
end
|
122
|
+
|
123
|
+
def translated_in?(tested_locale)
|
124
|
+
translation_keys.each do |translation_key|
|
125
|
+
return true if translated?(tested_locale, translation_key)
|
126
|
+
end
|
127
|
+
false
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
require 'i18n'
|
3
|
+
require 'ndd/rspec/rails/matchers/model/translation_matcher'
|
4
|
+
|
5
|
+
module Ndd
|
6
|
+
module RSpec
|
7
|
+
module Rails
|
8
|
+
module Matchers
|
9
|
+
module Model #:nodoc:
|
10
|
+
|
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
|
+
# Implements {#have_a_translated_model}.
|
34
|
+
#
|
35
|
+
class HaveATranslatedModel < TranslationMatcher
|
36
|
+
|
37
|
+
# @param model [Object] the model being tested.
|
38
|
+
# @return [Boolean] true if the model has an associated translation, false otherwise.
|
39
|
+
def matches?(model)
|
40
|
+
@model = model
|
41
|
+
@failed_locales = []
|
42
|
+
@tested_locales.each do |tested_locale|
|
43
|
+
@failed_locales << tested_locale unless translated?(tested_locale, translation_key)
|
44
|
+
end
|
45
|
+
@failed_locales.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
# @return [String] a description of this matcher.
|
49
|
+
def description
|
50
|
+
"have a translated model name in #{locales_as_string(@tested_locales)}"
|
51
|
+
end
|
52
|
+
|
53
|
+
# @return [String] details about the failure of this matcher.
|
54
|
+
def failure_message
|
55
|
+
message = ''
|
56
|
+
message << "expected '#{@model.class}' to have a translated model name\n"
|
57
|
+
message << "but the '#{translation_key}' key was not found\n"
|
58
|
+
message << "for the locales: #{locales_as_string(@failed_locales)}"
|
59
|
+
message
|
60
|
+
end
|
61
|
+
|
62
|
+
# -------------------------------------------------------------------------------------------- private -----
|
63
|
+
private
|
64
|
+
|
65
|
+
# @return [String] the translation key of the model.
|
66
|
+
def translation_key
|
67
|
+
"activerecord.models.#{@model.class.name.underscore}"
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,59 @@
|
|
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
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'matchers/model'
|
metadata
ADDED
@@ -0,0 +1,240 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ndd-rspec-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David DIDIER
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rspec
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3.0'
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '4.0'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.0'
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '4.0'
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: appraisal
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 2.2.0
|
60
|
+
type: :development
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 2.2.0
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: bundler
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 1.14.0
|
74
|
+
type: :development
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 1.14.0
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: codeclimate-test-reporter
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 1.0.0
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 1.0.0
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: guard
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - "~>"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 2.14.0
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - "~>"
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: 2.14.0
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
name: guard-bundler
|
111
|
+
requirement: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - "~>"
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: 2.1.0
|
116
|
+
type: :development
|
117
|
+
prerelease: false
|
118
|
+
version_requirements: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 2.1.0
|
123
|
+
- !ruby/object:Gem::Dependency
|
124
|
+
name: guard-rspec
|
125
|
+
requirement: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - "~>"
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: 4.7.0
|
130
|
+
type: :development
|
131
|
+
prerelease: false
|
132
|
+
version_requirements: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - "~>"
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: 4.7.0
|
137
|
+
- !ruby/object:Gem::Dependency
|
138
|
+
name: rake
|
139
|
+
requirement: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - "~>"
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: 12.0.0
|
144
|
+
type: :development
|
145
|
+
prerelease: false
|
146
|
+
version_requirements: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - "~>"
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: 12.0.0
|
151
|
+
- !ruby/object:Gem::Dependency
|
152
|
+
name: rubocop
|
153
|
+
requirement: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - "~>"
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: 0.48.0
|
158
|
+
type: :development
|
159
|
+
prerelease: false
|
160
|
+
version_requirements: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - "~>"
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: 0.48.0
|
165
|
+
- !ruby/object:Gem::Dependency
|
166
|
+
name: simplecov
|
167
|
+
requirement: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - "~>"
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: 0.14.0
|
172
|
+
type: :development
|
173
|
+
prerelease: false
|
174
|
+
version_requirements: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - "~>"
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: 0.14.0
|
179
|
+
- !ruby/object:Gem::Dependency
|
180
|
+
name: yard
|
181
|
+
requirement: !ruby/object:Gem::Requirement
|
182
|
+
requirements:
|
183
|
+
- - "~>"
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
version: 0.9.0
|
186
|
+
type: :development
|
187
|
+
prerelease: false
|
188
|
+
version_requirements: !ruby/object:Gem::Requirement
|
189
|
+
requirements:
|
190
|
+
- - "~>"
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: 0.9.0
|
193
|
+
description: RSpec utilities for Rails
|
194
|
+
email:
|
195
|
+
- c_inconnu2@yahoo.fr
|
196
|
+
executables: []
|
197
|
+
extensions: []
|
198
|
+
extra_rdoc_files:
|
199
|
+
- README.md
|
200
|
+
files:
|
201
|
+
- CHANGELOG.md
|
202
|
+
- CODE_OF_CONDUCT.md
|
203
|
+
- LICENSE
|
204
|
+
- README.md
|
205
|
+
- bin/console
|
206
|
+
- bin/rubocop
|
207
|
+
- bin/setup
|
208
|
+
- lib/ndd/rspec/rails.rb
|
209
|
+
- lib/ndd/rspec/rails/matchers.rb
|
210
|
+
- lib/ndd/rspec/rails/matchers/model.rb
|
211
|
+
- lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb
|
212
|
+
- lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb
|
213
|
+
- lib/ndd/rspec/rails/matchers/model/have_a_translated_model.rb
|
214
|
+
- lib/ndd/rspec/rails/matchers/model/translation_matcher.rb
|
215
|
+
- lib/ndd/rspec/rails/version.rb
|
216
|
+
homepage: http://github.com/ddidier/ndd-rspec-rails
|
217
|
+
licenses:
|
218
|
+
- MIT
|
219
|
+
metadata: {}
|
220
|
+
post_install_message:
|
221
|
+
rdoc_options: []
|
222
|
+
require_paths:
|
223
|
+
- lib
|
224
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
225
|
+
requirements:
|
226
|
+
- - ">="
|
227
|
+
- !ruby/object:Gem::Version
|
228
|
+
version: 2.2.0
|
229
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
230
|
+
requirements:
|
231
|
+
- - ">="
|
232
|
+
- !ruby/object:Gem::Version
|
233
|
+
version: '0'
|
234
|
+
requirements: []
|
235
|
+
rubyforge_project:
|
236
|
+
rubygems_version: 2.6.11
|
237
|
+
signing_key:
|
238
|
+
specification_version: 4
|
239
|
+
summary: RSpec utilities for Rails
|
240
|
+
test_files: []
|