rails_errors2html 1.0.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rails_errors2html.rb +81 -59
- data/rails_errors2html.gemspec +3 -1
- data/test/rails_errors2html_test.rb +54 -3
- metadata +17 -1
data/lib/rails_errors2html.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Errors2Html
|
2
|
-
VERSION = '1.0
|
2
|
+
VERSION = '1.3.0'
|
3
3
|
|
4
4
|
def Errors2Html.version
|
5
5
|
Errors2Html::VERSION
|
@@ -7,8 +7,9 @@ module Errors2Html
|
|
7
7
|
|
8
8
|
def Errors2Html.dependencies
|
9
9
|
{
|
10
|
-
'
|
11
|
-
'
|
10
|
+
'fattr' => [ 'fattr' , ' >= 2.2.1' ],
|
11
|
+
'map' => [ 'map' , ' >= 6.2.0' ],
|
12
|
+
'rails_view' => [ 'rails_view' , ' >= 1.0.1' ]
|
12
13
|
}
|
13
14
|
end
|
14
15
|
|
@@ -23,10 +24,6 @@ module Errors2Html
|
|
23
24
|
require(lib)
|
24
25
|
end
|
25
26
|
|
26
|
-
class << Errors2Html
|
27
|
-
attr_accessor :template
|
28
|
-
end
|
29
|
-
|
30
27
|
def Errors2Html.to_html(*args)
|
31
28
|
args.flatten!
|
32
29
|
args.compact!
|
@@ -35,23 +32,18 @@ module Errors2Html
|
|
35
32
|
|
36
33
|
errors = Map.new
|
37
34
|
errors[:global] = []
|
38
|
-
errors[:fields] = {}
|
35
|
+
errors[:fields] = {}
|
39
36
|
|
40
37
|
args.each do |e|
|
41
|
-
e.each do |key, messages|
|
42
|
-
key = key.to_s
|
43
|
-
|
38
|
+
flatten(e).each do |key, messages|
|
44
39
|
Array(messages).each do |message|
|
45
40
|
at_least_one_error = true
|
46
41
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
list.push(message.to_s).uniq!
|
42
|
+
if Array(key).join =~ /\A(?:[*]|base)\Z/iomx
|
43
|
+
errors.global.push(message.to_s).uniq!
|
44
|
+
else
|
45
|
+
(errors.fields[key] ||= []).push(message.to_s).uniq!
|
46
|
+
end
|
55
47
|
end
|
56
48
|
end
|
57
49
|
end
|
@@ -67,54 +59,84 @@ module Errors2Html
|
|
67
59
|
if template
|
68
60
|
View.render(:template => template, :locals => locals)
|
69
61
|
else
|
70
|
-
View.render(:inline =>
|
62
|
+
View.render(:inline => inline, :locals => locals)
|
71
63
|
end
|
72
64
|
end
|
73
65
|
|
74
|
-
def
|
75
|
-
|
66
|
+
def Errors2Html.flatten(hashlike)
|
67
|
+
case hashlike
|
68
|
+
when Map
|
69
|
+
hash = Hash.new
|
70
|
+
hashlike.depth_first_each do |key, value|
|
71
|
+
index = key.pop if key.last.is_a?(Integer)
|
72
|
+
(hash[key] ||= []).push(value)
|
73
|
+
end
|
74
|
+
hash
|
75
|
+
else
|
76
|
+
hashlike.respond_to?(:to_hash) ? hashlike.to_hash : hashlike
|
77
|
+
end
|
76
78
|
end
|
77
79
|
|
78
|
-
|
79
|
-
|
80
|
-
|
80
|
+
Fattr(:inline) do
|
81
|
+
<<-erb
|
82
|
+
<div class="errors2html errors-summary">
|
83
|
+
<h4 class="errors-caption">Sorry, we encountered some errors:</h4>
|
84
|
+
|
85
|
+
<% unless errors.global.empty? %>
|
81
86
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
<% unless errors.global.empty? %>
|
87
|
-
<ul class="errors-global-list">
|
88
|
-
<% errors.global.each do |message| %>
|
89
|
-
<li class="errors-message">
|
90
|
-
<%= message %>
|
91
|
-
</li>
|
92
|
-
<% end %>
|
93
|
-
</ul>
|
94
|
-
<% end %>
|
95
|
-
|
96
|
-
<% unless errors.fields.empty? %>
|
97
|
-
<dl class="errors-fields-list">
|
98
|
-
<%
|
99
|
-
errors.fields.each do |key, messages|
|
100
|
-
title = Array(key).join(" ").titleize
|
101
|
-
%>
|
102
|
-
<dt class="errors-title">
|
103
|
-
<%= title %>
|
104
|
-
</dt>
|
105
|
-
|
106
|
-
<% Array(messages).each do |message| %>
|
107
|
-
<dd class="errors-message">
|
87
|
+
<ul class="errors-global-list">
|
88
|
+
<% errors.global.each do |message| %>
|
89
|
+
<li class="errors-message">
|
108
90
|
<%= message %>
|
109
|
-
</
|
91
|
+
</li>
|
110
92
|
<% end %>
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
93
|
+
</ul>
|
94
|
+
<% end %>
|
95
|
+
|
96
|
+
<% unless errors.fields.empty? %>
|
97
|
+
|
98
|
+
<dl class="errors-fields-list">
|
99
|
+
<%
|
100
|
+
errors.fields.each do |key, messages|
|
101
|
+
title = Array(key).join(" ").titleize
|
102
|
+
%>
|
103
|
+
<dt class="errors-title">
|
104
|
+
<%= title %>
|
105
|
+
</dt>
|
106
|
+
<% Array(messages).each do |message| %>
|
107
|
+
<dd class="errors-message">
|
108
|
+
<%= message %>
|
109
|
+
</dd>
|
110
|
+
|
111
|
+
<% end %>
|
112
|
+
<% end %>
|
113
|
+
</dl>
|
114
|
+
<% end %>
|
115
|
+
</div>
|
116
|
+
erb
|
117
|
+
end
|
118
|
+
|
119
|
+
Fattr(:template){ nil }
|
120
|
+
|
121
|
+
module Mixin
|
122
|
+
def to_html
|
123
|
+
::Errors2Html.to_html(self)
|
124
|
+
end
|
125
|
+
|
126
|
+
def to_s
|
127
|
+
to_html
|
128
|
+
end
|
129
|
+
end
|
116
130
|
end
|
117
131
|
|
118
|
-
|
119
|
-
|
132
|
+
##
|
133
|
+
#
|
134
|
+
require 'active_model' unless defined?(ActiveModel)
|
135
|
+
|
136
|
+
ActiveModel::Errors.send(:include, Errors2Html::Mixin)
|
120
137
|
|
138
|
+
ActiveModel::Errors.class_eval do
|
139
|
+
def inspect(*args, &block)
|
140
|
+
to_hash.inspect(*args, &block)
|
141
|
+
end
|
142
|
+
end
|
data/rails_errors2html.gemspec
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
Gem::Specification::new do |spec|
|
5
5
|
spec.name = "rails_errors2html"
|
6
|
-
spec.version = "1.0
|
6
|
+
spec.version = "1.3.0"
|
7
7
|
spec.platform = Gem::Platform::RUBY
|
8
8
|
spec.summary = "rails_errors2html"
|
9
9
|
spec.description = "<%= form_for @post do %> <%= @post.errors.to_html %>"
|
@@ -25,6 +25,8 @@ Gem::Specification::new do |spec|
|
|
25
25
|
spec.test_files = nil
|
26
26
|
|
27
27
|
|
28
|
+
spec.add_dependency(*["fattr", " >= 2.2.1"])
|
29
|
+
|
28
30
|
spec.add_dependency(*["map", " >= 6.2.0"])
|
29
31
|
|
30
32
|
spec.add_dependency(*["rails_view", " >= 1.0.1"])
|
@@ -3,6 +3,8 @@ require_relative '../test/testing.rb'
|
|
3
3
|
|
4
4
|
Testing Errors2Html do
|
5
5
|
|
6
|
+
##
|
7
|
+
#
|
6
8
|
testing 'simple rendering' do
|
7
9
|
errors = ActiveModel::Errors.new(base = Map.new)
|
8
10
|
errors.add :base, 'error on base'
|
@@ -22,7 +24,6 @@ Testing Errors2Html do
|
|
22
24
|
<dt class="errors-title">
|
23
25
|
Field
|
24
26
|
</dt>
|
25
|
-
|
26
27
|
<dd class="errors-message">
|
27
28
|
error on field
|
28
29
|
</dd>
|
@@ -30,8 +31,58 @@ Testing Errors2Html do
|
|
30
31
|
</div>
|
31
32
|
__
|
32
33
|
|
33
|
-
compress
|
34
|
-
|
34
|
+
assert{ compress(errors.to_html) == compress(expected) }
|
35
|
+
assert{ compress(errors) == compress(expected) }
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
##
|
40
|
+
#
|
41
|
+
testing 'map-y errors' do
|
42
|
+
errors = Map.new
|
43
|
+
errors.set '*', 'error on base'
|
44
|
+
errors.set 'field', 'error on field'
|
45
|
+
errors.set 'a', 'b', 'c', %w'one two three'
|
46
|
+
errors.extend(Errors2Html::Mixin)
|
47
|
+
|
48
|
+
expected = <<-__
|
49
|
+
<div class="errors2html errors-summary">
|
50
|
+
<h4 class="errors-caption">Sorry, we encountered some errors:</h4>
|
51
|
+
|
52
|
+
|
53
|
+
<ul class="errors-global-list">
|
54
|
+
<li class="errors-message">
|
55
|
+
error on base
|
56
|
+
</li>
|
57
|
+
</ul>
|
58
|
+
|
59
|
+
|
60
|
+
<dl class="errors-fields-list">
|
61
|
+
<dt class="errors-title">
|
62
|
+
Field
|
63
|
+
</dt>
|
64
|
+
<dd class="errors-message">
|
65
|
+
error on field
|
66
|
+
</dd>
|
67
|
+
|
68
|
+
<dt class="errors-title">
|
69
|
+
A B C
|
70
|
+
</dt>
|
71
|
+
<dd class="errors-message">
|
72
|
+
one
|
73
|
+
</dd>
|
74
|
+
|
75
|
+
<dd class="errors-message">
|
76
|
+
two
|
77
|
+
</dd>
|
78
|
+
|
79
|
+
<dd class="errors-message">
|
80
|
+
three
|
81
|
+
</dd>
|
82
|
+
|
83
|
+
</dl>
|
84
|
+
</div>
|
85
|
+
__
|
35
86
|
|
36
87
|
assert{ compress(errors.to_html) == compress(expected) }
|
37
88
|
assert{ compress(errors) == compress(expected) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_errors2html
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,6 +11,22 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-12-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: fattr
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.2.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.2.1
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: map
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|