module Apes::Model

Some utility extensions to ActiveModel.

Public Instance Methods

additional_errors() click to toggle source

A list of manually managed errors for the model.

@return [ActiveModel::Errors] A list of manually managed errors for the model.

# File lib/apes/model.rb, line 69
def additional_errors
  @additional_errors ||= ActiveModel::Errors.new(self)
end
all_validation_errors() click to toggle source

A list of automatically and manually added errors for the model.

@return [ActiveModel::Errors] A list of automatically and manually added errors for the model.

# File lib/apes/model.rb, line 82
def all_validation_errors
  additional_errors.each do |field, error|
    errors.add(field, error)
  end

  errors.each do |field|
    errors[field].uniq!
  end

  errors
end
find_with_any(id) click to toggle source

Find a object by using the UUID, a handle or model specific definitions (defined using SECONDARY_KEY or SECONDARY_QUERY constants).

@param id [Object] The value to find. @return [Object] The first found model.

# File lib/apes/model.rb, line 33
def find_with_any(id)
  find_with_any!(id)
rescue ActiveRecord::RecordNotFound
  nil
end
find_with_any!(id) click to toggle source

Find a object by using the UUID, a handle or model specific definitions (defined using SECONDARY_KEY or SECONDARY_QUERY constants). Raise exception when nothing is found.

@param id [Object] The value to find. @return [Object] The first found model.

# File lib/apes/model.rb, line 17
def find_with_any!(id)
  if id =~ Validators::UuidValidator::VALID_REGEX
    find(id)
  elsif defined?(self::SECONDARY_KEY)
    find_by!(self::SECONDARY_KEY => id)
  elsif defined?(self::SECONDARY_QUERY)
    find_by!(self::SECONDARY_QUERY, {id: id})
  else
    find_by!(handle: id)
  end
end
run_validations!() click to toggle source

Perform validations on the model and makes sure manually added errors are included.

Calls superclass method
# File lib/apes/model.rb, line 74
def run_validations!
  errors.messages.merge!(additional_errors.messages)
  super
end