module Sequel::Plugins::ManyThroughMany::ClassMethods

Public Instance Methods

many_through_many(name, through, opts=OPTS, &block) click to toggle source

Create a #many_through_many association. Arguments:

  • name - Same as associate, the name of the association.

  • through - The tables and keys to join between the current table and the associated table. Must be an array, with elements that are either 3 element arrays, or hashes with keys :table, :left, and :right. The required entries in the array/hash are:

    :table (first array element)

    The name of the table to join.

    :left (middle array element)

    The key joining the table to the previous table. Can use an array of symbols for a composite key association.

    :right (last array element)

    The key joining the table to the next table. Can use an array of symbols for a composite key association.

    If a hash is provided, the following keys are respected when using eager_graph:

    :block

    A proc to use as the block argument to join.

    :conditions

    Extra conditions to add to the JOIN ON clause. Must be a hash or array of two pairs.

    :join_type

    The join type to use for the join, defaults to :left_outer.

    :only_conditions

    Conditions to use for the join instead of the ones specified by the keys.

  • opts - The options for the associaion. Takes the same options as many_to_many.

# File lib/sequel/plugins/many_through_many.rb, line 160
def many_through_many(name, through, opts=OPTS, &block)
  associate(:many_through_many, name, opts.merge(through.is_a?(Hash) ? through : {:through=>through}), &block)
end

Private Instance Methods

def_many_through_many(opts) click to toggle source

Create the association methods and :eager_loader and :eager_grapher procs.

# File lib/sequel/plugins/many_through_many.rb, line 167
def def_many_through_many(opts)
  name = opts[:name]
  model = self
  opts[:read_only] = true
  opts[:after_load].unshift(:array_uniq!) if opts[:uniq]
  opts[:cartesian_product_number] ||= 2
  opts[:through] = opts[:through].map do |e|
    case e
    when Array
      raise(Error, "array elements of the through option/argument for many_through_many associations must have at least three elements") unless e.length == 3
      {:table=>e[0], :left=>e[1], :right=>e[2]}
    when Hash
      raise(Error, "hash elements of the through option/argument for many_through_many associations must contain :table, :left, and :right keys") unless e[:table] && e[:left] && e[:right]
      e
    else
      raise(Error, "the through option/argument for many_through_many associations must be an enumerable of arrays or hashes")
    end
  end

  left_key = opts[:left_key] = opts[:through].first[:left]
  uses_lcks = opts[:uses_left_composite_keys] = left_key.is_a?(Array)
  left_keys = Array(left_key)
  left_pk = (opts[:left_primary_key] ||= self.primary_key)
  opts[:eager_loader_key] = left_pk unless opts.has_key?(:eager_loader_key)
  left_pks = opts[:left_primary_keys] = Array(left_pk)
  lpkc = opts[:left_primary_key_column] ||= left_pk
  opts[:left_primary_key_columns] ||= Array(lpkc)
  opts[:dataset] ||= lambda do
    ds = opts.associated_dataset
    opts.reverse_edges.each{|t| ds = ds.join(t[:table], Array(t[:left]).zip(Array(t[:right])), :table_alias=>t[:alias], :qualify=>:deep)}
    ft = opts.final_reverse_edge
    ds.join(ft[:table],  Array(ft[:left]).zip(Array(ft[:right])) + opts.predicate_keys.zip(left_pks.map{|k| send(k)}), :table_alias=>ft[:alias], :qualify=>:deep)
  end

  slice_range = opts.slice_range
  left_key_alias = opts[:left_key_alias] ||= opts.default_associated_key_alias
  opts[:eager_loader] ||= lambda do |eo|
    h = eo[:id_map]
    rows = eo[:rows]
    rows.each{|object| object.associations[name] = []}
    ds = opts.associated_class 
    opts.reverse_edges.each{|t| ds = ds.join(t[:table], Array(t[:left]).zip(Array(t[:right])), :table_alias=>t[:alias], :qualify=>:deep)}
    ft = opts.final_reverse_edge
    ds = ds.join(ft[:table], Array(ft[:left]).zip(Array(ft[:right])) + [[opts.predicate_key, h.keys]], :table_alias=>ft[:alias], :qualify=>:deep)
    ds = model.eager_loading_dataset(opts, ds, nil, eo[:associations], eo)
    if opts.eager_limit_strategy == :window_function
      delete_rn = true
      rn = ds.row_number_column
      ds = apply_window_function_eager_limit_strategy(ds, opts)
    end
    ds.all do |assoc_record|
      assoc_record.values.delete(rn) if delete_rn
      hash_key = if uses_lcks
        left_key_alias.map{|k| assoc_record.values.delete(k)}
      else
        assoc_record.values.delete(left_key_alias)
      end
      next unless objects = h[hash_key]
      objects.each{|object| object.associations[name].push(assoc_record)}
    end
    if opts.eager_limit_strategy == :ruby
      rows.each{|o| o.associations[name] = o.associations[name][slice_range] || []}
    end
  end

  join_type = opts[:graph_join_type]
  select = opts[:graph_select]
  graph_block = opts[:graph_block]
  only_conditions = opts[:graph_only_conditions]
  use_only_conditions = opts.include?(:graph_only_conditions)
  conditions = opts[:graph_conditions]
  opts[:eager_grapher] ||= proc do |eo|
    ds = eo[:self]
    iq = eo[:implicit_qualifier]
    opts.edges.each do |t|
      ds = ds.graph(t[:table], t.fetch(:only_conditions, (Array(t[:right]).zip(Array(t[:left])) + t[:conditions])), :select=>false, :table_alias=>ds.unused_table_alias(t[:table]), :join_type=>t[:join_type], :qualify=>:deep, :implicit_qualifier=>iq, &t[:block])
      iq = nil
    end
    fe = opts.final_edge
    ds.graph(opts.associated_class, use_only_conditions ? only_conditions : (Array(opts.right_primary_key).zip(Array(fe[:left])) + conditions), :select=>select, :table_alias=>eo[:table_alias], :qualify=>:deep, :join_type=>join_type, &graph_block)
  end

  def_association_dataset_methods(opts)
end