aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-08-10 13:09:12 +0100
committerJon Leighton <j@jonathanleighton.com>2012-08-10 17:45:07 +0100
commit08fb3c8f33a17a3c8cc71a1e58110662eb37e429 (patch)
tree5eaee7075db2da4f01bf9b31958e051bbf409296 /activerecord
parent5ad79989ef0a015fd22cfed90b2e8a56881e6c36 (diff)
downloadrails-08fb3c8f33a17a3c8cc71a1e58110662eb37e429.tar.gz
rails-08fb3c8f33a17a3c8cc71a1e58110662eb37e429.tar.bz2
rails-08fb3c8f33a17a3c8cc71a1e58110662eb37e429.zip
Clean up dependent option validation.
We don't need the complexity of to_sentence, and it shouldn't be a bang method.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/builder/association.rb12
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb2
-rw-r--r--activerecord/lib/active_record/associations/builder/has_many.rb4
-rw-r--r--activerecord/lib/active_record/associations/builder/has_one.rb3
4 files changed, 7 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb
index 47f06421ee..14e8f9f8f5 100644
--- a/activerecord/lib/active_record/associations/builder/association.rb
+++ b/activerecord/lib/active_record/associations/builder/association.rb
@@ -77,16 +77,12 @@ module ActiveRecord::Associations::Builder
end
end
- def check_valid_dependent!(dependent, valid_options)
- unless valid_options.include?(dependent)
- valid_options_message = valid_options.map(&:inspect).to_sentence(
- words_connector: ', ', two_words_connector: ' or ', last_word_connector: ' or ')
-
- raise ArgumentError, "The :dependent option expects either " \
- "#{valid_options_message} (#{dependent.inspect})"
+ def validate_dependent_option(valid_options)
+ unless valid_options.include? options[:dependent]
+ raise ArgumentError, "The :dependent option must be one of #{valid_options}, but is :#{options[:dependent]}"
end
- if dependent == :restrict
+ if options[:dependent] == :restrict
ActiveSupport::Deprecation.warn(
"The :restrict option is deprecated. Please use :restrict_with_exception instead, which " \
"provides the same functionality."
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index f205a456f7..b9642f556d 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -72,7 +72,7 @@ module ActiveRecord::Associations::Builder
def configure_dependency
if dependent = options[:dependent]
- check_valid_dependent! dependent, [:destroy, :delete]
+ validate_dependent_option [:destroy, :delete]
method_name = "belongs_to_dependent_#{dependent}_for_#{name}"
model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
diff --git a/activerecord/lib/active_record/associations/builder/has_many.rb b/activerecord/lib/active_record/associations/builder/has_many.rb
index 63278c8cfd..ee52e873d0 100644
--- a/activerecord/lib/active_record/associations/builder/has_many.rb
+++ b/activerecord/lib/active_record/associations/builder/has_many.rb
@@ -19,9 +19,7 @@ module ActiveRecord::Associations::Builder
def configure_dependency
if dependent = options[:dependent]
- check_valid_dependent! dependent, [:destroy, :delete_all, :nullify, :restrict,
- :restrict_with_error, :restrict_with_exception]
-
+ validate_dependent_option [:destroy, :delete_all, :nullify, :restrict, :restrict_with_error, :restrict_with_exception]
send("define_#{dependent}_dependency_method")
model.before_destroy dependency_method_name
end
diff --git a/activerecord/lib/active_record/associations/builder/has_one.rb b/activerecord/lib/active_record/associations/builder/has_one.rb
index 82c5905caf..e5d57b2990 100644
--- a/activerecord/lib/active_record/associations/builder/has_one.rb
+++ b/activerecord/lib/active_record/associations/builder/has_one.rb
@@ -25,8 +25,7 @@ module ActiveRecord::Associations::Builder
def configure_dependency
if dependent = options[:dependent]
- check_valid_dependent! dependent, [:destroy, :delete, :nullify, :restrict, :restrict_with_error, :restrict_with_exception]
-
+ validate_dependent_option [:destroy, :delete, :nullify, :restrict, :restrict_with_error, :restrict_with_exception]
send("define_#{dependent}_dependency_method")
model.before_destroy dependency_method_name
end