lhs 6.5.0 → 6.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/lib/lhs/errors.rb +11 -5
- data/lib/lhs/item.rb +5 -0
- data/lib/lhs/version.rb +1 -1
- data/spec/item/add_error_spec.rb +19 -0
- data/spec/record/create_spec.rb +2 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03af8ee3ccf17c8a75304d5c3f3ce6e395ef51dc
|
4
|
+
data.tar.gz: 60292bdfe213362cc375f32ac399bc26945d28d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47a6488f9d1a9dcd99b90e4cc5ef81bc16fdeb1559cf589ecd36fdacdf0d00c173d20f62743bc28b7e114ed64463a694e0709532a4c08d7fca01f8ae41124774
|
7
|
+
data.tar.gz: 88559585cc42b9f679038c2b8cf9005ce2d7ae3ffe2ab9997028d77105d37153f78647952555a0dfd7b4cb476c4a29a74d49da07b800c746e41974a7c19d38ad
|
data/README.md
CHANGED
@@ -534,6 +534,13 @@ class User < LHS::Record
|
|
534
534
|
end
|
535
535
|
```
|
536
536
|
|
537
|
+
## Custom validation errors
|
538
|
+
|
539
|
+
In case you want to add custom validation errors to an instance of LHS::Record:
|
540
|
+
|
541
|
+
```ruby
|
542
|
+
user.errors.add(:name, 'The name you provided is not valid.')
|
543
|
+
```
|
537
544
|
|
538
545
|
## Pagination
|
539
546
|
|
data/lib/lhs/errors.rb
CHANGED
@@ -4,10 +4,10 @@ class LHS::Errors
|
|
4
4
|
|
5
5
|
attr_reader :messages, :message, :raw
|
6
6
|
|
7
|
-
def initialize(response)
|
7
|
+
def initialize(response = nil)
|
8
8
|
@messages = messages_from_response(response)
|
9
9
|
@message = message_from_response(response)
|
10
|
-
@raw = response.body
|
10
|
+
@raw = response.body if response
|
11
11
|
rescue JSON::ParserError # rubocop:disable Lint/HandleExceptions
|
12
12
|
end
|
13
13
|
|
@@ -17,6 +17,11 @@ class LHS::Errors
|
|
17
17
|
alias has_key? include?
|
18
18
|
alias key? include?
|
19
19
|
|
20
|
+
def add(attribute, message = :invalid, _options = {})
|
21
|
+
self[attribute]
|
22
|
+
messages[attribute] << message
|
23
|
+
end
|
24
|
+
|
20
25
|
def get(key)
|
21
26
|
messages[key]
|
22
27
|
end
|
@@ -81,13 +86,14 @@ class LHS::Errors
|
|
81
86
|
messages
|
82
87
|
end
|
83
88
|
|
84
|
-
def messages_from_response(response)
|
85
|
-
return {} if !response.body.is_a?(String) || response.body.length.zero?
|
89
|
+
def messages_from_response(response = nil)
|
90
|
+
return {} if !response || !response.body.is_a?(String) || response.body.length.zero?
|
86
91
|
json = JSON.parse(response.body)
|
87
92
|
parse_messages(json)
|
88
93
|
end
|
89
94
|
|
90
|
-
def message_from_response(response)
|
95
|
+
def message_from_response(response = nil)
|
96
|
+
return unless response
|
91
97
|
json = JSON.parse(response.body)
|
92
98
|
json['message']
|
93
99
|
end
|
data/lib/lhs/item.rb
CHANGED
data/lib/lhs/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
describe LHS::Item do
|
4
|
+
before(:each) do
|
5
|
+
class Record < LHS::Record
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
subject { Record.new }
|
10
|
+
|
11
|
+
context 'add error' do
|
12
|
+
it 'allows to add validation errors to the instance itself' do
|
13
|
+
subject.errors.add(:name, 'This date is invalid')
|
14
|
+
expect(
|
15
|
+
subject.errors.first
|
16
|
+
).to eq [:name, 'This date is invalid']
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/record/create_spec.rb
CHANGED
@@ -26,7 +26,8 @@ describe LHS::Record do
|
|
26
26
|
record = Feedback.create(object)
|
27
27
|
expect(record).to be_kind_of Feedback
|
28
28
|
expect(record.recommended).to eq true
|
29
|
-
expect(record.errors).to eq
|
29
|
+
expect(record.errors.messages).to eq({})
|
30
|
+
expect(record.errors.message).to eq nil
|
30
31
|
end
|
31
32
|
|
32
33
|
it 'uses proper endpoint when creating data' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.
|
4
|
+
version: 6.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/local-ch/lhs/graphs/contributors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lhc
|
@@ -273,6 +273,7 @@ files:
|
|
273
273
|
- spec/dummy/public/favicon.ico
|
274
274
|
- spec/endpoint/for_url_spec.rb
|
275
275
|
- spec/item/accessors_spec.rb
|
276
|
+
- spec/item/add_error_spec.rb
|
276
277
|
- spec/item/delegate_spec.rb
|
277
278
|
- spec/item/destroy_spec.rb
|
278
279
|
- spec/item/errors_spec.rb
|
@@ -420,6 +421,7 @@ test_files:
|
|
420
421
|
- spec/dummy/public/favicon.ico
|
421
422
|
- spec/endpoint/for_url_spec.rb
|
422
423
|
- spec/item/accessors_spec.rb
|
424
|
+
- spec/item/add_error_spec.rb
|
423
425
|
- spec/item/delegate_spec.rb
|
424
426
|
- spec/item/destroy_spec.rb
|
425
427
|
- spec/item/errors_spec.rb
|