Permalink
Browse files

move method for dependent option check

  • Loading branch information...
1 parent c102022 commit 08a6b5753ed65244c793bab455c26454ab1a1dc5 @shime shime committed with carlosantoniodasilva Jul 14, 2012
@@ -77,6 +77,19 @@ def define_writers
end
end
+ def print_valid_dependent_options(options)
+ options.map {|s| s.to_s.insert 0, ":" }.
+ to_sentence(:words_connector => ", ",
+ :two_words_connector => ' or ',
+ :last_word_connector => ' or ')
+ end
+
+ def check_dependent_valid(valid_options)
+ return if valid_options.include?(options[:dependent])
+ raise ArgumentError, "The :dependent option expects either " \
+ "#{print_valid_dependent_options(valid_options)} (#{options[:dependent].inspect})"
+ end
+
def dependent_restrict_raises?
ActiveRecord::Base.dependent_restrict_raises == true
end
@@ -73,9 +73,8 @@ def add_touch_callbacks(reflection)
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
+
+ check_dependent_valid [:destroy, :delete]
method_name = "belongs_to_dependent_#{options[:dependent]}_for_#{name}"
model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
@@ -85,6 +84,7 @@ def #{method_name}
end
eoruby
model.after_destroy method_name
+
end
end
end
@@ -20,14 +20,13 @@ def build
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
+
+ check_dependent_valid [:destroy, :delete_all, :nullify, :restrict]
dependent_restrict_deprecation_warning if options[:dependent] == :restrict
send("define_#{options[:dependent]}_dependency_method")
model.before_destroy dependency_method_name
+
end
end
@@ -26,14 +26,13 @@ def build
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
+
+ check_dependent_valid [:destroy, :delete, :nullify, :restrict]
dependent_restrict_deprecation_warning if options[:dependent] == :restrict
send("define_#{options[:dependent]}_dependency_method")
model.before_destroy dependency_method_name
+
end
end

0 comments on commit 08a6b57

Please sign in to comment.