Exception: Exceptions::Model
- Inherits:
-
Base
- Object
- StandardError
- Base
- Exceptions::Model
show all
- Defined in:
- lib/exceptions/model.rb
Instance Attribute Summary
Attributes inherited from Base
#object, #type
Instance Method Summary
collapse
Methods inherited from Base
build, #initialize, #model?, #simple?
Instance Method Details
#attribute ⇒ Object
39
40
41
|
# File 'lib/exceptions/model.rb', line 39
def attribute
self.object.errors.first[0]
end
|
#attribute_human ⇒ Object
47
48
49
|
# File 'lib/exceptions/model.rb', line 47
def attribute_human
self.object.class.human_attribute_name(self.object.errors.first[0])
end
|
#build_nested ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/exceptions/model.rb', line 9
def build_nested
{
error: {
model: self.nested_model.camelcase,
field: "#{self.nested_model}[#{self.nested_attr}]",
attribute: self.nested_attr,
message: self.message,
full_message: "#{self.nested_attr_human} #{self.message}"
}
}
end
|
#build_normal ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/exceptions/model.rb', line 21
def build_normal
{
error: {
model: self.model.camelcase,
field: "#{self.model}[#{self.attribute}]",
attribute: self.attribute,
message: self.message,
full_message: "#{self.attribute_human} #{self.message}"
}
}
end
|
#error ⇒ String
for model errors this method build a hash with all necessary information
5
6
7
|
# File 'lib/exceptions/model.rb', line 5
def error
self.is_nested? ? self.build_nested : self.build_normal
end
|
#is_nested? ⇒ Boolean
61
62
63
64
65
66
67
68
69
|
# File 'lib/exceptions/model.rb', line 61
def is_nested?
attribute = self.object.errors.first[0]
if self.object.errors.first[0].to_s.split(".").size > 1
self.object.respond_to?(attribute) ? false : true
else
false
end
end
|
#message ⇒ String
53
54
55
|
# File 'lib/exceptions/model.rb', line 53
def message
"#{self.object.errors.first[1]}"
end
|
#model ⇒ String
35
36
37
|
# File 'lib/exceptions/model.rb', line 35
def model
self.object.class.name.demodulize.tableize.singularize.downcase
end
|
#model_human ⇒ Object
43
44
45
|
# File 'lib/exceptions/model.rb', line 43
def model_human
self.object.class.model_name.human.demodulize.tableize.singularize.downcase
end
|
#nested_attr ⇒ Object
79
80
81
|
# File 'lib/exceptions/model.rb', line 79
def nested_attr
self.object.errors.first[0].to_s.split(".").last
end
|
#nested_attr_human ⇒ Object
83
84
85
|
# File 'lib/exceptions/model.rb', line 83
def nested_attr_human
self.nested_model.capitalize.constantize.human_attribute_name(self.nested_attr)
end
|
#nested_model ⇒ Object
71
72
73
|
# File 'lib/exceptions/model.rb', line 71
def nested_model
self.object.errors.first[0].to_s.split(".").first.singularize.downcase
end
|
#nested_model_human ⇒ Object
75
76
77
|
# File 'lib/exceptions/model.rb', line 75
def nested_model_human
self.nested_model.capitalize.constantize.model_name.human
end
|
#status ⇒ Object
57
58
59
|
# File 'lib/exceptions/model.rb', line 57
def status
422
end
|