human_error 1.8.0 → 1.8.1
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/lib/human_error/persistable.rb +29 -22
- data/lib/human_error/version.rb +1 -1
- 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: c55554df9b5ca6be992b36fbca062fa8e6c4ea0e
|
4
|
+
data.tar.gz: 246c273b755e273e132e9fb8d65e27e909ab087e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f40e1a014d53632a840475003c64f96117f9e2cad20f412e8bd0844b48063c5efcc658ac949f0ac8281daa50aa29dd8a694f585fcb31e393370c590e83011469
|
7
|
+
data.tar.gz: a643c39007b66cc9629be8eebb2d27bd9388773b459802fde156c4b284f248dc44cecbe91b8c90c0857775b4d001eb2b3c9148d5377be71c56df885a1fc3fa78
|
@@ -1,5 +1,24 @@
|
|
1
1
|
class HumanError
|
2
2
|
module Persistable
|
3
|
+
module ClassMethods
|
4
|
+
def find(*ids)
|
5
|
+
super
|
6
|
+
rescue ActiveRecord::RecordNotFound => e
|
7
|
+
ids = case e.message
|
8
|
+
when /\ACouldn't find .* without an ID\z/
|
9
|
+
[]
|
10
|
+
when /\ACouldn't find .* with \'.*\'=(\d+)\z/
|
11
|
+
[$1]
|
12
|
+
when /\ACouldn't find all .* with \'.*\': ((?:\d+(?:, )?)+)\z/
|
13
|
+
$1.split(', ')
|
14
|
+
end
|
15
|
+
|
16
|
+
raise HumanError::Errors::ResourceNotFoundError.new(
|
17
|
+
resource_name: human_error_resource_name(self),
|
18
|
+
resource_id: ids)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
3
22
|
def save!(*args)
|
4
23
|
super
|
5
24
|
rescue ActiveRecord::InvalidForeignKey => e
|
@@ -9,43 +28,31 @@ module Persistable
|
|
9
28
|
[1..-1]
|
10
29
|
|
11
30
|
raise HumanError::Errors::AssociationError.new(
|
12
|
-
resource_name: human_error_resource_name,
|
31
|
+
resource_name: human_error_resource_name(self.class),
|
13
32
|
association_name: association_name,
|
14
33
|
association_id: association_id,
|
15
34
|
attributes: attributes)
|
16
35
|
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
|
17
36
|
raise HumanError::Errors::ResourcePersistenceError.new(
|
18
|
-
resource_name: human_error_resource_name,
|
37
|
+
resource_name: human_error_resource_name(self.class),
|
19
38
|
attributes: attributes,
|
20
39
|
errors: errors.full_messages)
|
21
40
|
end
|
22
41
|
|
23
|
-
def
|
24
|
-
super
|
25
|
-
rescue ActiveRecord::RecordNotFound => e
|
26
|
-
ids = case e.message
|
27
|
-
when /\ACouldn't find .* without an ID\z/
|
28
|
-
[]
|
29
|
-
when /\ACouldn't find .* with \'.*\'=(\d+)\z/
|
30
|
-
[$1]
|
31
|
-
when /\ACouldn't find all .* with \'.*\': ((?:\d+(?:, )?)+)\z/
|
32
|
-
$1.split(', ')
|
33
|
-
end
|
34
|
-
|
35
|
-
raise HumanError::Errors::ResourceNotFoundError.new(
|
36
|
-
resource_name: human_error_resource_name,
|
37
|
-
resource_id: ids)
|
38
|
-
end
|
39
|
-
|
40
|
-
def human_error_resource_name
|
42
|
+
def human_error_resource_name(klass)
|
41
43
|
last_part_of_class_name = /::(\w+)\z/
|
42
44
|
|
43
|
-
|
44
|
-
class.
|
45
|
+
klass.
|
45
46
|
name[last_part_of_class_name, 1].
|
46
47
|
underscore.
|
47
48
|
humanize.
|
48
49
|
downcase
|
49
50
|
end
|
51
|
+
|
52
|
+
module_function :human_error_resource_name
|
53
|
+
|
54
|
+
def self.included(base)
|
55
|
+
base.extend ClassMethods
|
56
|
+
end
|
50
57
|
end
|
51
58
|
end
|
data/lib/human_error/version.rb
CHANGED