提交 6bb8afb4 编写于 作者: J Jon Leighton

DRY up handling of dependent option

上级 825c05d4
......@@ -39,6 +39,7 @@ def mixin
def build
validate_options
define_accessors
configure_dependency if options[:dependent]
@reflection = model.create_reflection(macro, name, scope, options, model)
super # provides an extension point
@reflection
......@@ -75,9 +76,9 @@ def define_writers
end
end
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]}"
def configure_dependency
unless valid_dependent_options.include? options[:dependent]
raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{options[:dependent]}"
end
if options[:dependent] == :restrict
......@@ -86,6 +87,17 @@ def validate_dependent_option(valid_options)
"provides the same functionality."
)
end
name = self.name
mixin.redefine_method("#{macro}_dependent_for_#{name}") do
association(name).handle_dependency
end
model.before_destroy "#{macro}_dependent_for_#{name}"
end
def valid_dependent_options
raise NotImplementedError
end
end
end
module ActiveRecord::Associations::Builder
class BelongsTo < SingularAssociation #:nodoc:
def macro
......@@ -17,7 +16,6 @@ def build
reflection = super
add_counter_cache_callbacks(reflection) if options[:counter_cache]
add_touch_callbacks(reflection) if options[:touch]
configure_dependency
reflection
end
......@@ -68,22 +66,8 @@ def add_touch_callbacks(reflection)
model.after_destroy(method_name)
end
def configure_dependency
if dependent = options[:dependent]
validate_dependent_option [:destroy, :delete]
model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
def #{dependency_method_name}
association(:#{name}).handle_dependency
end
eoruby
model.after_destroy dependency_method_name
end
end
def dependency_method_name
"belongs_to_dependent_for_#{name}"
def valid_dependent_options
[:destroy, :delete]
end
end
end
module ActiveRecord::Associations::Builder
class CollectionAssociation < Association #:nodoc:
CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
......
module ActiveRecord::Associations::Builder
class HasMany < CollectionAssociation #:nodoc:
def macro
......@@ -9,27 +8,8 @@ def valid_options
super + [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of]
end
def build
reflection = super
configure_dependency
reflection
end
def configure_dependency
if dependent = options[:dependent]
validate_dependent_option [:destroy, :delete_all, :nullify, :restrict, :restrict_with_error, :restrict_with_exception]
name = self.name
mixin.redefine_method(dependency_method_name) do
association(name).handle_dependency
end
model.before_destroy dependency_method_name
end
end
def dependency_method_name
"has_many_dependent_for_#{name}"
def valid_dependent_options
[:destroy, :delete_all, :nullify, :restrict, :restrict_with_error, :restrict_with_exception]
end
end
end
module ActiveRecord::Associations::Builder
class HasOne < SingularAssociation #:nodoc:
def macro
......@@ -15,27 +14,12 @@ def constructable?
!options[:through]
end
def build
reflection = super
configure_dependency unless options[:through]
reflection
end
def configure_dependency
if dependent = options[:dependent]
validate_dependent_option [:destroy, :delete, :nullify, :restrict, :restrict_with_error, :restrict_with_exception]
name = self.name
mixin.redefine_method(dependency_method_name) do
association(name).handle_dependency
end
model.before_destroy dependency_method_name
end
super unless options[:through]
end
def dependency_method_name
"has_one_dependent_for_#{name}"
def valid_dependent_options
[:destroy, :delete, :nullify, :restrict, :restrict_with_error, :restrict_with_exception]
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册