model_error_messages 1.0.1 → 1.0.2
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/Rakefile +1 -1
- data/lib/model_error_messages/helpers.rb +35 -23
- data/lib/model_error_messages/version.rb +1 -1
- data/spec/model_error_messages/helpers_spec.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3346cf1d3cc91ec5fa3a8befc871931b1087b33a
|
4
|
+
data.tar.gz: cf709e9fad5263088ae0e3ffba6bac1a8fde6439
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d33d97a77439dd700d9afdf0e4ab471dd9f7f4c1ee20bf3c00cc9ae880a488d648c68bb37dd3fd0fc308126ae83415e7c40e9d953c3c3eb30320fb3aecbbbf5
|
7
|
+
data.tar.gz: 1ad227a9061979297a003d05ec3a8de746b5e000a41a887c7cf062b732117a4431811558a05ba1e1532715f7eb2ce39cfbc51d8223e9716c11f50b9f714742d2
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://travis-ci.org/christophemaximin/model_error_messages)
|
4
4
|
[](https://rubygems.org/gems/model_error_messages)
|
5
|
+
[](https://codeclimate.com/github/christophemaximin/model_error_messages)
|
5
6
|
|
6
7
|
A simple Rails helper which displays a HTML div with the validation errors attached to a model.
|
7
8
|
|
@@ -16,6 +17,7 @@ gem 'model_error_messages'
|
|
16
17
|
## What this gem does / How to use
|
17
18
|
|
18
19
|
This gem allows you to use a helper called `model_error_messages`.
|
20
|
+
By default, the wrapper `div` has the [Bootstrap](http://getbootstrap.com)-friendly classes `alert alert-danger`.
|
19
21
|
If you have a typical form, you would want to display `model_error_messages(@model)` next to it, which will render something like:
|
20
22
|
|
21
23
|
```html
|
data/Rakefile
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
1
3
|
module ModelErrorMessages
|
2
4
|
module Helpers
|
3
|
-
include ActionView::Helpers::TagHelper
|
4
|
-
|
5
5
|
def model_error_messages(model, options = {})
|
6
6
|
return '' if model.nil?
|
7
7
|
return '' if model.errors.full_messages.empty?
|
8
|
-
config = local_config(options)
|
9
|
-
errors_wrapper(model, config)
|
10
|
-
end
|
11
8
|
|
12
|
-
|
9
|
+
private_helpers = ModelErrorMessages::PrivateHelpers
|
10
|
+
config = private_helpers.local_config(options)
|
11
|
+
private_helpers.errors_wrapper(model, config)
|
12
|
+
end
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
+
module PrivateHelpers
|
16
|
+
def self.errors_wrapper(model, config)
|
15
17
|
class_attr = wrapper_class_attr(model, config)
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
div_string = [
|
20
|
+
config.prepend_html,
|
21
|
+
errors_list(model, config),
|
22
|
+
config.append_html
|
23
|
+
].join.html_safe
|
24
|
+
|
25
|
+
tag(:div, div_string, class: class_attr)
|
24
26
|
end
|
25
27
|
|
26
|
-
def wrapper_class_attr(model, config)
|
28
|
+
def self.wrapper_class_attr(model, config)
|
27
29
|
if config.classes.is_a?(Proc)
|
28
30
|
config.classes.call(model)
|
29
31
|
else
|
@@ -31,26 +33,36 @@ module ModelErrorMessages
|
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
34
|
-
def errors_list(model, config)
|
36
|
+
def self.errors_list(model, config)
|
35
37
|
messages = model.errors.full_messages
|
36
38
|
|
37
39
|
if messages.count == 1 && config.single_error_in_paragraph
|
38
|
-
return
|
40
|
+
return tag(:p, messages.first)
|
39
41
|
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
ul_string = messages.map do |message|
|
44
|
+
tag(:li, message)
|
45
|
+
end.join.html_safe
|
46
|
+
|
47
|
+
tag(:ul, ul_string)
|
46
48
|
end
|
47
49
|
|
48
|
-
def local_config(options)
|
50
|
+
def self.local_config(options)
|
49
51
|
config = ModelErrorMessages.configuration.clone
|
50
52
|
options.each_pair do |k, v|
|
51
53
|
config.send(k.to_s + '=', v)
|
52
54
|
end
|
53
55
|
config
|
54
56
|
end
|
57
|
+
|
58
|
+
def self.tag(name, value, attributes = {})
|
59
|
+
string_attributes = attributes.inject('') do |attrs, pair|
|
60
|
+
unless pair.last.nil?
|
61
|
+
attrs << %( #{pair.first}="#{CGI.escapeHTML(pair.last.to_s)}")
|
62
|
+
end
|
63
|
+
attrs
|
64
|
+
end
|
65
|
+
"<#{name}#{string_attributes}>#{value}</#{name}>"
|
66
|
+
end
|
55
67
|
end
|
56
68
|
end
|
@@ -69,8 +69,8 @@ describe 'Helpers' do
|
|
69
69
|
let(:result) { model_error_messages(model, options) }
|
70
70
|
|
71
71
|
before do
|
72
|
-
configuration.prepend_html =
|
73
|
-
configuration.append_html =
|
72
|
+
configuration.prepend_html = '<h1>An error occurred</h1>'
|
73
|
+
configuration.append_html = '<p>Please try again.</p>'
|
74
74
|
configuration.classes = model.class.model_name.param_key + '-foo bar'
|
75
75
|
end
|
76
76
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_error_messages
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christophe Maximin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|