Skip to content
This repository
Browse code

move method for dependent option check

  • Loading branch information...
commit 08a6b5753ed65244c793bab455c26454ab1a1dc5 1 parent c102022
Hrvoje Simic authored July 14, 2012 carlosantoniodasilva committed August 01, 2012
13  activerecord/lib/active_record/associations/builder/association.rb
@@ -77,6 +77,19 @@ def define_writers
77 77
         end
78 78
       end
79 79
 
  80
+      def print_valid_dependent_options(options)
  81
+        options.map {|s| s.to_s.insert 0, ":" }.
  82
+          to_sentence(:words_connector => ", ",
  83
+                      :two_words_connector => ' or ',
  84
+                      :last_word_connector => ' or ')
  85
+      end
  86
+
  87
+      def check_dependent_valid(valid_options)
  88
+        return if valid_options.include?(options[:dependent])
  89
+        raise ArgumentError, "The :dependent option expects either " \
  90
+          "#{print_valid_dependent_options(valid_options)} (#{options[:dependent].inspect})"
  91
+      end
  92
+
80 93
       def dependent_restrict_raises?
81 94
         ActiveRecord::Base.dependent_restrict_raises == true
82 95
       end
6  activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -73,9 +73,8 @@ def add_touch_callbacks(reflection)
73 73
 
74 74
       def configure_dependency
75 75
         if options[:dependent]
76  
-          unless options[:dependent].in?([:destroy, :delete])
77  
-            raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{options[:dependent].inspect})"
78  
-          end
  76
+
  77
+          check_dependent_valid [:destroy, :delete]
79 78
 
80 79
           method_name = "belongs_to_dependent_#{options[:dependent]}_for_#{name}"
81 80
           model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
@@ -85,6 +84,7 @@ def #{method_name}
85 84
             end
86 85
           eoruby
87 86
           model.after_destroy method_name
  87
+
88 88
         end
89 89
       end
90 90
   end
7  activerecord/lib/active_record/associations/builder/has_many.rb
@@ -20,14 +20,13 @@ def build
20 20
 
21 21
       def configure_dependency
22 22
         if options[:dependent]
23  
-          unless options[:dependent].in?([:destroy, :delete_all, :nullify, :restrict])
24  
-            raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, " \
25  
-                                 ":nullify or :restrict (#{options[:dependent].inspect})"
26  
-          end
  23
+
  24
+          check_dependent_valid [:destroy, :delete_all, :nullify, :restrict]
27 25
 
28 26
           dependent_restrict_deprecation_warning if options[:dependent] == :restrict
29 27
           send("define_#{options[:dependent]}_dependency_method")
30 28
           model.before_destroy dependency_method_name
  29
+
31 30
         end
32 31
       end
33 32
 
7  activerecord/lib/active_record/associations/builder/has_one.rb
@@ -26,14 +26,13 @@ def build
26 26
 
27 27
       def configure_dependency
28 28
         if options[:dependent]
29  
-          unless options[:dependent].in?([:destroy, :delete, :nullify, :restrict])
30  
-            raise ArgumentError, "The :dependent option expects either :destroy, :delete, " \
31  
-                                 ":nullify or :restrict (#{options[:dependent].inspect})"
32  
-          end
  29
+
  30
+          check_dependent_valid [:destroy, :delete, :nullify, :restrict]
33 31
 
34 32
           dependent_restrict_deprecation_warning if options[:dependent] == :restrict
35 33
           send("define_#{options[:dependent]}_dependency_method")
36 34
           model.before_destroy dependency_method_name
  35
+
37 36
         end
38 37
       end
39 38
 

0 notes on commit 08a6b57

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