Files

Class Index [+]

Quicksearch

ActiveRecord::AutosaveAssociation::ClassMethods

Private Instance Methods

add_autosave_association_callbacks(reflection) click to toggle source

Adds validation and save callbacks for the association as specified by the reflection.

For performance reasons, we don’t check whether to validate at runtime. However the validation and callback methods are lazy and those methods get created when they are invoked for the very first time. However, this can change, for instance, when using nested attributes, which is called after the association has been defined. Since we don’t want the callbacks to get defined multiple times, there are guards that check if the save or validation methods have already been defined before actually defining them.

     # File lib/active_record/autosave_association.rb, line 154
154:       def add_autosave_association_callbacks(reflection)
155:         save_method = :"autosave_associated_records_for_#{reflection.name}"
156:         validation_method = :"validate_associated_records_for_#{reflection.name}"
157:         collection = reflection.collection?
158: 
159:         unless method_defined?(save_method)
160:           if collection
161:             before_save :before_save_collection_association
162: 
163:             define_method(save_method) { save_collection_association(reflection) }
164:             # Doesn't use after_save as that would save associations added in after_create/after_update twice
165:             after_create save_method
166:             after_update save_method
167:           else
168:             if reflection.macro == :has_one
169:               define_method(save_method) { save_has_one_association(reflection) }
170:               after_save save_method
171:             else
172:               define_method(save_method) { save_belongs_to_association(reflection) }
173:               before_save save_method
174:             end
175:           end
176:         end
177: 
178:         if reflection.validate? && !method_defined?(validation_method)
179:           method = (collection ? :validate_collection_association : :validate_single_association)
180:           define_method(validation_method) { send(method, reflection) }
181:           validate validation_method
182:         end
183:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.