Sha256: 51cbb6b61fcb6b0e51cbfe05d0d694c51f16e45e4f5d3a4f004be0211f189b28

Contents?: true

Size: 1.8 KB

Versions: 127

Compression:

Stored size: 1.8 KB

Contents

# frozen-string-literal: true

module Sequel
  module Plugins
    # The delay_add_association plugin delays the adding of
    # associated objects to a new (unsaved) object until after the new
    # object is saved.  By default, if you attempt to add
    # associated objects to a new object, Sequel will raise
    # an error, because you need to have a primary key before
    # saving the objects.
    #
    # When delaying the add of an associated object, the object
    # will be immediately added to the cached association array.
    # When saving the current object, it will also attempt to
    # validate any associated objects, and if the associated objects
    # are not valid, the current object will also be considered
    # not valid.
    #
    # Usage:
    #
    #   # Make all model subclass delay add_association for new objects
    #   Sequel::Model.plugin :delay_add_association
    #
    #   # Make the Album class delay add_association for new objects
    #   Album.plugin :delay_add_association
    module DelayAddAssociation
      # Depend on the validate_associated plugin.
      def self.apply(mod)
        mod.plugin :validate_associated
      end

      module InstanceMethods
        private

        # Delay the addition of the associated object till after
        # saving the current object, if the current object is new
        # and the associated dataset requires a primary key on the
        # current object.
        def add_associated_object(opts, o, *args)
          if opts.dataset_need_primary_key? && new?
            o = make_add_associated_object(opts, o)
            delay_validate_associated_object(opts, o)
            public_send(opts[:name]) << o
            after_create_hook{super(opts, o, *args)}
            o
          else
            super
          end
        end
      end
    end
  end
end

Version data entries

127 entries across 110 versions & 2 rubygems

Version Path
sequel-5.67.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.66.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.65.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.64.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.63.0 lib/sequel/plugins/delay_add_association.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/sequel-5.62.0/lib/sequel/plugins/delay_add_association.rb
sequel-5.62.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.61.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.60.1 lib/sequel/plugins/delay_add_association.rb
sequel-5.60.0 lib/sequel/plugins/delay_add_association.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/sequel-5.59.0/lib/sequel/plugins/delay_add_association.rb
sequel-5.59.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.58.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.57.0 lib/sequel/plugins/delay_add_association.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/sequel-5.56.0/lib/sequel/plugins/delay_add_association.rb
sequel-5.56.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.55.0 lib/sequel/plugins/delay_add_association.rb
sequel-5.54.0 lib/sequel/plugins/delay_add_association.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/sequel-5.53.0/lib/sequel/plugins/delay_add_association.rb
sequel-5.53.0 lib/sequel/plugins/delay_add_association.rb