class Exceptions::Model

Public Instance Methods

attribute() click to toggle source
# File lib/exceptions/model.rb, line 39
def attribute
        self.object.errors.first[0]
end
attribute_human() click to toggle source
# File lib/exceptions/model.rb, line 47
def attribute_human
        self.object.class.human_attribute_name(self.object.errors.first[0])
end
build_nested() click to toggle source
# 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() click to toggle source
# 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() click to toggle source

for model errors this method build a hash with all necessary information @return [String] json string

# File lib/exceptions/model.rb, line 5
def error
        self.is_nested? ? self.build_nested : self.build_normal
end
is_nested?() click to toggle source
# 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() click to toggle source

return the error message @return [String]

# File lib/exceptions/model.rb, line 53
def message 
        "#{self.object.errors.first[1]}"
end
model() click to toggle source

return what model is @return [String]

# File lib/exceptions/model.rb, line 35
def model
        self.object.class.name.demodulize.tableize.singularize.downcase
end
model_human() click to toggle source
# File lib/exceptions/model.rb, line 43
def model_human
        self.object.class.model_name.human.demodulize.tableize.singularize.downcase
end
nested_attr() click to toggle source
# File lib/exceptions/model.rb, line 79
def nested_attr
        self.object.errors.first[0].to_s.split(".").last
end
nested_attr_human() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/exceptions/model.rb, line 75
def nested_model_human
        self.nested_model.capitalize.constantize.model_name.human
end
status() click to toggle source
# File lib/exceptions/model.rb, line 57
def status
        422
end