提交 26c1a4a0 编写于 作者: C Carlos Antonio da Silva

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
...@@ -77,6 +77,16 @@ def define_writers ...@@ -77,6 +77,16 @@ def define_writers
end end
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? def dependent_restrict_raises?
ActiveRecord::Base.dependent_restrict_raises == true ActiveRecord::Base.dependent_restrict_raises == true
end end
......
...@@ -72,16 +72,14 @@ def add_touch_callbacks(reflection) ...@@ -72,16 +72,14 @@ def add_touch_callbacks(reflection)
end end
def configure_dependency def configure_dependency
if options[:dependent] if dependent = options[:dependent]
unless options[:dependent].in?([:destroy, :delete]) check_valid_dependent! dependent, [:destroy, :delete]
raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{options[:dependent].inspect})"
end
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) model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
def #{method_name} def #{method_name}
association = #{name} association = #{name}
association.#{options[:dependent]} if association association.#{dependent} if association
end end
eoruby eoruby
model.after_destroy method_name model.after_destroy method_name
......
...@@ -19,14 +19,11 @@ def build ...@@ -19,14 +19,11 @@ def build
private private
def configure_dependency def configure_dependency
if options[:dependent] if dependent = options[:dependent]
unless options[:dependent].in?([:destroy, :delete_all, :nullify, :restrict]) check_valid_dependent! dependent, [:destroy, :delete_all, :nullify, :restrict]
raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, " \ dependent_restrict_deprecation_warning if dependent == :restrict
":nullify or :restrict (#{options[:dependent].inspect})"
end
dependent_restrict_deprecation_warning if options[:dependent] == :restrict send("define_#{dependent}_dependency_method")
send("define_#{options[:dependent]}_dependency_method")
model.before_destroy dependency_method_name model.before_destroy dependency_method_name
end end
end end
......
...@@ -25,14 +25,11 @@ def build ...@@ -25,14 +25,11 @@ def build
private private
def configure_dependency def configure_dependency
if options[:dependent] if dependent = options[:dependent]
unless options[:dependent].in?([:destroy, :delete, :nullify, :restrict]) check_valid_dependent! dependent, [:destroy, :delete, :nullify, :restrict]
raise ArgumentError, "The :dependent option expects either :destroy, :delete, " \ dependent_restrict_deprecation_warning if dependent == :restrict
":nullify or :restrict (#{options[:dependent].inspect})"
end send("define_#{dependent}_dependency_method")
dependent_restrict_deprecation_warning if options[:dependent] == :restrict
send("define_#{options[:dependent]}_dependency_method")
model.before_destroy dependency_method_name model.before_destroy dependency_method_name
end end
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册