Skip to content
Browse files

Merge branch 'refactor-dependency-check'

Extract dependency check logic that raises ArgumentError in case of an
invalid :dependent option given to an association.

Closes #7054
  • Loading branch information...
2 parents 08ee16e + 0f6865e commit 26c1a4a01c14627bc13d6774696a4e622c768727 @carlosantoniodasilva carlosantoniodasilva committed Aug 1, 2012
View
10 activerecord/lib/active_record/associations/builder/association.rb
@@ -77,6 +77,16 @@ 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})"
+ end
+ end
+
def dependent_restrict_raises?
ActiveRecord::Base.dependent_restrict_raises == true
end
View
10 activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -72,16 +72,14 @@ def add_touch_callbacks(reflection)
end
def configure_dependency
- if options[:dependent]
- unless options[:dependent].in?([:destroy, :delete])
- raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{options[:dependent].inspect})"
- end
+ if dependent = options[:dependent]
+ check_valid_dependent! dependent, [:destroy, :delete]
- method_name = "belongs_to_dependent_#{options[:dependent]}_for_#{name}"
+ method_name = "belongs_to_dependent_#{dependent}_for_#{name}"
model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
def #{method_name}
association = #{name}
- association.#{options[:dependent]} if association
+ association.#{dependent} if association
end
eoruby
model.after_destroy method_name
View
11 activerecord/lib/active_record/associations/builder/has_many.rb
@@ -19,14 +19,11 @@ def build
private
def configure_dependency
- if options[:dependent]
- unless options[:dependent].in?([:destroy, :delete_all, :nullify, :restrict])
- raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, " \
- ":nullify or :restrict (#{options[:dependent].inspect})"
- end
+ if dependent = options[:dependent]
+ check_valid_dependent! dependent, [:destroy, :delete_all, :nullify, :restrict]
+ dependent_restrict_deprecation_warning if dependent == :restrict
- dependent_restrict_deprecation_warning if options[:dependent] == :restrict
- send("define_#{options[:dependent]}_dependency_method")
+ send("define_#{dependent}_dependency_method")
model.before_destroy dependency_method_name
end
end
View
13 activerecord/lib/active_record/associations/builder/has_one.rb
@@ -25,14 +25,11 @@ def build
private
def configure_dependency
- if options[:dependent]
- unless options[:dependent].in?([:destroy, :delete, :nullify, :restrict])
- raise ArgumentError, "The :dependent option expects either :destroy, :delete, " \
- ":nullify or :restrict (#{options[:dependent].inspect})"
- end
-
- dependent_restrict_deprecation_warning if options[:dependent] == :restrict
- send("define_#{options[:dependent]}_dependency_method")
+ if dependent = options[:dependent]
+ check_valid_dependent! dependent, [:destroy, :delete, :nullify, :restrict]
+ dependent_restrict_deprecation_warning if dependent == :restrict
+
+ send("define_#{dependent}_dependency_method")
model.before_destroy dependency_method_name
end
end

0 comments on commit 26c1a4a

Please sign in to comment.
Something went wrong with that request. Please try again.