active_record-errors-localize 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/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/.vscode/settings.json +3 -0
- data/CHANGELOG.md +0 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -0
- data/Rakefile +6 -0
- data/active_record-errors-localize.gemspec +47 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/locales_examples/ja.yml +10 -0
- data/examples/error_handler.rb +25 -0
- data/lib/active_record/errors/localize/version.rb +7 -0
- data/lib/active_record/errors/localize.rb +59 -0
- metadata +180 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d4fc6a8ef7be2772d44a886d5a07359fabc223a70e11bc9bc5ddfc6186608685
|
|
4
|
+
data.tar.gz: 7753d5531d87ebb5deb3cca14d85ffcebb118d8352a376d04d1ec1da179f5754
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d9c097a2c9b176c325cfab0219b3cf1393b5c3f54d8a0d040c7f06a193c8381b039308398dfd2eac6c764dd2e28a6a6ec14ef9416633e6dd3d7d58da2b4a2bc1
|
|
7
|
+
data.tar.gz: c2916be7f040e4b74d5db8a5f77d29a9f143f6a382db5ff0e161c3831916a0830e5fa6fa95b1c4812c2c0aaaf9f8eed77875fd72bdb9e9e4bbfc445ae15773c5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
|
File without changes
|
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 matsumotokazuya7@gmail.com. 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/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
active_record-errors-localize (0.1.0)
|
|
5
|
+
activerecord (~> 5)
|
|
6
|
+
i18n (~> 1)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
activemodel (5.2.2)
|
|
12
|
+
activesupport (= 5.2.2)
|
|
13
|
+
activerecord (5.2.2)
|
|
14
|
+
activemodel (= 5.2.2)
|
|
15
|
+
activesupport (= 5.2.2)
|
|
16
|
+
arel (>= 9.0)
|
|
17
|
+
activerecord-nulldb-adapter (0.3.9)
|
|
18
|
+
activerecord (>= 2.0.0)
|
|
19
|
+
activesupport (5.2.2)
|
|
20
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
21
|
+
i18n (>= 0.7, < 2)
|
|
22
|
+
minitest (~> 5.1)
|
|
23
|
+
tzinfo (~> 1.1)
|
|
24
|
+
arel (9.0.0)
|
|
25
|
+
byebug (10.0.2)
|
|
26
|
+
coderay (1.1.2)
|
|
27
|
+
concurrent-ruby (1.1.4)
|
|
28
|
+
diff-lcs (1.3)
|
|
29
|
+
i18n (1.5.3)
|
|
30
|
+
concurrent-ruby (~> 1.0)
|
|
31
|
+
method_source (0.9.2)
|
|
32
|
+
minitest (5.11.3)
|
|
33
|
+
pry (0.12.2)
|
|
34
|
+
coderay (~> 1.1.0)
|
|
35
|
+
method_source (~> 0.9.0)
|
|
36
|
+
pry-byebug (3.6.0)
|
|
37
|
+
byebug (~> 10.0)
|
|
38
|
+
pry (~> 0.10)
|
|
39
|
+
rake (10.5.0)
|
|
40
|
+
rspec (3.8.0)
|
|
41
|
+
rspec-core (~> 3.8.0)
|
|
42
|
+
rspec-expectations (~> 3.8.0)
|
|
43
|
+
rspec-mocks (~> 3.8.0)
|
|
44
|
+
rspec-core (3.8.0)
|
|
45
|
+
rspec-support (~> 3.8.0)
|
|
46
|
+
rspec-expectations (3.8.2)
|
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
48
|
+
rspec-support (~> 3.8.0)
|
|
49
|
+
rspec-mocks (3.8.0)
|
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
51
|
+
rspec-support (~> 3.8.0)
|
|
52
|
+
rspec-support (3.8.0)
|
|
53
|
+
thread_safe (0.3.6)
|
|
54
|
+
tzinfo (1.2.5)
|
|
55
|
+
thread_safe (~> 0.1)
|
|
56
|
+
|
|
57
|
+
PLATFORMS
|
|
58
|
+
ruby
|
|
59
|
+
|
|
60
|
+
DEPENDENCIES
|
|
61
|
+
active_record-errors-localize!
|
|
62
|
+
activerecord-nulldb-adapter
|
|
63
|
+
bundler (~> 1.17)
|
|
64
|
+
pry
|
|
65
|
+
pry-byebug
|
|
66
|
+
rake (~> 10.0)
|
|
67
|
+
rspec (~> 3.0)
|
|
68
|
+
|
|
69
|
+
BUNDLED WITH
|
|
70
|
+
1.17.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 松本和也
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# ActiveRecord::Errors::Localize
|
|
2
|
+
|
|
3
|
+
Localize & Customize ActiveRecord Error messages to show user.
|
|
4
|
+
|
|
5
|
+
## Motivation
|
|
6
|
+
In default, [ActiveRecord Errors can not be localized](https://github.com/rails/rails/issues/35147) other than [ActiveRecord::RecordInvalid](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/validations.rb#L22)
|
|
7
|
+
|
|
8
|
+
```ruby
|
|
9
|
+
begin
|
|
10
|
+
User.find(0)
|
|
11
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
12
|
+
p(e.message) # => "Couldn't find User with 'id'=0" This message is static and can not change and localize
|
|
13
|
+
end
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Most of cases, it's no problem ActiveRecord Error messages are only english,
|
|
17
|
+
but in some cases like error handler below, it needs localization.
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
module ErrorHandler
|
|
21
|
+
extend ActiveSupport::Concern
|
|
22
|
+
included do
|
|
23
|
+
# ... other rescue_from
|
|
24
|
+
rescue_from ActiveRecord::RecordNotFound do |e|
|
|
25
|
+
# this error message is showed to user, need to localize.
|
|
26
|
+
render json: { error_message: e.message }, status: :not_found
|
|
27
|
+
end
|
|
28
|
+
# ...
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
By using this gem, you can use solve this problem in simple way :)
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
Add this line to your application's Gemfile:
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
gem 'active_record-errors-localize'
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
And then execute:
|
|
44
|
+
|
|
45
|
+
$ bundle
|
|
46
|
+
|
|
47
|
+
Or install it yourself as:
|
|
48
|
+
|
|
49
|
+
$ gem install active_record-errors-localize
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
First, add localization data to i18n localization file.
|
|
54
|
+
Add key of {lang}.activerecord.errors.messges.{underscored_error_class_name}
|
|
55
|
+
|
|
56
|
+
Customize example
|
|
57
|
+
```yaml:en.yaml
|
|
58
|
+
en:
|
|
59
|
+
activerecord:
|
|
60
|
+
models:
|
|
61
|
+
user: User
|
|
62
|
+
errors:
|
|
63
|
+
messages:
|
|
64
|
+
record_not_found: "Sorry, We can't find your %{model} ID: %{id} PrimaryKey: %{primary_key}" # => Sorry, We can't find your User ID: 1 PrimaryKey: id
|
|
65
|
+
record_not_saved: "%{record} is not saved reason: %{errors}" # => User is not saved reason: xxxxx
|
|
66
|
+
record_not_destroyed: "%{record} is not deleted reason: %{errors}" # => User is not deleted reason: yyyyy
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Localize example
|
|
70
|
+
```yaml:ja.yaml
|
|
71
|
+
ja:
|
|
72
|
+
activerecord:
|
|
73
|
+
models:
|
|
74
|
+
user: ユーザー
|
|
75
|
+
errors:
|
|
76
|
+
messages:
|
|
77
|
+
record_not_found: "%{model}が見つかりません。" #=> ユーザーが見つかりません。
|
|
78
|
+
record_not_saved: "%{record}が保存されませんでした。" #=> ユーザーが保存されませんでした。
|
|
79
|
+
record_not_destroyed: "%{record}が削除されませんでした。" #=> ユーザーが削除されませんでした。
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Now, you can use #i18n_message method like this.
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
# Need to write using cuz implemented by refinements
|
|
86
|
+
using ActiveRecord::Errors::Localize
|
|
87
|
+
begin
|
|
88
|
+
User.find(0)
|
|
89
|
+
rescue ActiveRecord::RecordNotFound => e
|
|
90
|
+
# Can get localized message by #i18n_message
|
|
91
|
+
p(e.i18n_message) # => "ユーザーが見つかりません。"
|
|
92
|
+
end
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
You can see more practical error handler usage in [this example](https://github.com/kazuooooo/active_record-errors-localize/blob/master/examples/error_handler.rb)
|
|
96
|
+
|
|
97
|
+
## Development
|
|
98
|
+
|
|
99
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
100
|
+
|
|
101
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
102
|
+
|
|
103
|
+
## Contributing
|
|
104
|
+
|
|
105
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record-errors-localize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
110
|
+
|
|
111
|
+
## Code of Conduct
|
|
112
|
+
|
|
113
|
+
Everyone interacting in the ActiveRecord::Errors::Localize project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/active_record-errors-localize/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "active_record/errors/localize/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "active_record-errors-localize"
|
|
8
|
+
spec.version = ActiveRecord::Errors::Localize::VERSION
|
|
9
|
+
spec.authors = ["kazuooooo"]
|
|
10
|
+
spec.email = ["matsumotokazuya7@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{ Localize ActiveRecord::ActiveRecordError subclasses error message with I18n }
|
|
13
|
+
spec.description = %q{ Deafault ActiveRecord::Errors class doesn't localize erorr messages. In some cases, we need localized error message for user. With this gem you can localize these error messages using I18n. }
|
|
14
|
+
spec.homepage = "https://github.com/kazuooooo/active_record-errors-localize"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
19
|
+
if spec.respond_to?(:metadata)
|
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
|
|
21
|
+
|
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
23
|
+
spec.metadata["source_code_uri"] = "https://github.com/kazuooooo/active_record-errors-localize"
|
|
24
|
+
spec.metadata["changelog_uri"] = "https://github.com/kazuooooo/active_record-errors-localize/CHANGELOG.md"
|
|
25
|
+
else
|
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
27
|
+
"public gem pushes."
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
34
|
+
end
|
|
35
|
+
spec.bindir = "exe"
|
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
37
|
+
spec.require_paths = ["lib"]
|
|
38
|
+
|
|
39
|
+
spec.add_dependency "activerecord", "~> 5"
|
|
40
|
+
spec.add_dependency "i18n", "~> 1"
|
|
41
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
|
42
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
43
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
44
|
+
spec.add_development_dependency "pry"
|
|
45
|
+
spec.add_development_dependency "pry-byebug"
|
|
46
|
+
spec.add_development_dependency "activerecord-nulldb-adapter"
|
|
47
|
+
end
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "active_record/errors/localize"
|
|
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/setup
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# NOTE: This is example
|
|
2
|
+
# To use in your app, need more rescue_from handler in app
|
|
3
|
+
require 'active_support'
|
|
4
|
+
require 'active_record-errors-localizer'
|
|
5
|
+
using ActiveRecord::Errors::Localize
|
|
6
|
+
# Common error handler in all controllers
|
|
7
|
+
# class UsersController < ApplicationController
|
|
8
|
+
# include ErrorHandler
|
|
9
|
+
# end
|
|
10
|
+
module ErrorHandler
|
|
11
|
+
extend ActiveSupport::Concern
|
|
12
|
+
included do
|
|
13
|
+
# ... other rescue_from
|
|
14
|
+
rescue_from ActiveRecord::RecordNotFound do |e|
|
|
15
|
+
render json: { error_message: e.i18n_message }, status: :not_found
|
|
16
|
+
end
|
|
17
|
+
rescue_from ActiveRecord::RecordNotSaved do |e|
|
|
18
|
+
render json: { error_message: e.i18n_message }, status: :bad_request
|
|
19
|
+
end
|
|
20
|
+
rescue_from ActiveRecord::RecordNotDestroyed do |e|
|
|
21
|
+
render json: { error_message: e.i18n_message }, status: :bad_request
|
|
22
|
+
end
|
|
23
|
+
# ... other rescue_from
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require "active_record/errors/localize/version"
|
|
2
|
+
require 'active_record'
|
|
3
|
+
require 'active_support'
|
|
4
|
+
require 'i18n'
|
|
5
|
+
require 'pry'
|
|
6
|
+
|
|
7
|
+
module ActiveRecord
|
|
8
|
+
module Errors
|
|
9
|
+
# Localize ActiveRcord error messages.
|
|
10
|
+
# It uses refinements, cuz do not want to change ActiveRecord behavior as possible and
|
|
11
|
+
# Intended to use this gem in rare situation, we need return error message for user.
|
|
12
|
+
# Like error handler exmple below.
|
|
13
|
+
# @see example.rb
|
|
14
|
+
module Localize
|
|
15
|
+
refine ActiveRecordError do
|
|
16
|
+
# Get error class under scored name
|
|
17
|
+
# to detect which key use for localize.
|
|
18
|
+
def error_under_scored_name
|
|
19
|
+
self.class.name.demodulize.underscore
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# default
|
|
23
|
+
def i18n_message
|
|
24
|
+
I18n.t("activerecord.errors.messages.#{error_under_scored_name}")
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
refine RecordNotFound do
|
|
28
|
+
def i18n_message
|
|
29
|
+
I18n.t(
|
|
30
|
+
"activerecord.errors.messages.#{error_under_scored_name}",
|
|
31
|
+
model: model.constantize.model_name.human,
|
|
32
|
+
primary_key: primary_key,
|
|
33
|
+
id: id,
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
refine RecordNotSaved do
|
|
39
|
+
def i18n_message
|
|
40
|
+
I18n.t(
|
|
41
|
+
"activerecord.errors.messages.#{error_under_scored_name}",
|
|
42
|
+
record: record.model_name.human,
|
|
43
|
+
errors: record.errors.full_messages.join(', ')
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
refine RecordNotDestroyed do
|
|
49
|
+
def i18n_message
|
|
50
|
+
I18n.t(
|
|
51
|
+
"activerecord.errors.messages.#{error_under_scored_name}",
|
|
52
|
+
record: record.model_name.human,
|
|
53
|
+
errors: record.errors.full_messages.join(', ')
|
|
54
|
+
)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: active_record-errors-localize
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- kazuooooo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-02-06 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activerecord
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '5'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '5'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: i18n
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.17'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.17'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '10.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '10.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '3.0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: pry
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: pry-byebug
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: activerecord-nulldb-adapter
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - ">="
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '0'
|
|
125
|
+
description: " Deafault ActiveRecord::Errors class doesn't localize erorr messages.
|
|
126
|
+
In some cases, we need localized error message for user. With this gem you can localize
|
|
127
|
+
these error messages using I18n. "
|
|
128
|
+
email:
|
|
129
|
+
- matsumotokazuya7@gmail.com
|
|
130
|
+
executables: []
|
|
131
|
+
extensions: []
|
|
132
|
+
extra_rdoc_files: []
|
|
133
|
+
files:
|
|
134
|
+
- ".gitignore"
|
|
135
|
+
- ".rspec"
|
|
136
|
+
- ".travis.yml"
|
|
137
|
+
- ".vscode/settings.json"
|
|
138
|
+
- CHANGELOG.md
|
|
139
|
+
- CODE_OF_CONDUCT.md
|
|
140
|
+
- Gemfile
|
|
141
|
+
- Gemfile.lock
|
|
142
|
+
- LICENSE.txt
|
|
143
|
+
- README.md
|
|
144
|
+
- Rakefile
|
|
145
|
+
- active_record-errors-localize.gemspec
|
|
146
|
+
- bin/console
|
|
147
|
+
- bin/setup
|
|
148
|
+
- config/locales_examples/ja.yml
|
|
149
|
+
- examples/error_handler.rb
|
|
150
|
+
- lib/active_record/errors/localize.rb
|
|
151
|
+
- lib/active_record/errors/localize/version.rb
|
|
152
|
+
homepage: https://github.com/kazuooooo/active_record-errors-localize
|
|
153
|
+
licenses:
|
|
154
|
+
- MIT
|
|
155
|
+
metadata:
|
|
156
|
+
allowed_push_host: https://rubygems.org/
|
|
157
|
+
homepage_uri: https://github.com/kazuooooo/active_record-errors-localize
|
|
158
|
+
source_code_uri: https://github.com/kazuooooo/active_record-errors-localize
|
|
159
|
+
changelog_uri: https://github.com/kazuooooo/active_record-errors-localize/CHANGELOG.md
|
|
160
|
+
post_install_message:
|
|
161
|
+
rdoc_options: []
|
|
162
|
+
require_paths:
|
|
163
|
+
- lib
|
|
164
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
165
|
+
requirements:
|
|
166
|
+
- - ">="
|
|
167
|
+
- !ruby/object:Gem::Version
|
|
168
|
+
version: '0'
|
|
169
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
170
|
+
requirements:
|
|
171
|
+
- - ">="
|
|
172
|
+
- !ruby/object:Gem::Version
|
|
173
|
+
version: '0'
|
|
174
|
+
requirements: []
|
|
175
|
+
rubyforge_project:
|
|
176
|
+
rubygems_version: 2.7.6
|
|
177
|
+
signing_key:
|
|
178
|
+
specification_version: 4
|
|
179
|
+
summary: Localize ActiveRecord::ActiveRecordError subclasses error message with I18n
|
|
180
|
+
test_files: []
|