diff --git a/init.rb b/init.rb
index 3161feb..6dbfa4c 100644
--- a/init.rb
+++ b/init.rb
@@ -1 +1 @@
-require 'shoulda/rails'
+require File.join(File.dirname(__FILE__), 'rails', 'init')
diff --git a/lib/shoulda/active_record/assertions.rb b/lib/shoulda/active_record/assertions.rb
index b485844..1cdc43b 100644
--- a/lib/shoulda/active_record/assertions.rb
+++ b/lib/shoulda/active_record/assertions.rb
@@ -57,7 +57,7 @@ def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid
# @product = Product.new(:tangible => true)
# assert_bad_value(Product, :price, "0")
def assert_bad_value(object_or_klass, attribute, value,
- error_message_to_expect = DEFAULT_ERROR_MESSAGES[:invalid])
+ error_message_to_expect = self.class.default_error_message(:invalid))
object = get_instance_of(object_or_klass)
object.send("#{attribute}=", value)
assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
diff --git a/lib/shoulda/active_record/macros.rb b/lib/shoulda/active_record/macros.rb
index 3ccf7cb..81e0718 100644
--- a/lib/shoulda/active_record/macros.rb
+++ b/lib/shoulda/active_record/macros.rb
@@ -1,12 +1,22 @@
module ThoughtBot # :nodoc:
module Shoulda # :nodoc:
module ActiveRecord # :nodoc:
- DEFAULT_ERROR_MESSAGES =
- if Object.const_defined?(:I18n)
- I18n.translate('activerecord.errors.messages')
- else
- ::ActiveRecord::Errors.default_error_messages
+ module MacroHelpers # :nodoc:
+ # Helper method that determines the default error message used by Active
+ # Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
+ # introduced I18n module used for localization.
+ #
+ # default_error_message(:blank)
+ # default_error_message(:too_short, :count => 5)
+ # default_error_message(:too_long, :count => 60)
+ def default_error_message(key, values = {})
+ if Object.const_defined?(:I18n) # Rails >= 2.2
+ I18n.translate("activerecord.errors.messages.#{key}", values)
+ else # Rails <= 2.1.x
+ ::ActiveRecord::Errors.default_error_messages[key] % values[:count]
+ end
end
+ end
# = Macro test helpers for your active record models
#
@@ -28,6 +38,8 @@ module ActiveRecord # :nodoc:
# For all of these helpers, the last parameter may be a hash of options.
#
module Macros
+ include MacroHelpers
+
# DEPRECATED: Use fixtures :all instead
#
# Loads all fixture files (test/fixtures/*.yml)
@@ -44,14 +56,14 @@ def load_all_fixtures
#
# Options:
# * :message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:blank]
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.blank')
#
# Example:
# should_require_attributes :name, :phone_number
#
def should_require_attributes(*attributes)
message = get_options!(attributes, :message)
- message ||= DEFAULT_ERROR_MESSAGES[:blank]
+ message ||= default_error_message(:blank)
klass = model_class
attributes.each do |attribute|
@@ -66,7 +78,7 @@ def should_require_attributes(*attributes)
#
# Options:
# * :message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:taken]
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.taken')
# * :scoped_to - field(s) to scope the uniqueness to.
#
# Examples:
@@ -78,7 +90,7 @@ def should_require_attributes(*attributes)
def should_require_unique_attributes(*attributes)
message, scope = get_options!(attributes, :message, :scoped_to)
scope = [*scope].compact
- message ||= DEFAULT_ERROR_MESSAGES[:taken]
+ message ||= default_error_message(:taken)
klass = model_class
attributes.each do |attribute|
@@ -164,14 +176,14 @@ def should_have_readonly_attributes(*attributes)
#
# Options:
# * :message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:invalid]
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.invalid')
#
# 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 ||= DEFAULT_ERROR_MESSAGES[:invalid]
+ message ||= default_error_message(:invalid)
klass = model_class
bad_values.each do |v|
should "not allow #{attribute} to be set to #{v.inspect}" do
@@ -207,17 +219,17 @@ def should_allow_values_for(attribute, *good_values)
#
# Options:
# * :short_message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:too_short] % range.first
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.too_short') % range.first
# * :long_message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:too_long] % range.last
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.too_long') % range.last
#
# 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 ||= DEFAULT_ERROR_MESSAGES[:too_short] % range.first
- long_message ||= DEFAULT_ERROR_MESSAGES[:too_long] % range.last
+ short_message ||= default_error_message(:too_short, :count => range.first)
+ long_message ||= default_error_message(:too_long, :count => range.last)
klass = model_class
min_length = range.first
@@ -259,14 +271,14 @@ def should_ensure_length_in_range(attribute, range, opts = {})
#
# Options:
# * :short_message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:too_short] % min_length
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.too_short') % min_length
#
# 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 ||= DEFAULT_ERROR_MESSAGES[:too_short] % min_length
+ short_message ||= default_error_message(:too_short, :count => min_length)
klass = model_class
@@ -290,14 +302,14 @@ def should_ensure_length_at_least(attribute, min_length, opts = {})
#
# Options:
# * :message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:wrong_length] % length
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.wrong_length') % length
#
# Example:
# should_ensure_length_is :ssn, 9
#
def should_ensure_length_is(attribute, length, opts = {})
message = get_options!([opts], :message)
- message ||= DEFAULT_ERROR_MESSAGES[:wrong_length] % length
+ message ||= default_error_message(:wrong_length, :count => length)
klass = model_class
@@ -325,17 +337,17 @@ def should_ensure_length_is(attribute, length, opts = {})
#
# Options:
# * :low_message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:inclusion]
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.inclusion')
# * :high_message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:inclusion]
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.inclusion')
#
# 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 ||= DEFAULT_ERROR_MESSAGES[:inclusion]
- high_message ||= DEFAULT_ERROR_MESSAGES[:inclusion]
+ low_message ||= default_error_message(:inclusion)
+ high_message ||= default_error_message(:inclusion)
klass = model_class
min = range.first
@@ -370,14 +382,14 @@ def should_ensure_value_in_range(attribute, range, opts = {})
#
# Options:
# * :message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:not_a_number]
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.not_a_number')
#
# Example:
# should_only_allow_numeric_values_for :age
#
def should_only_allow_numeric_values_for(*attributes)
message = get_options!(attributes, :message)
- message ||= DEFAULT_ERROR_MESSAGES[:not_a_number]
+ message ||= default_error_message(:not_a_number)
klass = model_class
attributes.each do |attribute|
attribute = attribute.to_sym
@@ -624,14 +636,14 @@ def should_have_indices(*columns)
#
# Options:
# * :message - value the test expects to find in errors.on(:attribute).
- # Regexp or string. Default = I18n.translate('activerecord.errors.messages')[:accepted]
+ # Regexp or string. Default = I18n.translate('activerecord.errors.messages.accepted')
#
# Example:
# should_require_acceptance_of :eula
#
def should_require_acceptance_of(*attributes)
message = get_options!(attributes, :message)
- message ||= DEFAULT_ERROR_MESSAGES[:accepted]
+ message ||= default_error_message(:accepted)
klass = model_class
attributes.each do |attribute|
@@ -697,7 +709,6 @@ def should_have_named_scope(scope_call, *args)
end
end
end
-
end
end
end
diff --git a/lib/shoulda/controller/macros.rb b/lib/shoulda/controller/macros.rb
index ae07557..7148ee9 100644
--- a/lib/shoulda/controller/macros.rb
+++ b/lib/shoulda/controller/macros.rb
@@ -253,7 +253,8 @@ def should_render_without_layout
# Example:
#
# should_redirect_to '"/"'
- # should_redirect_to "users_url(@user)"
+ # should_redirect_to "user_url(@user)"
+ # should_redirect_to "users_url"
def should_redirect_to(url)
should "redirect to #{url.inspect}" do
instantiate_variables_from_assigns do
diff --git a/rails/init.rb b/rails/init.rb
new file mode 100644
index 0000000..3161feb
--- /dev/null
+++ b/rails/init.rb
@@ -0,0 +1 @@
+require 'shoulda/rails'
diff --git a/shoulda.gemspec b/shoulda.gemspec
index 058cb2b..221509d 100644
--- a/shoulda.gemspec
+++ b/shoulda.gemspec
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.email = %q{tsaleh@thoughtbot.com}
s.executables = ["convert_to_should_syntax"]
s.extra_rdoc_files = ["README.rdoc", "CONTRIBUTION_GUIDELINES.rdoc"]
- s.files = ["CONTRIBUTION_GUIDELINES.rdoc", "MIT-LICENSE", "Rakefile", "README.rdoc", "bin/convert_to_should_syntax", "lib/shoulda", "lib/shoulda/action_mailer", "lib/shoulda/action_mailer/assertions.rb", "lib/shoulda/action_mailer.rb", "lib/shoulda/active_record", "lib/shoulda/active_record/assertions.rb", "lib/shoulda/active_record/macros.rb", "lib/shoulda/active_record.rb", "lib/shoulda/assertions.rb", "lib/shoulda/context.rb", "lib/shoulda/controller", "lib/shoulda/controller/formats", "lib/shoulda/controller/formats/html.rb", "lib/shoulda/controller/formats/xml.rb", "lib/shoulda/controller/helpers.rb", "lib/shoulda/controller/macros.rb", "lib/shoulda/controller/resource_options.rb", "lib/shoulda/controller.rb", "lib/shoulda/helpers.rb", "lib/shoulda/macros.rb", "lib/shoulda/private_helpers.rb", "lib/shoulda/proc_extensions.rb", "lib/shoulda/rails.rb", "lib/shoulda/tasks", "lib/shoulda/tasks/list_tests.rake", "lib/shoulda/tasks/yaml_to_shoulda.rake", "lib/shoulda/tasks.rb", "lib/shoulda.rb", "test/fail_macros.rb", "test/fixtures", "test/fixtures/addresses.yml", "test/fixtures/friendships.yml", "test/fixtures/posts.yml", "test/fixtures/products.yml", "test/fixtures/taggings.yml", "test/fixtures/tags.yml", "test/fixtures/users.yml", "test/functional", "test/functional/posts_controller_test.rb", "test/functional/users_controller_test.rb", "test/other", "test/other/context_test.rb", "test/other/convert_to_should_syntax_test.rb", "test/other/helpers_test.rb", "test/other/private_helpers_test.rb", "test/other/should_test.rb", "test/rails_root", "test/rails_root/app", "test/rails_root/app/controllers", "test/rails_root/app/controllers/application.rb", "test/rails_root/app/controllers/posts_controller.rb", "test/rails_root/app/controllers/users_controller.rb", "test/rails_root/app/helpers", "test/rails_root/app/helpers/application_helper.rb", "test/rails_root/app/helpers/posts_helper.rb", "test/rails_root/app/helpers/users_helper.rb", "test/rails_root/app/models", "test/rails_root/app/models/address.rb", "test/rails_root/app/models/dog.rb", "test/rails_root/app/models/flea.rb", "test/rails_root/app/models/friendship.rb", "test/rails_root/app/models/post.rb", "test/rails_root/app/models/product.rb", "test/rails_root/app/models/tag.rb", "test/rails_root/app/models/tagging.rb", "test/rails_root/app/models/user.rb", "test/rails_root/app/views", "test/rails_root/app/views/layouts", "test/rails_root/app/views/layouts/posts.rhtml", "test/rails_root/app/views/layouts/users.rhtml", "test/rails_root/app/views/layouts/wide.html.erb", "test/rails_root/app/views/posts", "test/rails_root/app/views/posts/edit.rhtml", "test/rails_root/app/views/posts/index.rhtml", "test/rails_root/app/views/posts/new.rhtml", "test/rails_root/app/views/posts/show.rhtml", "test/rails_root/app/views/users", "test/rails_root/app/views/users/edit.rhtml", "test/rails_root/app/views/users/index.rhtml", "test/rails_root/app/views/users/new.rhtml", "test/rails_root/app/views/users/show.rhtml", "test/rails_root/config", "test/rails_root/config/boot.rb", "test/rails_root/config/database.yml", "test/rails_root/config/environment.rb", "test/rails_root/config/environments", "test/rails_root/config/environments/sqlite3.rb", "test/rails_root/config/initializers", "test/rails_root/config/initializers/new_rails_defaults.rb", "test/rails_root/config/initializers/shoulda.rb", "test/rails_root/config/routes.rb", "test/rails_root/db", "test/rails_root/db/migrate", "test/rails_root/db/migrate/001_create_users.rb", "test/rails_root/db/migrate/002_create_posts.rb", "test/rails_root/db/migrate/003_create_taggings.rb", "test/rails_root/db/migrate/004_create_tags.rb", "test/rails_root/db/migrate/005_create_dogs.rb", "test/rails_root/db/migrate/006_create_addresses.rb", "test/rails_root/db/migrate/007_create_fleas.rb", "test/rails_root/db/migrate/008_create_dogs_fleas.rb", "test/rails_root/db/migrate/009_create_products.rb", "test/rails_root/db/migrate/010_create_friendships.rb", "test/rails_root/db/schema.rb", "test/rails_root/log", "test/rails_root/log/sqlite3.log", "test/rails_root/public", "test/rails_root/public/404.html", "test/rails_root/public/422.html", "test/rails_root/public/500.html", "test/rails_root/script", "test/rails_root/script/console", "test/rails_root/script/generate", "test/rails_root/vendor", "test/rails_root/vendor/plugins", "test/README", "test/test_helper.rb", "test/unit", "test/unit/address_test.rb", "test/unit/dog_test.rb", "test/unit/flea_test.rb", "test/unit/friendship_test.rb", "test/unit/post_test.rb", "test/unit/product_test.rb", "test/unit/tag_test.rb", "test/unit/tagging_test.rb", "test/unit/user_test.rb"]
+ s.files = ["CONTRIBUTION_GUIDELINES.rdoc", "MIT-LICENSE", "Rakefile", "README.rdoc", "bin/convert_to_should_syntax", "lib/shoulda", "lib/shoulda/action_mailer", "lib/shoulda/action_mailer/assertions.rb", "lib/shoulda/action_mailer.rb", "lib/shoulda/active_record", "lib/shoulda/active_record/assertions.rb", "lib/shoulda/active_record/macros.rb", "lib/shoulda/active_record.rb", "lib/shoulda/assertions.rb", "lib/shoulda/context.rb", "lib/shoulda/controller", "lib/shoulda/controller/formats", "lib/shoulda/controller/formats/html.rb", "lib/shoulda/controller/formats/xml.rb", "lib/shoulda/controller/helpers.rb", "lib/shoulda/controller/macros.rb", "lib/shoulda/controller/resource_options.rb", "lib/shoulda/controller.rb", "lib/shoulda/helpers.rb", "lib/shoulda/macros.rb", "lib/shoulda/private_helpers.rb", "lib/shoulda/proc_extensions.rb", "lib/shoulda/rails.rb", "lib/shoulda/tasks", "lib/shoulda/tasks/list_tests.rake", "lib/shoulda/tasks/yaml_to_shoulda.rake", "lib/shoulda/tasks.rb", "lib/shoulda.rb", "rails", "rails/init.rb", "test/fail_macros.rb", "test/fixtures", "test/fixtures/addresses.yml", "test/fixtures/friendships.yml", "test/fixtures/posts.yml", "test/fixtures/products.yml", "test/fixtures/taggings.yml", "test/fixtures/tags.yml", "test/fixtures/users.yml", "test/functional", "test/functional/posts_controller_test.rb", "test/functional/users_controller_test.rb", "test/other", "test/other/context_test.rb", "test/other/convert_to_should_syntax_test.rb", "test/other/helpers_test.rb", "test/other/private_helpers_test.rb", "test/other/should_test.rb", "test/rails_root", "test/rails_root/app", "test/rails_root/app/controllers", "test/rails_root/app/controllers/application.rb", "test/rails_root/app/controllers/posts_controller.rb", "test/rails_root/app/controllers/users_controller.rb", "test/rails_root/app/helpers", "test/rails_root/app/helpers/application_helper.rb", "test/rails_root/app/helpers/posts_helper.rb", "test/rails_root/app/helpers/users_helper.rb", "test/rails_root/app/models", "test/rails_root/app/models/address.rb", "test/rails_root/app/models/dog.rb", "test/rails_root/app/models/flea.rb", "test/rails_root/app/models/friendship.rb", "test/rails_root/app/models/post.rb", "test/rails_root/app/models/product.rb", "test/rails_root/app/models/tag.rb", "test/rails_root/app/models/tagging.rb", "test/rails_root/app/models/user.rb", "test/rails_root/app/views", "test/rails_root/app/views/layouts", "test/rails_root/app/views/layouts/posts.rhtml", "test/rails_root/app/views/layouts/users.rhtml", "test/rails_root/app/views/layouts/wide.html.erb", "test/rails_root/app/views/posts", "test/rails_root/app/views/posts/edit.rhtml", "test/rails_root/app/views/posts/index.rhtml", "test/rails_root/app/views/posts/new.rhtml", "test/rails_root/app/views/posts/show.rhtml", "test/rails_root/app/views/users", "test/rails_root/app/views/users/edit.rhtml", "test/rails_root/app/views/users/index.rhtml", "test/rails_root/app/views/users/new.rhtml", "test/rails_root/app/views/users/show.rhtml", "test/rails_root/config", "test/rails_root/config/boot.rb", "test/rails_root/config/database.yml", "test/rails_root/config/environment.rb", "test/rails_root/config/environments", "test/rails_root/config/environments/sqlite3.rb", "test/rails_root/config/initializers", "test/rails_root/config/initializers/new_rails_defaults.rb", "test/rails_root/config/initializers/shoulda.rb", "test/rails_root/config/routes.rb", "test/rails_root/db", "test/rails_root/db/migrate", "test/rails_root/db/migrate/001_create_users.rb", "test/rails_root/db/migrate/002_create_posts.rb", "test/rails_root/db/migrate/003_create_taggings.rb", "test/rails_root/db/migrate/004_create_tags.rb", "test/rails_root/db/migrate/005_create_dogs.rb", "test/rails_root/db/migrate/006_create_addresses.rb", "test/rails_root/db/migrate/007_create_fleas.rb", "test/rails_root/db/migrate/008_create_dogs_fleas.rb", "test/rails_root/db/migrate/009_create_products.rb", "test/rails_root/db/migrate/010_create_friendships.rb", "test/rails_root/db/schema.rb", "test/rails_root/log", "test/rails_root/log/sqlite3.log", "test/rails_root/public", "test/rails_root/public/404.html", "test/rails_root/public/422.html", "test/rails_root/public/500.html", "test/rails_root/script", "test/rails_root/script/console", "test/rails_root/script/generate", "test/rails_root/vendor", "test/rails_root/vendor/plugins", "test/README", "test/test_helper.rb", "test/unit", "test/unit/address_test.rb", "test/unit/dog_test.rb", "test/unit/flea_test.rb", "test/unit/friendship_test.rb", "test/unit/post_test.rb", "test/unit/product_test.rb", "test/unit/tag_test.rb", "test/unit/tagging_test.rb", "test/unit/user_test.rb"]
s.has_rdoc = true
s.homepage = %q{http://thoughtbot.com/projects/shoulda}
s.rdoc_options = ["--line-numbers", "--inline-source", "--main", "README.rdoc"]