Exception: Exceptions::Model

Inherits:
Base
  • Object
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?

Constructor Details

This class inherits a constructor from Exceptions::Base

Instance Method Details

#attributeObject



39
40
41
# File 'lib/exceptions/model.rb', line 39

def attribute
	self.object.errors.first[0]
end

#attribute_humanObject



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_nestedObject



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_normalObject



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

#errorString

for model errors this method build a hash with all necessary information

Returns:

  • (String)

    json string



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

Returns:

  • (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

#messageString

return the error message

Returns:

  • (String)


53
54
55
# File 'lib/exceptions/model.rb', line 53

def message 
	"#{self.object.errors.first[1]}"
end

#modelString

return what model is

Returns:

  • (String)


35
36
37
# File 'lib/exceptions/model.rb', line 35

def model
	self.object.class.name.demodulize.tableize.singularize.downcase
end

#model_humanObject



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_attrObject



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_humanObject



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_modelObject



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_humanObject



75
76
77
# File 'lib/exceptions/model.rb', line 75

def nested_model_human
	self.nested_model.capitalize.constantize.model_name.human
end

#statusObject



57
58
59
# File 'lib/exceptions/model.rb', line 57

def status
	422
end