Permalink
Browse files

Fixed AR deprecation warnings when using edge Rails [#80]

 * I18n.translate('active_record.error_messages') instead of ActiveRecord::Errors.default_error_messages
  • Loading branch information...
1 parent 5ccbee0 commit c60fa4be85af2c8273460c7fc0881847b4dbd171 @rmm5t rmm5t committed Sep 17, 2008
Showing with 29 additions and 22 deletions.
  1. +29 −22 lib/shoulda/active_record/macros.rb
@@ -1,6 +1,13 @@
module ThoughtBot # :nodoc:
module Shoulda # :nodoc:
module ActiveRecord # :nodoc:
+ DEFAULT_ERROR_MESSAGES =
+ if Object.const_defined?(:I18n)
+ I18n.translate('active_record.error_messages')
+ else
+ ::ActiveRecord::Errors.default_error_messages
+ end
+
# = Macro test helpers for your active record models
#
# These helpers will test most of the validations and associations for your ActiveRecord models.
@@ -36,14 +43,14 @@ def load_all_fixtures
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:blank]</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:blank]</tt>
#
# Example:
# should_require_attributes :name, :phone_number
#
def should_require_attributes(*attributes)
message = get_options!(attributes, :message)
- message ||= ::ActiveRecord::Errors.default_error_messages[:blank]
+ message ||= DEFAULT_ERROR_MESSAGES[:blank]
klass = model_class
attributes.each do |attribute|
@@ -58,7 +65,7 @@ def should_require_attributes(*attributes)
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:taken]</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:taken]</tt>
# * <tt>:scoped_to</tt> - field(s) to scope the uniqueness to.
#
# Examples:
@@ -70,7 +77,7 @@ def should_require_attributes(*attributes)
def should_require_unique_attributes(*attributes)
message, scope = get_options!(attributes, :message, :scoped_to)
scope = [*scope].compact
- message ||= ::ActiveRecord::Errors.default_error_messages[:taken]
+ message ||= DEFAULT_ERROR_MESSAGES[:taken]
klass = model_class
attributes.each do |attribute|
@@ -156,14 +163,14 @@ def should_have_readonly_attributes(*attributes)
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:invalid]</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:invalid]</tt>
#
# Example:
# should_not_allow_values_for :isbn, "bad 1", "bad 2"
#
def should_not_allow_values_for(attribute, *bad_values)
message = get_options!(bad_values, :message)
- message ||= ::ActiveRecord::Errors.default_error_messages[:invalid]
+ message ||= DEFAULT_ERROR_MESSAGES[:invalid]
klass = model_class
bad_values.each do |v|
should "not allow #{attribute} to be set to #{v.inspect}" do
@@ -199,17 +206,17 @@ def should_allow_values_for(attribute, *good_values)
#
# Options:
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:too_short] % range.first</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:too_short] % range.first</tt>
# * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:too_long] % range.last</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:too_long] % range.last</tt>
#
# Example:
# should_ensure_length_in_range :password, (6..20)
#
def should_ensure_length_in_range(attribute, range, opts = {})
short_message, long_message = get_options!([opts], :short_message, :long_message)
- short_message ||= ::ActiveRecord::Errors.default_error_messages[:too_short] % range.first
- long_message ||= ::ActiveRecord::Errors.default_error_messages[:too_long] % range.last
+ short_message ||= DEFAULT_ERROR_MESSAGES[:too_short] % range.first
+ long_message ||= DEFAULT_ERROR_MESSAGES[:too_long] % range.last
klass = model_class
min_length = range.first
@@ -251,14 +258,14 @@ def should_ensure_length_in_range(attribute, range, opts = {})
#
# Options:
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:too_short] % min_length</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:too_short] % min_length</tt>
#
# Example:
# should_ensure_length_at_least :name, 3
#
def should_ensure_length_at_least(attribute, min_length, opts = {})
short_message = get_options!([opts], :short_message)
- short_message ||= ::ActiveRecord::Errors.default_error_messages[:too_short] % min_length
+ short_message ||= DEFAULT_ERROR_MESSAGES[:too_short] % min_length
klass = model_class
@@ -282,14 +289,14 @@ def should_ensure_length_at_least(attribute, min_length, opts = {})
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:wrong_length] % length</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:wrong_length] % length</tt>
#
# Example:
# should_ensure_length_is :ssn, 9
#
def should_ensure_length_is(attribute, length, opts = {})
message = get_options!([opts], :message)
- message ||= ::ActiveRecord::Errors.default_error_messages[:wrong_length] % length
+ message ||= DEFAULT_ERROR_MESSAGES[:wrong_length] % length
klass = model_class
@@ -317,17 +324,17 @@ def should_ensure_length_is(attribute, length, opts = {})
#
# Options:
# * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:inclusion]</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:inclusion]</tt>
# * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:inclusion]</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:inclusion]</tt>
#
# Example:
# should_ensure_value_in_range :age, (0..100)
#
def should_ensure_value_in_range(attribute, range, opts = {})
low_message, high_message = get_options!([opts], :low_message, :high_message)
- low_message ||= ::ActiveRecord::Errors.default_error_messages[:inclusion]
- high_message ||= ::ActiveRecord::Errors.default_error_messages[:inclusion]
+ low_message ||= DEFAULT_ERROR_MESSAGES[:inclusion]
+ high_message ||= DEFAULT_ERROR_MESSAGES[:inclusion]
klass = model_class
min = range.first
@@ -362,14 +369,14 @@ def should_ensure_value_in_range(attribute, range, opts = {})
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:not_a_number]</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:not_a_number]</tt>
#
# Example:
# should_only_allow_numeric_values_for :age
#
def should_only_allow_numeric_values_for(*attributes)
message = get_options!(attributes, :message)
- message ||= ::ActiveRecord::Errors.default_error_messages[:not_a_number]
+ message ||= DEFAULT_ERROR_MESSAGES[:not_a_number]
klass = model_class
attributes.each do |attribute|
attribute = attribute.to_sym
@@ -605,14 +612,14 @@ def should_have_indices(*columns)
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:accepted]</tt>
+ # Regexp or string. Default = <tt>I18n.translate('active_record.error_messages')[:accepted]</tt>
#
# Example:
# should_require_acceptance_of :eula
#
def should_require_acceptance_of(*attributes)
message = get_options!(attributes, :message)
- message ||= ::ActiveRecord::Errors.default_error_messages[:accepted]
+ message ||= DEFAULT_ERROR_MESSAGES[:accepted]
klass = model_class
attributes.each do |attribute|

0 comments on commit c60fa4b

Please sign in to comment.