Sha256: 6f028c0a4200880d572009af56da16b6624919f125c477c3bb6e24252423a54f

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

module Torque
  module PostgreSQL
    module AutosaveAssociation
      module ClassMethods
        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) { save_belongs_to_many_array(reflection) }

          before_save(:before_save_collection_association)
          after_save(:after_save_collection_association) if ::ActiveRecord::Base
            .instance_methods.include?(:after_save_collection_association)

          before_create(save_method)
          before_update(save_method)

          define_autosave_validation_callbacks(reflection)
        end
      end

      def save_belongs_to_many_array(reflection)
        save_collection_association(reflection)

        association = association_instance_get(reflection.name)
        return unless association

        klass_attr = reflection.active_record_primary_key
        source_attr = reflection.foreign_key

        records = association.target.each_with_object(klass_attr)
        write_attribute(source_attr, records.map(&:read_attribute).compact)
      end
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
torque-postgresql-1.1.8 lib/torque/postgresql/autosave_association.rb
torque-postgresql-1.1.7 lib/torque/postgresql/autosave_association.rb
torque-postgresql-2.0.3 lib/torque/postgresql/autosave_association.rb