Sha256: 726bebfaf0a436150f5134f05e0ddcd7d0b4e9c0892b3c98a7ef1c4cf2eb39ba

Contents?: true

Size: 1.52 KB

Versions: 29

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Torque
  module PostgreSQL
    module AutosaveAssociation
      module ClassMethods
        # Since belongs to many is a collection, the callback would normally go
        # to +after_create+. However, since it is a +belongs_to+ kind of
        # association, it neds to be executed +before_save+
        def add_autosave_association_callbacks(reflection)
          return super unless reflection.macro.eql?(:belongs_to_many)

          save_method = :"autosave_associated_records_for_#{reflection.name}"
          define_non_cyclic_method(save_method) do
            save_belongs_to_many_association(reflection)
          end

          before_save(save_method)

          define_autosave_validation_callbacks(reflection)
        end
      end

      # Ensure the right way to execute +save_collection_association+ and also
      # keep it as a single change using +build_changes+
      def save_belongs_to_many_association(reflection)
        previously_new_record_before_save = (@new_record_before_save ||= false)
        @new_record_before_save = new_record?

        association = association_instance_get(reflection.name)
        association&.build_changes { save_collection_association(reflection) }
      rescue ::ActiveRecord::RecordInvalid
        throw(:abort)
      ensure
        @new_record_before_save = previously_new_record_before_save
      end
    end

    ::ActiveRecord::Base.singleton_class.prepend(AutosaveAssociation::ClassMethods)
    ::ActiveRecord::Base.include(AutosaveAssociation)
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
torque-postgresql-3.4.1 lib/torque/postgresql/autosave_association.rb
torque-postgresql-2.4.5 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.4.0 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.3.3 lib/torque/postgresql/autosave_association.rb
torque-postgresql-2.4.4 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.3.2 lib/torque/postgresql/autosave_association.rb
torque-postgresql-2.4.3 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.3.1 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.3.0 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.2.2 lib/torque/postgresql/autosave_association.rb
torque-postgresql-2.4.2 lib/torque/postgresql/autosave_association.rb
torque-postgresql-2.4.1 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.2.1 lib/torque/postgresql/autosave_association.rb
torque-postgresql-2.4.0 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.2.0 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.1.0 lib/torque/postgresql/autosave_association.rb
torque-postgresql-2.3.0 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.0.1 lib/torque/postgresql/autosave_association.rb
torque-postgresql-2.2.4 lib/torque/postgresql/autosave_association.rb
torque-postgresql-3.0.0 lib/torque/postgresql/autosave_association.rb