Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7813

Show
Ignore:
Timestamp:
10/09/07 00:32:36 (1 year ago)
Author:
bitsweat
Message:

Update :dependent docs and improve its argument error message.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/associations.rb

    r7812 r7813  
    625625      #   objects are deleted *without* calling their destroy method.  If set to <tt>:nullify</tt> all associated 
    626626      #   objects' foreign keys are set to +NULL+ *without* calling their save callbacks. 
    627       #   May not be set if <tt>:exclusively_dependent</tt> is also set. 
    628627      # * <tt>:finder_sql</tt>  - specify a complete SQL statement to fetch the association. This is a good way to go for complex 
    629628      #   associations that depend on multiple tables. Note: When this option is used, +find_in_collection+ is _not_ added. 
     
    706705      # * <tt>:order</tt>       - specify the order from which the associated object will be picked at the top. Specified as 
    707706      #   an <tt>ORDER BY</tt> SQL fragment, such as <tt>last_name, first_name DESC</tt> 
    708       # * <tt>:dependent</tt>   - if set to <tt>:destroy</tt> (or +true+), the associated object is destroyed when this object is. If set to 
     707      # * <tt>:dependent</tt>   - if set to <tt>:destroy</tt>, the associated object is destroyed when this object is. If set to 
    709708      #   <tt>:delete</tt>, the associated object is deleted *without* calling its destroy method. If set to <tt>:nullify</tt>, the associated 
    710709      #   object's foreign key is set to +NULL+. Also, association is assigned. 
     
    11181117        end 
    11191118 
     1119        # See HasManyAssociation#delete_records.  Dependent associations 
     1120        # delete children, otherwise foreign key is set to NULL. 
    11201121        def configure_dependency_for_has_many(reflection) 
    1121           # See HasManyAssociation#delete_records.  Dependent associations 
    1122           # delete children, otherwise foreign key is set to NULL. 
    1123  
    1124           # Add polymorphic type if the :as option is present 
    1125           dependent_conditions = [] 
    1126           dependent_conditions << "#{reflection.primary_key_name} = \#{record.quoted_id}" 
    1127           dependent_conditions << "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as] 
    1128           dependent_conditions << sanitize_sql(reflection.options[:conditions]) if reflection.options[:conditions] 
    1129           dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ") 
    1130  
    1131           case reflection.options[:dependent] 
    1132             when :destroy 
    1133               module_eval "before_destroy '#{reflection.name}.each { |o| o.destroy }'" 
    1134             when :delete_all 
    1135               module_eval "before_destroy { |record| #{reflection.class_name}.delete_all(%(#{dependent_conditions})) }" 
    1136             when :nullify 
    1137               module_eval "before_destroy { |record| #{reflection.class_name}.update_all(%(#{reflection.primary_key_name} = NULL),  %(#{dependent_conditions})) }" 
    1138             when nil 
    1139               # pass 
    1140             else 
    1141               raise ArgumentError, 'The :dependent option expects either :destroy, :delete_all, or :nullify' 
     1122          if reflection.options.include?(:dependent) 
     1123            # Add polymorphic type if the :as option is present 
     1124            dependent_conditions = [] 
     1125            dependent_conditions << "#{reflection.primary_key_name} = \#{record.quoted_id}" 
     1126            dependent_conditions << "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as] 
     1127            dependent_conditions << sanitize_sql(reflection.options[:conditions]) if reflection.options[:conditions] 
     1128            dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ") 
     1129 
     1130            case reflection.options[:dependent] 
     1131              when :destroy 
     1132                module_eval "before_destroy '#{reflection.name}.each { |o| o.destroy }'" 
     1133              when :delete_all 
     1134                module_eval "before_destroy { |record| #{reflection.class_name}.delete_all(%(#{dependent_conditions})) }" 
     1135              when :nullify 
     1136                module_eval "before_destroy { |record| #{reflection.class_name}.update_all(%(#{reflection.primary_key_name} = NULL),  %(#{dependent_conditions})) }" 
     1137              else 
     1138                raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, or :nullify (#{reflection.options[:dependent].inspect})" 
     1139            end 
    11421140          end 
    11431141        end 
    11441142 
    11451143        def configure_dependency_for_has_one(reflection) 
    1146           case reflection.options[:dependent] 
    1147             when :destroy 
    1148               module_eval "before_destroy '#{reflection.name}.destroy unless #{reflection.name}.nil?'" 
    1149             when :delete 
    1150               module_eval "before_destroy '#{reflection.class_name}.delete(#{reflection.name}.id) unless #{reflection.name}.nil?'" 
    1151             when :nullify 
    1152               module_eval "before_destroy '#{reflection.name}.update_attribute(\"#{reflection.primary_key_name}\", nil) unless #{reflection.name}.nil?'" 
    1153             when nil 
    1154               # pass 
    1155             else 
    1156               raise ArgumentError, "The :dependent option expects either :destroy, :delete or :nullify." 
     1144          if reflection.options.include?(:dependent) 
     1145            case reflection.options[:dependent] 
     1146              when :destroy 
     1147                module_eval "before_destroy '#{reflection.name}.destroy unless #{reflection.name}.nil?'" 
     1148              when :delete 
     1149                module_eval "before_destroy '#{reflection.class_name}.delete(#{reflection.name}.id) unless #{reflection.name}.nil?'" 
     1150              when :nullify 
     1151                module_eval "before_destroy '#{reflection.name}.update_attribute(\"#{reflection.primary_key_name}\", nil) unless #{reflection.name}.nil?'" 
     1152              else 
     1153                raise ArgumentError, "The :dependent option expects either :destroy, :delete or :nullify (#{reflection.options[:dependent].inspect})" 
     1154            end 
    11571155          end 
    11581156        end