Class: Mutations::DefaultErrorMessageCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/mutations/errors.rb

Overview

Offers a non-localized, english only, non configurable way to get error messages. This probably isnt good enough for users as-is.

Constant Summary collapse

MESSAGES =
Hash.new("is invalid").tap do |h|
  h.merge!(
    # General
    :nils => "can't be nil",
    :required => "is required",

    # Datatypes
    :string => "isn't a string",
    :integer => "isn't an integer",
    :boolean => "isn't a boolean",
    :hash => "isn't a hash",
    :array => "isn't an array",
    :model => "isn't the right class",

    # Date
    :date => "date doesn't exist",
    :before => "isn't before given date",
    :after => "isn't after given date",

    # String
    :empty => "can't be blank",
    :max_length => "is too long",
    :min_length => "is too short",
    :matches => "isn't in the right format",
    :in => "isn't an option",

    # Array
    :class => "isn't the right class",

    # Integer
    :min => "is too small",
    :max => "is too big",

    # Model
    :new_records => "isn't a saved model"
  )
end

Instance Method Summary collapse

Instance Method Details

#message(key, error_symbol, options = {}) ⇒ Object

key: the name of the field, eg, :email. Could be nil if it’s an array element error_symbol: the validation symbol, eg, :matches or :required options:

:index -- index of error if it's in an array


48
49
50
51
52
53
54
# File 'lib/mutations/errors.rb', line 48

def message(key, error_symbol, options = {})
  if options[:index]
    "#{titleize(key || 'array')}[#{options[:index]}] #{MESSAGES[error_symbol]}"
  else
    "#{titleize(key)} #{MESSAGES[error_symbol]}"
  end
end

#titleize(key) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/mutations/errors.rb', line 56

def titleize(key)
  key = key.to_s.downcase
  if key == "id"
    "ID"
  elsif key.end_with?("_id")
    "#{key.titleize} ID"
  else
    key.titleize
  end
end