0
module ThoughtBot # :nodoc:
0
module Shoulda # :nodoc:
0
module ActiveRecord # :nodoc:
0
- DEFAULT_ERROR_MESSAGES =
0
- if Object.const_defined?(:I18n)
0
- I18n.translate('activerecord.errors.messages')
0
- ::ActiveRecord::Errors.default_error_messages
0
+ module MacroHelpers # :nodoc:
0
+ # Helper method that determines the default error message used by Active
0
+ # Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
0
+ # introduced I18n module used for localization.
0
+ # default_error_message(:blank)
0
+ # default_error_message(:too_short, :count => 5)
0
+ # default_error_message(:too_long, :count => 60)
0
+ def default_error_message(key, values = {})
0
+ if Object.const_defined?(:I18n) # Rails >= 2.2
0
+ I18n.translate("activerecord.errors.messages.#{key}", values)
0
+ ::ActiveRecord::Errors.default_error_messages[key] % values[:count]
0
# = Macro test helpers for your active record models
0
@@ -28,6 +38,8 @@ module ThoughtBot # :nodoc:
0
# For all of these helpers, the last parameter may be a hash of options.
0
# <b>DEPRECATED:</b> Use <tt>fixtures :all</tt> instead
0
# Loads all fixture files (<tt>test/fixtures/*.yml</tt>)
0
@@ -44,14 +56,14 @@ module ThoughtBot # :nodoc:
0
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:blank]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.blank')</tt>
0
# should_require_attributes :name, :phone_number
0
def should_require_attributes(*attributes)
0
message = get_options!(attributes, :message)
0
- message ||=
DEFAULT_ERROR_MESSAGES[:blank]0
+ message ||=
default_error_message(:blank)0
attributes.each do |attribute|
0
@@ -66,7 +78,7 @@ module ThoughtBot # :nodoc:
0
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:taken]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.taken')</tt>
0
# * <tt>:scoped_to</tt> - field(s) to scope the uniqueness to.
0
@@ -78,7 +90,7 @@ module ThoughtBot # :nodoc:
0
def should_require_unique_attributes(*attributes)
0
message, scope = get_options!(attributes, :message, :scoped_to)
0
scope = [*scope].compact
0
- message ||=
DEFAULT_ERROR_MESSAGES[:taken]0
+ message ||=
default_error_message(:taken)0
attributes.each do |attribute|
0
@@ -164,14 +176,14 @@ module ThoughtBot # :nodoc:
0
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:invalid]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.invalid')</tt>
0
# should_not_allow_values_for :isbn, "bad 1", "bad 2"
0
def should_not_allow_values_for(attribute, *bad_values)
0
message = get_options!(bad_values, :message)
0
- message ||=
DEFAULT_ERROR_MESSAGES[:invalid]0
+ message ||=
default_error_message(:invalid)0
should "not allow #{attribute} to be set to #{v.inspect}" do
0
@@ -207,17 +219,17 @@ module ThoughtBot # :nodoc:
0
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:too_short] % range.first</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.too_short') % range.first</tt>
0
# * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:too_long] % range.last</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.too_long') % range.last</tt>
0
# should_ensure_length_in_range :password, (6..20)
0
def should_ensure_length_in_range(attribute, range, opts = {})
0
short_message, long_message = get_options!([opts], :short_message, :long_message)
0
- short_message ||= DEFAULT_ERROR_MESSAGES[:too_short] % range.first
0
- long_message ||= DEFAULT_ERROR_MESSAGES[:too_long] % range.last
0
+ short_message ||= default_error_message(:too_short, :count => range.first)
0
+ long_message ||= default_error_message(:too_long, :count => range.last)
0
min_length = range.first
0
@@ -259,14 +271,14 @@ module ThoughtBot # :nodoc:
0
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:too_short] % min_length</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.too_short') % min_length</tt>
0
# should_ensure_length_at_least :name, 3
0
def should_ensure_length_at_least(attribute, min_length, opts = {})
0
short_message = get_options!([opts], :short_message)
0
- short_message ||=
DEFAULT_ERROR_MESSAGES[:too_short] % min_length0
+ short_message ||=
default_error_message(:too_short, :count => min_length)0
@@ -290,14 +302,14 @@ module ThoughtBot # :nodoc:
0
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:wrong_length] % length</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.wrong_length') % length</tt>
0
# should_ensure_length_is :ssn, 9
0
def should_ensure_length_is(attribute, length, opts = {})
0
message = get_options!([opts], :message)
0
- message ||=
DEFAULT_ERROR_MESSAGES[:wrong_length] % length0
+ message ||=
default_error_message(:wrong_length, :count => length)0
@@ -325,17 +337,17 @@ module ThoughtBot # :nodoc:
0
# * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:inclusion]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.inclusion')</tt>
0
# * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:inclusion]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.inclusion')</tt>
0
# should_ensure_value_in_range :age, (0..100)
0
def should_ensure_value_in_range(attribute, range, opts = {})
0
low_message, high_message = get_options!([opts], :low_message, :high_message)
0
- low_message ||= DEFAULT_ERROR_MESSAGES[:inclusion]
0
- high_message ||= DEFAULT_ERROR_MESSAGES[:inclusion]
0
+ low_message ||= default_error_message(:inclusion)
0
+ high_message ||= default_error_message(:inclusion)
0
@@ -370,14 +382,14 @@ module ThoughtBot # :nodoc:
0
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:not_a_number]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.not_a_number')</tt>
0
# should_only_allow_numeric_values_for :age
0
def should_only_allow_numeric_values_for(*attributes)
0
message = get_options!(attributes, :message)
0
- message ||=
DEFAULT_ERROR_MESSAGES[:not_a_number]0
+ message ||=
default_error_message(:not_a_number)0
attributes.each do |attribute|
0
attribute = attribute.to_sym
0
@@ -624,14 +636,14 @@ module ThoughtBot # :nodoc:
0
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
')[:accepted]</tt>
0
+ # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages
.accepted')</tt>
0
# should_require_acceptance_of :eula
0
def should_require_acceptance_of(*attributes)
0
message = get_options!(attributes, :message)
0
- message ||=
DEFAULT_ERROR_MESSAGES[:accepted]0
+ message ||=
default_error_message(:accepted)0
attributes.each do |attribute|
0
@@ -697,7 +709,6 @@ module ThoughtBot # :nodoc:
Comments
No one has commented yet.