Browse files

Clean up dependent option validation.

We don't need the complexity of to_sentence, and it shouldn't be a bang
method.
  • Loading branch information...
1 parent 5ad7998 commit 08fb3c8f33a17a3c8cc71a1e58110662eb37e429 @jonleighton jonleighton committed Aug 10, 2012
View
12 activerecord/lib/active_record/associations/builder/association.rb
@@ -77,16 +77,12 @@ def define_writers
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."
View
2 activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -72,7 +72,7 @@ def add_touch_callbacks(reflection)
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)
View
4 activerecord/lib/active_record/associations/builder/has_many.rb
@@ -19,9 +19,7 @@ def build
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
View
3 activerecord/lib/active_record/associations/builder/has_one.rb
@@ -25,8 +25,7 @@ def build
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

1 comment on commit 08fb3c8

@carlosantoniodasilva
Ruby on Rails member

I kinda agree with to_sentence, and the bang thing was added following other similar check methods in Active Record (such as Relation#check_validity!. Anyway, I see this was further refactored later, thanks.

Please sign in to comment.