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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 20dc7cae02ae70c040dbe206dd966b61b88a1d6a
4
- data.tar.gz: 211adc8a4290324f7259ae542148d9f690e6ebd9
3
+ metadata.gz: 016d5a3108de8581c6b9cd88e3c812a43d3a6f08
4
+ data.tar.gz: 3085e7f26acd7ba51e55bdb43bcaa6a40a06de53
5
5
  SHA512:
6
- metadata.gz: 242d4ad7beafb5fbc4be88de43604f7e1e3e3b2b2eecc159f4af4d791b1a791fd3a93d405c9864de79b6528d0adeba64d123e762cb294fbad5b4aad3ab87d161
7
- data.tar.gz: 7d95e0fe4d9ea8f0c374fe71c8e7a0200c75f11efe684f07c9faf733c78ec3341a02a47cc7da85ba9f3c191f77f39318b63344735705fb374fc93ceebd278591
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 errors message for use in failure notices or
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
 
@@ -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
- }.freeze
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 to_s
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 :message :to_s
34
+ alias :to_s :message
29
35
 
30
36
  def messages
31
- scrivener.errors.inject([]) do |memo, error|
37
+ scrivener.errors.each_with_object([]) do |error, collection|
32
38
  att = error[0]
33
- memo.concat error[1].map {|e| error_string(att, e) }
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 = att.to_s.tr('_', ' ')
40
- failure = MESSAGES.fetch(error, 'is invalid')
44
+ att = att.to_s.tr('_', ' ')
45
+ [att, lookup(error)].join(' ')
46
+ end
41
47
 
42
- [att, failure].join(' ')
48
+ def lookup(key)
49
+ MESSAGES.fetch(key, 'is invalid')
43
50
  end
44
51
 
45
52
  module Helpers
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "scrivener_errors"
3
- s.version = "0.0.4"
3
+ s.version = "0.0.5"
4
4
  s.summary = "Basic error messages for Scrivener filters"
5
5
  s.description = s.summary
6
6
  s.authors = ["Brendon Murphy"]
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrivener_errors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brendon Murphy