Sha256: 9adc760f9290b3d4cf573dc628b2fbc23f9d174ea92354f7baecf593489de8b4

Contents?: true

Size: 1.25 KB

Versions: 20

Compression:

Stored size: 1.25 KB

Contents

# encoding: utf-8
module Dynamoid #:nodoc:

  # The has and belongs to many association.
  module Associations
    class HasAndBelongsToMany
      include ManyAssociation

      private

      # Find the target association, always another :has_and_belongs_to_many association. Uses either options[:inverse_of] or the source class name
      # and default parsing to return the most likely name for the target association.
      #
      # @since 0.2.0
      def target_association
        key_name = options[:inverse_of] || source.class.to_s.pluralize.underscore.to_sym
        guess = target_class.associations[key_name]
        return nil if guess.nil? || guess[:type] != :has_and_belongs_to_many
        key_name
      end

      # Associate a source object to this association.
      #
      # @since 0.2.0
      def associate_target(object)
        ids = object.send(target_attribute) || Set.new
        object.update_attribute(target_attribute, ids.merge(Array(source.id)))
      end

      # Disassociate a source object from this association.
      #
      # @since 0.2.0
      def disassociate_target(object)
        ids = object.send(target_attribute) || Set.new
        object.update_attribute(target_attribute, ids - Array(source.id))
      end
    end
  end

end

Version data entries

20 entries across 20 versions & 4 rubygems

Version Path
dynamoid-1.2.1 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-1.2.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-edge-1.1.2 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-edge-1.1.1 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-edge-1.1.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-1.1.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-1.0.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-moda-0.7.2 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-moda-0.7.1 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.7.1 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.7.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.6.1 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.6.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
adept_dynamoid-0.6.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
adept_dynamoid-0.5.0.8 lib/dynamoid/associations/has_and_belongs_to_many.rb
adept_dynamoid-0.5.0.7 lib/dynamoid/associations/has_and_belongs_to_many.rb
adept_dynamoid-0.5.0.6 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.5.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.4.1 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.4.0 lib/dynamoid/associations/has_and_belongs_to_many.rb