module ActiveFedora::AutosaveAssociation::ClassMethods
Private Instance Methods
define_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_fedora/autosave_association.rb, line 126 def define_autosave_association_callbacks(reflection) save_method = :"autosave_associated_records_for_#{reflection.name}" validation_method = :"validate_associated_records_for_#{reflection.name}" collection = reflection.collection? unless method_defined?(save_method) if collection before_save :before_save_collection_association define_non_cyclic_method(save_method, reflection) { save_collection_association(reflection) } # Doesn't use after_save as that would save associations added in after_create/after_update twice after_create save_method after_update save_method else define_non_cyclic_method(save_method, reflection) { save_belongs_to_association(reflection) } before_save save_method end end if reflection.validate? && !method_defined?(validation_method) method = (collection ? :validate_collection_association : :validate_single_association) define_non_cyclic_method(validation_method, reflection) { send(method, reflection) } validate validation_method end end
define_non_cyclic_method(name, reflection, &block)
click to toggle source
# File lib/active_fedora/autosave_association.rb, line 97 def define_non_cyclic_method(name, reflection, &block) define_method(name) do |*_args| result = true @_already_called ||= {} # Loop prevention for validation of associations unless @_already_called[[name, reflection.name]] begin @_already_called[[name, reflection.name]] = true result = instance_eval(&block) ensure @_already_called[[name, reflection.name]] = false end end result end end