scrivener_errors 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -1
- data/lib/scrivener_errors.rb +16 -9
- data/scrivener_errors.gemspec +1 -1
- data/test/scrivener_errors_test.rb +16 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 016d5a3108de8581c6b9cd88e3c812a43d3a6f08
|
4
|
+
data.tar.gz: 3085e7f26acd7ba51e55bdb43bcaa6a40a06de53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c37510e2cb059bf3e008558d7900cfc835775babbae35d4c36940fd6b139140f78a9124e8e1815faa36eda70a7c9defd0c7bc07fde10c82088f59709fdf78c1b
|
7
|
+
data.tar.gz: ffe5c0fa4217bbc5c28575def83d8b115b235fbdb6ed60eba58e71122d00029cb524ae68e245bc15df61e7ec7881efdb149eafc4ecab14c2f78c0ef2aa3346d0
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Email is not an email, password can't be blank
|
|
20
20
|
```
|
21
21
|
|
22
22
|
ScrivenerErrors will present these error hashes as a string
|
23
|
-
or list of
|
23
|
+
or list of error messages for use in failure notices or
|
24
24
|
form errors.
|
25
25
|
|
26
26
|
This also comes with a small plugin for [Cuba](http://cuba.is/)
|
@@ -36,6 +36,7 @@ Usage
|
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
# Setup the plugin for Cuba
|
39
|
+
require "scrivener_errors"
|
39
40
|
Cuba.plugin ScrivenerErrors::Helpers
|
40
41
|
|
41
42
|
# Inside a Cuba action
|
@@ -46,6 +47,13 @@ if !filter.valid?
|
|
46
47
|
end
|
47
48
|
```
|
48
49
|
|
50
|
+
You can also get a string of comma joined errors for a specific attribute.
|
51
|
+
It does not include the attribute name in this case.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
scrivener_errors[:email] # => "is too short, is not an email"
|
55
|
+
```
|
56
|
+
|
49
57
|
Notes
|
50
58
|
-----
|
51
59
|
|
data/lib/scrivener_errors.rb
CHANGED
@@ -9,7 +9,7 @@ class ScrivenerErrors
|
|
9
9
|
:not_present => "can't be blank",
|
10
10
|
:not_url => "is not a url",
|
11
11
|
:too_short => "is too short"
|
12
|
-
}
|
12
|
+
}
|
13
13
|
|
14
14
|
def initialize(scrivener)
|
15
15
|
@scrivener = scrivener
|
@@ -22,24 +22,31 @@ class ScrivenerErrors
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
25
|
+
def [](att)
|
26
|
+
if (errors = scrivener.errors[att.to_sym])
|
27
|
+
errors.map { |e| lookup(e) }.join(', ')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def message
|
26
32
|
messages.join(', ').capitalize
|
27
33
|
end
|
28
|
-
alias :
|
34
|
+
alias :to_s :message
|
29
35
|
|
30
36
|
def messages
|
31
|
-
scrivener.errors.
|
37
|
+
scrivener.errors.each_with_object([]) do |error, collection|
|
32
38
|
att = error[0]
|
33
|
-
|
34
|
-
memo
|
39
|
+
collection.concat error[1].map {|e| error_string(att, e) }
|
35
40
|
end
|
36
41
|
end
|
37
42
|
|
38
43
|
def error_string(att, error)
|
39
|
-
att
|
40
|
-
|
44
|
+
att = att.to_s.tr('_', ' ')
|
45
|
+
[att, lookup(error)].join(' ')
|
46
|
+
end
|
41
47
|
|
42
|
-
|
48
|
+
def lookup(key)
|
49
|
+
MESSAGES.fetch(key, 'is invalid')
|
43
50
|
end
|
44
51
|
|
45
52
|
module Helpers
|
data/scrivener_errors.gemspec
CHANGED
@@ -11,6 +11,8 @@ class SignUp < Scrivener
|
|
11
11
|
assert_length :password, (8..99)
|
12
12
|
assert(password == password_confirmation,
|
13
13
|
[:password_confirmation, :not_confirmed])
|
14
|
+
|
15
|
+
assert_format :password, /^[\S]+/
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
@@ -55,6 +57,20 @@ scope do
|
|
55
57
|
ScrivenerErrors.new(filter).message
|
56
58
|
)
|
57
59
|
end
|
60
|
+
|
61
|
+
test "an error string for a single attribute" do
|
62
|
+
filter = SignUp.new(email: 'john',
|
63
|
+
password: ' x',
|
64
|
+
password_confirmation: 'y')
|
65
|
+
|
66
|
+
assert_equal(
|
67
|
+
"is not an email",
|
68
|
+
ScrivenerErrors.new(filter)[:email])
|
69
|
+
|
70
|
+
assert_equal(
|
71
|
+
"has invalid length, is invalid",
|
72
|
+
ScrivenerErrors.new(filter)[:password])
|
73
|
+
end
|
58
74
|
end
|
59
75
|
|
60
76
|
scope do
|