model_error_messages 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +3 -0
- data/.rubocop.yml +11 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +190 -0
- data/Guardfile +19 -0
- data/LICENSE +9 -0
- data/README.md +107 -0
- data/lib/model_error_messages/configuration.rb +35 -0
- data/lib/model_error_messages/helpers.rb +56 -0
- data/lib/model_error_messages/railtie.rb +10 -0
- data/lib/model_error_messages/version.rb +9 -0
- data/lib/model_error_messages.rb +3 -0
- data/model_error_messages.gemspec +32 -0
- data/spec/model_error_messages/configuration_spec.rb +28 -0
- data/spec/model_error_messages/helpers_spec.rb +88 -0
- data/spec/spec_helper.rb +15 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bb46994646e4355347eb467e290653f77c7f4315
|
4
|
+
data.tar.gz: 5f0d440fbfa857ac32c245d5d641a4dbc7b3ef54
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7cc6ba2d96f6c7e8b28468beaa9c7908ea8019dc4b802061a4ca62a17a2c71de488997ee44de37a9cde7e28db9df4ff99eee10ca93a43bfaeb1304f9f065c980
|
7
|
+
data.tar.gz: 5839e44fb3b65d8e0109d24dbca1def1bb4729ed8c14b88514e2fa2b7e40a48917be5c1885667fa25ae69e0845ef15b9e5c819e221824f588521a7b6f00283b2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisabledByDefault: false
|
3
|
+
TargetRubyVersion: 2.3
|
4
|
+
|
5
|
+
Style/Documentation:
|
6
|
+
Description: 'Document classes and non-namespace modules.'
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Style/FrozenStringLiteralComment:
|
10
|
+
Description: 'Missing frozen string literal comment.'
|
11
|
+
Enabled: false
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
model_error_messages (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actioncable (5.0.0.1)
|
10
|
+
actionpack (= 5.0.0.1)
|
11
|
+
nio4r (~> 1.2)
|
12
|
+
websocket-driver (~> 0.6.1)
|
13
|
+
actionmailer (5.0.0.1)
|
14
|
+
actionpack (= 5.0.0.1)
|
15
|
+
actionview (= 5.0.0.1)
|
16
|
+
activejob (= 5.0.0.1)
|
17
|
+
mail (~> 2.5, >= 2.5.4)
|
18
|
+
rails-dom-testing (~> 2.0)
|
19
|
+
actionpack (5.0.0.1)
|
20
|
+
actionview (= 5.0.0.1)
|
21
|
+
activesupport (= 5.0.0.1)
|
22
|
+
rack (~> 2.0)
|
23
|
+
rack-test (~> 0.6.3)
|
24
|
+
rails-dom-testing (~> 2.0)
|
25
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
26
|
+
actionview (5.0.0.1)
|
27
|
+
activesupport (= 5.0.0.1)
|
28
|
+
builder (~> 3.1)
|
29
|
+
erubis (~> 2.7.0)
|
30
|
+
rails-dom-testing (~> 2.0)
|
31
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
32
|
+
activejob (5.0.0.1)
|
33
|
+
activesupport (= 5.0.0.1)
|
34
|
+
globalid (>= 0.3.6)
|
35
|
+
activemodel (5.0.0.1)
|
36
|
+
activesupport (= 5.0.0.1)
|
37
|
+
activerecord (5.0.0.1)
|
38
|
+
activemodel (= 5.0.0.1)
|
39
|
+
activesupport (= 5.0.0.1)
|
40
|
+
arel (~> 7.0)
|
41
|
+
activesupport (5.0.0.1)
|
42
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
43
|
+
i18n (~> 0.7)
|
44
|
+
minitest (~> 5.1)
|
45
|
+
tzinfo (~> 1.1)
|
46
|
+
arel (7.1.4)
|
47
|
+
ast (2.3.0)
|
48
|
+
builder (3.2.2)
|
49
|
+
coderay (1.1.1)
|
50
|
+
concurrent-ruby (1.0.2)
|
51
|
+
diff-lcs (1.2.5)
|
52
|
+
erubis (2.7.0)
|
53
|
+
ffi (1.9.14)
|
54
|
+
formatador (0.2.5)
|
55
|
+
globalid (0.3.7)
|
56
|
+
activesupport (>= 4.1.0)
|
57
|
+
guard (2.14.0)
|
58
|
+
formatador (>= 0.2.4)
|
59
|
+
listen (>= 2.7, < 4.0)
|
60
|
+
lumberjack (~> 1.0)
|
61
|
+
nenv (~> 0.1)
|
62
|
+
notiffany (~> 0.0)
|
63
|
+
pry (>= 0.9.12)
|
64
|
+
shellany (~> 0.0)
|
65
|
+
thor (>= 0.18.1)
|
66
|
+
guard-compat (1.2.1)
|
67
|
+
guard-rspec (4.7.3)
|
68
|
+
guard (~> 2.1)
|
69
|
+
guard-compat (~> 1.1)
|
70
|
+
rspec (>= 2.99.0, < 4.0)
|
71
|
+
guard-rubocop (1.2.0)
|
72
|
+
guard (~> 2.0)
|
73
|
+
rubocop (~> 0.20)
|
74
|
+
i18n (0.7.0)
|
75
|
+
listen (3.1.5)
|
76
|
+
rb-fsevent (~> 0.9, >= 0.9.4)
|
77
|
+
rb-inotify (~> 0.9, >= 0.9.7)
|
78
|
+
ruby_dep (~> 1.2)
|
79
|
+
loofah (2.0.3)
|
80
|
+
nokogiri (>= 1.5.9)
|
81
|
+
lumberjack (1.0.10)
|
82
|
+
mail (2.6.4)
|
83
|
+
mime-types (>= 1.16, < 4)
|
84
|
+
method_source (0.8.2)
|
85
|
+
mime-types (3.1)
|
86
|
+
mime-types-data (~> 3.2015)
|
87
|
+
mime-types-data (3.2016.0521)
|
88
|
+
mini_portile2 (2.1.0)
|
89
|
+
minitest (5.9.1)
|
90
|
+
nenv (0.3.0)
|
91
|
+
nio4r (1.2.1)
|
92
|
+
nokogiri (1.6.8.1)
|
93
|
+
mini_portile2 (~> 2.1.0)
|
94
|
+
notiffany (0.1.1)
|
95
|
+
nenv (~> 0.1)
|
96
|
+
shellany (~> 0.0)
|
97
|
+
parser (2.3.1.4)
|
98
|
+
ast (~> 2.2)
|
99
|
+
powerpack (0.1.1)
|
100
|
+
pry (0.10.4)
|
101
|
+
coderay (~> 1.1.0)
|
102
|
+
method_source (~> 0.8.1)
|
103
|
+
slop (~> 3.4)
|
104
|
+
rack (2.0.1)
|
105
|
+
rack-test (0.6.3)
|
106
|
+
rack (>= 1.0)
|
107
|
+
rails (5.0.0.1)
|
108
|
+
actioncable (= 5.0.0.1)
|
109
|
+
actionmailer (= 5.0.0.1)
|
110
|
+
actionpack (= 5.0.0.1)
|
111
|
+
actionview (= 5.0.0.1)
|
112
|
+
activejob (= 5.0.0.1)
|
113
|
+
activemodel (= 5.0.0.1)
|
114
|
+
activerecord (= 5.0.0.1)
|
115
|
+
activesupport (= 5.0.0.1)
|
116
|
+
bundler (>= 1.3.0, < 2.0)
|
117
|
+
railties (= 5.0.0.1)
|
118
|
+
sprockets-rails (>= 2.0.0)
|
119
|
+
rails-dom-testing (2.0.1)
|
120
|
+
activesupport (>= 4.2.0, < 6.0)
|
121
|
+
nokogiri (~> 1.6.0)
|
122
|
+
rails-html-sanitizer (1.0.3)
|
123
|
+
loofah (~> 2.0)
|
124
|
+
railties (5.0.0.1)
|
125
|
+
actionpack (= 5.0.0.1)
|
126
|
+
activesupport (= 5.0.0.1)
|
127
|
+
method_source
|
128
|
+
rake (>= 0.8.7)
|
129
|
+
thor (>= 0.18.1, < 2.0)
|
130
|
+
rainbow (2.1.0)
|
131
|
+
rake (11.3.0)
|
132
|
+
rb-fchange (0.0.6)
|
133
|
+
ffi
|
134
|
+
rb-fsevent (0.9.7)
|
135
|
+
rb-inotify (0.9.7)
|
136
|
+
ffi (>= 0.5.0)
|
137
|
+
rspec (3.5.0)
|
138
|
+
rspec-core (~> 3.5.0)
|
139
|
+
rspec-expectations (~> 3.5.0)
|
140
|
+
rspec-mocks (~> 3.5.0)
|
141
|
+
rspec-core (3.5.4)
|
142
|
+
rspec-support (~> 3.5.0)
|
143
|
+
rspec-expectations (3.5.0)
|
144
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
145
|
+
rspec-support (~> 3.5.0)
|
146
|
+
rspec-mocks (3.5.0)
|
147
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
148
|
+
rspec-support (~> 3.5.0)
|
149
|
+
rspec-support (3.5.0)
|
150
|
+
rubocop (0.44.1)
|
151
|
+
parser (>= 2.3.1.1, < 3.0)
|
152
|
+
powerpack (~> 0.1)
|
153
|
+
rainbow (>= 1.99.1, < 3.0)
|
154
|
+
ruby-progressbar (~> 1.7)
|
155
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
156
|
+
ruby-progressbar (1.8.1)
|
157
|
+
ruby_dep (1.5.0)
|
158
|
+
shellany (0.0.1)
|
159
|
+
slop (3.6.0)
|
160
|
+
sprockets (3.7.0)
|
161
|
+
concurrent-ruby (~> 1.0)
|
162
|
+
rack (> 1, < 3)
|
163
|
+
sprockets-rails (3.2.0)
|
164
|
+
actionpack (>= 4.0)
|
165
|
+
activesupport (>= 4.0)
|
166
|
+
sprockets (>= 3.0.0)
|
167
|
+
thor (0.19.1)
|
168
|
+
thread_safe (0.3.5)
|
169
|
+
tzinfo (1.2.2)
|
170
|
+
thread_safe (~> 0.1)
|
171
|
+
unicode-display_width (1.1.1)
|
172
|
+
websocket-driver (0.6.4)
|
173
|
+
websocket-extensions (>= 0.1.0)
|
174
|
+
websocket-extensions (0.1.2)
|
175
|
+
|
176
|
+
PLATFORMS
|
177
|
+
ruby
|
178
|
+
|
179
|
+
DEPENDENCIES
|
180
|
+
guard-rspec (~> 4.7)
|
181
|
+
guard-rubocop (~> 1.2)
|
182
|
+
model_error_messages!
|
183
|
+
rails (~> 5.0)
|
184
|
+
rb-fchange (~> 0)
|
185
|
+
rb-fsevent (~> 0.9)
|
186
|
+
rb-inotify (~> 0.9)
|
187
|
+
rspec (~> 3.5)
|
188
|
+
|
189
|
+
BUNDLED WITH
|
190
|
+
1.13.5
|
data/Guardfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
2
|
+
require 'guard/rspec/dsl'
|
3
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
4
|
+
|
5
|
+
rspec = dsl.rspec
|
6
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
7
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
8
|
+
watch(rspec.spec_files)
|
9
|
+
|
10
|
+
ruby = dsl.ruby
|
11
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
12
|
+
end
|
13
|
+
|
14
|
+
guard :rubocop do
|
15
|
+
watch(/.+\.rb$/)
|
16
|
+
watch(/^Gemfile$/)
|
17
|
+
watch(/.*\.gemspec$/)
|
18
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
19
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016, Christophe Maximin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# Model Error Messages
|
2
|
+
|
3
|
+
A simple Rails helper which displays a HTML div with the validation errors attached to a model.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
**Model Error Messages**'s installation is pretty standard, add the following line to your `Gemfile`, then run `bundle install`:
|
8
|
+
|
9
|
+
```rb
|
10
|
+
gem 'model_error_messages'
|
11
|
+
```
|
12
|
+
|
13
|
+
## What this gem does / How to use
|
14
|
+
|
15
|
+
This gem allows you to use a helper called `model_error_messages`.
|
16
|
+
If you have a typical form, you would want to display `model_error_messages(@model)` next to it, which will render something like:
|
17
|
+
|
18
|
+
```html
|
19
|
+
<div class="alert alert-danger article-error-messages">
|
20
|
+
<ul>
|
21
|
+
<li>Title can't be blank</li>
|
22
|
+
<li>You must select an author</li>
|
23
|
+
</ul>
|
24
|
+
</div>
|
25
|
+
```
|
26
|
+
|
27
|
+
... or if there is only one error:
|
28
|
+
|
29
|
+
```html
|
30
|
+
<div class="alert alert-danger article-error-messages">
|
31
|
+
<p>Title can't be blank</p>
|
32
|
+
</div>
|
33
|
+
```
|
34
|
+
|
35
|
+
Example of integration:
|
36
|
+
|
37
|
+
```erb
|
38
|
+
<%= model_error_messages(@article) %>
|
39
|
+
|
40
|
+
<%= form_for @article do |f| %>
|
41
|
+
<!-- ... form fields ... -->
|
42
|
+
<% end %>
|
43
|
+
```
|
44
|
+
|
45
|
+
## What this gem doesn't do
|
46
|
+
|
47
|
+
* Include any other dependencies
|
48
|
+
* Influence the generation of errors
|
49
|
+
* Inject or execute any code in your controllers and models
|
50
|
+
* Do anything with the `flash` messages
|
51
|
+
* Anything not listed in "What this gem does"
|
52
|
+
|
53
|
+
## Optional configuration
|
54
|
+
|
55
|
+
You can change the default behavior of `model_error_messages` by:
|
56
|
+
|
57
|
+
## Setting an initializer
|
58
|
+
|
59
|
+
Create a file `config/initializers/model_error_messages.rb` and replace one of the defaults:
|
60
|
+
|
61
|
+
```rb
|
62
|
+
ModelErrorMessages.configure do |config|
|
63
|
+
# Multiple errors will rendered in a several <li> in a <ul>, while one error will be rendered in a <p>
|
64
|
+
config.single_error_in_paragraph = true
|
65
|
+
|
66
|
+
# The following classes will be added in the main wrapper div.
|
67
|
+
config.classes = lambda do |model|
|
68
|
+
[
|
69
|
+
'alert',
|
70
|
+
'alert-danger',
|
71
|
+
model.class.model_name.param_key + '-error-messages'
|
72
|
+
].join(' ')
|
73
|
+
end
|
74
|
+
|
75
|
+
# Note: you can pass a simple string to `config.classes`, example:
|
76
|
+
# config.classes = 'alert alert-danger'
|
77
|
+
|
78
|
+
# HTML that will be added before the list of errors.
|
79
|
+
config.prepend_html = String.new
|
80
|
+
|
81
|
+
# HTML that will be added after the list of errors.
|
82
|
+
config.append_html = String.new
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
## Passing options when calling `model_error_messages`
|
87
|
+
|
88
|
+
Examples:
|
89
|
+
|
90
|
+
```erb
|
91
|
+
<%= model_error_messages(@article, single_error_in_paragraph: false) %>
|
92
|
+
```
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
Don't hesitate to send a pull request!
|
97
|
+
|
98
|
+
## Testing
|
99
|
+
|
100
|
+
```sh
|
101
|
+
$ bundle install
|
102
|
+
$ bundle exec rspec spec/
|
103
|
+
```
|
104
|
+
|
105
|
+
## License
|
106
|
+
|
107
|
+
This software is distributed under the MIT License. Copyright (c) 2016, Christophe Maximin
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ModelErrorMessages
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :single_error_in_paragraph
|
4
|
+
attr_accessor :prepend_html
|
5
|
+
attr_accessor :append_html
|
6
|
+
attr_accessor :classes
|
7
|
+
attr_writer :configuration
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
reset
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
def reset
|
15
|
+
@single_error_in_paragraph = true
|
16
|
+
@prepend_html = ''
|
17
|
+
@append_html = ''
|
18
|
+
@classes = lambda do |model|
|
19
|
+
[
|
20
|
+
'alert',
|
21
|
+
'alert-danger',
|
22
|
+
model.class.model_name.param_key + '-error-messages'
|
23
|
+
].join(' ')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.configuration
|
29
|
+
@configuration ||= Configuration.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.configure
|
33
|
+
yield configuration
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module ModelErrorMessages
|
2
|
+
module Helpers
|
3
|
+
include ActionView::Helpers::TagHelper
|
4
|
+
|
5
|
+
def model_error_messages(model, options = {})
|
6
|
+
return '' if model.nil?
|
7
|
+
return '' if model.errors.full_messages.empty?
|
8
|
+
config = local_config(options)
|
9
|
+
errors_wrapper(model, config)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def errors_wrapper(model, config)
|
15
|
+
class_attr = wrapper_class_attr(model, config)
|
16
|
+
|
17
|
+
content_tag(:div, class: class_attr) do
|
18
|
+
[
|
19
|
+
config.prepend_html,
|
20
|
+
errors_list(model, config),
|
21
|
+
config.append_html
|
22
|
+
].join.html_safe
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def wrapper_class_attr(model, config)
|
27
|
+
if config.classes.is_a?(Proc)
|
28
|
+
config.classes.call(model)
|
29
|
+
else
|
30
|
+
config.classes
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def errors_list(model, config)
|
35
|
+
messages = model.errors.full_messages
|
36
|
+
|
37
|
+
if messages.count == 1 && config.single_error_in_paragraph
|
38
|
+
return content_tag(:p, messages.first)
|
39
|
+
end
|
40
|
+
|
41
|
+
content_tag(:ul) do
|
42
|
+
messages.map do |message|
|
43
|
+
content_tag(:li, message)
|
44
|
+
end.join.html_safe
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def local_config(options)
|
49
|
+
config = ModelErrorMessages.configuration.clone
|
50
|
+
options.each_pair do |k, v|
|
51
|
+
config.send(k.to_s + '=', v)
|
52
|
+
end
|
53
|
+
config
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'model_error_messages/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'model_error_messages'
|
6
|
+
s.version = ModelErrorMessages::VERSION::STRING
|
7
|
+
s.summary =
|
8
|
+
'Simple Rails helper which displays a HTML div' \
|
9
|
+
' with the errors attached to a model.'
|
10
|
+
s.authors = ['Christophe Maximin']
|
11
|
+
s.email = 'christophe.maximin@gmail.com'
|
12
|
+
s.homepage = 'https://github.com/christophemaximin/model_error_messages'
|
13
|
+
s.licenses = ['MIT']
|
14
|
+
|
15
|
+
s.platform = Gem::Platform::RUBY
|
16
|
+
|
17
|
+
s.add_development_dependency 'rspec', '~> 3.5'
|
18
|
+
s.add_development_dependency 'guard-rspec', '~> 4.7'
|
19
|
+
s.add_development_dependency 'guard-rubocop', '~> 1.2'
|
20
|
+
s.add_development_dependency 'rb-inotify', '~> 0.9'
|
21
|
+
s.add_development_dependency 'rb-fsevent', '~> 0.9'
|
22
|
+
s.add_development_dependency 'rb-fchange', '~> 0'
|
23
|
+
s.add_development_dependency 'rails', '~> 5.0'
|
24
|
+
|
25
|
+
s.files = `git ls-files`.split("\n")
|
26
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
27
|
+
s.executables = `git ls-files -- bin/*`.split('\n').map do |f|
|
28
|
+
File.basename(f)
|
29
|
+
end
|
30
|
+
|
31
|
+
s.require_paths = ['lib']
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'ModelErrorMessages::Configuration' do
|
4
|
+
let(:klass) { ModelErrorMessages }
|
5
|
+
subject { klass.configuration }
|
6
|
+
|
7
|
+
it 'can be set by a block' do
|
8
|
+
klass.configure do |config|
|
9
|
+
config.single_error_in_paragraph = 'test'
|
10
|
+
end
|
11
|
+
|
12
|
+
expect(subject.single_error_in_paragraph).to eql('test')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'can be set individually' do
|
16
|
+
subject.single_error_in_paragraph = 'test'
|
17
|
+
expect(subject.single_error_in_paragraph).to eql('test')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'can be resetted' do
|
21
|
+
subject.single_error_in_paragraph = 'test'
|
22
|
+
expect(subject.single_error_in_paragraph).to eql('test')
|
23
|
+
|
24
|
+
subject.reset
|
25
|
+
|
26
|
+
expect(subject.single_error_in_paragraph).not_to eql('test')
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
class ModelStruct < OpenStruct
|
5
|
+
def self.model_name
|
6
|
+
OpenStruct.new(param_key: 'model')
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def mock_model_full_messages(errors_count)
|
11
|
+
case errors_count
|
12
|
+
when 0 then []
|
13
|
+
when 1 then ['Title is missing']
|
14
|
+
when 2 then ['Please select an author', 'Content is missing']
|
15
|
+
else
|
16
|
+
raise 'Please enter a valid `errors` count.'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def mock_model(options = {})
|
21
|
+
model = ModelStruct.new
|
22
|
+
model.errors = OpenStruct.new
|
23
|
+
model.errors.full_messages = mock_model_full_messages(options[:errors])
|
24
|
+
model
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'Helpers' do
|
28
|
+
describe '#model_error_messages' do
|
29
|
+
include ModelErrorMessages::Helpers
|
30
|
+
attr_accessor :output_buffer
|
31
|
+
|
32
|
+
subject { model_error_messages(mock_model) }
|
33
|
+
|
34
|
+
it 'returns nothing if model is nil' do
|
35
|
+
result = model_error_messages(nil)
|
36
|
+
expect(result).to eql('')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns nothing if model has no errors' do
|
40
|
+
result = model_error_messages(mock_model(errors: 0))
|
41
|
+
expect(result).to eql('')
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'with default configuration' do
|
45
|
+
it 'renders correctly with one error' do
|
46
|
+
result = model_error_messages(mock_model(errors: 1))
|
47
|
+
expect(result).to eql(
|
48
|
+
'<div class="alert alert-danger model-error-messages">' \
|
49
|
+
'<p>Title is missing</p>' \
|
50
|
+
'</div>'
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'renders correctly with two errors' do
|
55
|
+
result = model_error_messages(mock_model(errors: 2))
|
56
|
+
expect(result).to eql(
|
57
|
+
'<div class="alert alert-danger model-error-messages">' \
|
58
|
+
'<ul><li>Please select an author</li>' \
|
59
|
+
'<li>Content is missing</li></ul>' \
|
60
|
+
'</div>'
|
61
|
+
)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'with a custom configuration' do
|
66
|
+
let(:configuration) { ModelErrorMessages.configuration }
|
67
|
+
let(:options) { { single_error_in_paragraph: false } }
|
68
|
+
let(:model) { mock_model(errors: 1) }
|
69
|
+
let(:result) { model_error_messages(model, options) }
|
70
|
+
|
71
|
+
before do
|
72
|
+
configuration.prepend_html = content_tag(:h1, 'An error occurred')
|
73
|
+
configuration.append_html = content_tag(:p, 'Please try again.')
|
74
|
+
configuration.classes = model.class.model_name.param_key + '-foo bar'
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'renders correctly' do
|
78
|
+
expect(result).to eql(
|
79
|
+
'<div class="model-foo bar">' \
|
80
|
+
'<h1>An error occurred</h1>' \
|
81
|
+
'<ul><li>Title is missing</li></ul>' \
|
82
|
+
'<p>Please try again.</p>' \
|
83
|
+
'</div>'
|
84
|
+
)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'action_view'
|
3
|
+
require 'action_view/helpers'
|
4
|
+
require 'model_error_messages'
|
5
|
+
require 'model_error_messages/helpers'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.order = :random
|
9
|
+
config.filter_run focus: true
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
|
12
|
+
config.before(:each) do
|
13
|
+
ModelErrorMessages.configuration.reset
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: model_error_messages
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christophe Maximin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard-rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rb-inotify
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rb-fsevent
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.9'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.9'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rb-fchange
|
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: rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '5.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '5.0'
|
111
|
+
description:
|
112
|
+
email: christophe.maximin@gmail.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- ".gitignore"
|
118
|
+
- ".rspec"
|
119
|
+
- ".rubocop.yml"
|
120
|
+
- Gemfile
|
121
|
+
- Gemfile.lock
|
122
|
+
- Guardfile
|
123
|
+
- LICENSE
|
124
|
+
- README.md
|
125
|
+
- lib/model_error_messages.rb
|
126
|
+
- lib/model_error_messages/configuration.rb
|
127
|
+
- lib/model_error_messages/helpers.rb
|
128
|
+
- lib/model_error_messages/railtie.rb
|
129
|
+
- lib/model_error_messages/version.rb
|
130
|
+
- model_error_messages.gemspec
|
131
|
+
- spec/model_error_messages/configuration_spec.rb
|
132
|
+
- spec/model_error_messages/helpers_spec.rb
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
homepage: https://github.com/christophemaximin/model_error_messages
|
135
|
+
licenses:
|
136
|
+
- MIT
|
137
|
+
metadata: {}
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 2.5.1
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: Simple Rails helper which displays a HTML div with the errors attached to
|
158
|
+
a model.
|
159
|
+
test_files:
|
160
|
+
- spec/model_error_messages/configuration_spec.rb
|
161
|
+
- spec/model_error_messages/helpers_spec.rb
|
162
|
+
- spec/spec_helper.rb
|