Exception: Mongoid::Errors::MongoidError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/mongoid/errors/mongoid_error.rb

Overview

Default parent Mongoid error for all custom errors. This handles the base key for the translations and provides the convenience method for translating the messages.

Constant Summary collapse

BASE_KEY =
"mongoid.errors.messages"

Instance Method Summary collapse

Instance Method Details

#compose_message(key, attributes) ⇒ String

Compose the message.

Examples:

Create the message

error.compose_message

Returns:

  • (String)

    The composed message.

Since:

  • 3.0.0



20
21
22
23
24
# File 'lib/mongoid/errors/mongoid_error.rb', line 20

def compose_message(key, attributes)
  "\nProblem:\n  #{problem(key, attributes)}"+
  "\nSummary:\n  #{summary(key, attributes)}"+
  "\nResolution:\n  #{resolution(key, attributes)}"
end

#problem(key, attributes) ⇒ String

Create the problem.

Examples:

Create the problem.

error.problem

Returns:

  • (String)

    The problem.

Since:

  • 3.0.0



48
49
50
# File 'lib/mongoid/errors/mongoid_error.rb', line 48

def problem(key, attributes)
  translate("#{key}.message", attributes)
end

#resolution(key, attributes) ⇒ String

Create the resolution.

Examples:

Create the resolution.

error.resolution

Returns:

  • (String)

    The resolution.

Since:

  • 3.0.0



72
73
74
# File 'lib/mongoid/errors/mongoid_error.rb', line 72

def resolution(key, attributes)
  translate("#{key}.resolution", attributes)
end

#summary(key, attributes) ⇒ String

Create the summary.

Examples:

Create the summary.

error.summary

Returns:

  • (String)

    The summary.

Since:

  • 3.0.0



60
61
62
# File 'lib/mongoid/errors/mongoid_error.rb', line 60

def summary(key, attributes)
  translate("#{key}.summary", attributes)
end

#translate(key, options) ⇒ String

Given the key of the specific error and the options hash, translate the message.

Examples:

Translate the message.

error.translate("errors", :key => value)

Parameters:

  • key (String)

    The key of the error in the locales.

  • options (Hash)

    The objects to pass to create the message.

Returns:

  • (String)

    A localized error message string.



36
37
38
# File 'lib/mongoid/errors/mongoid_error.rb', line 36

def translate(key, options)
  ::I18n.translate("#{BASE_KEY}.#{key}", { locale: :en }.merge(options))
end