module CustomErrorMessage::InstanceMethods

Private Instance Methods

add_with_procs(attribute, message = nil, options = {}) click to toggle source
# File lib/custom_error_message.rb, line 29
def add_with_procs(attribute, message = nil, options = {})
  if options[:default].respond_to? :to_proc
    options[:default] = "^#{options[:default].to_proc.call(@base)}"
  end

  add_without_procs(attribute, message, options)
end
full_messages_with_tilde() click to toggle source

Redefine the full_messages method:

Returns all the full error messages in an array. 'Base' messages are handled as usual.
Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
in which case the attribute name is omitted.
E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
# File lib/custom_error_message.rb, line 18
def full_messages_with_tilde
  full_messages = full_messages_without_tilde
  full_messages.map do |message|
    if starts_with_humanized_column_followed_by_circumflex? message
      message.gsub(/^.+\^/, '')
    else
      message
    end
  end
end
starts_with_humanized_column_followed_by_circumflex?(message) click to toggle source
# File lib/custom_error_message.rb, line 37
def starts_with_humanized_column_followed_by_circumflex?(message)
  keys.any? do |column|
    humanized = @base.class.human_attribute_name column.to_s.gsub('.','_')
    message.match(/^#{humanized} \^/)
  end
end