Sha256: 3f21658b65216e877b3cc547b4c2769e071ffbf938f789a8fa3a4932589297a0

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

require 'active_ldap/association/collection'

module ActiveLdap
  module Association
    class BelongsToMany < Collection
      private
      def insert_entry(entry)
        old_value = entry[@options[:many], true]
        new_value = old_value + @owner[@options[:foreign_key_name], true]
        new_value = new_value.uniq.sort
        if old_value != new_value
          entry[@options[:many]] = new_value
          entry.save
        end
      end

      def delete_entries(entries)
        entries.each do |entry|
          old_value = entry[@options[:many], true]
          new_value = old_value - @owner[@options[:foreign_key_name], true]
          new_value = new_value.uniq.sort
          if old_value != new_value
            entry[@options[:many]] = new_value
            entry.save
          end
        end
      end

      def find_target
        values = @owner[@options[:foreign_key_name], true].compact
        return [] if values.empty?

        key = @options[:many]
        components = values.collect do |value|
          [key, value]
        end
        options = find_options(:filter => [:or, *components])
        foreign_class.find(:all, options)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
activeldap-0.10.0 lib/active_ldap/association/belongs_to_many.rb
activeldap-0.9.0 lib/active_ldap/association/belongs_to_many.rb
activeldap-1.0.0 lib/active_ldap/association/belongs_to_many.rb
ruby-activeldap-0.8.3.1 lib/active_ldap/association/belongs_to_many.rb