Sha256: 2f1e42ddea7bc4f2c9041d9c08f05dd945c375fc4026b435b1b5e98106de1c62

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

Contents

module Dase
  module PreloaderMethods
    def foreign_key
      reflection.klass.arel_table[reflection.foreign_key]
    end

    def preload(preloader)
      pk = model.primary_key.to_sym
      ids = owners.map(&pk)
      scope = records_for(ids)

      # applying options like :where => ... or :conditions => ..., or -> {....}
      scope = apply_association_options(preloader.options.clone, scope)

      # the actual magic of attaching counters to the records comes here
      counters_hash = scope.group(foreign_key).count(Arel.star)
      owners.each do |owner|
        owner.define_singleton_method(preloader.options[:as]) do
          counters_hash[owner[pk]] || 0
        end
      end
    end

    def apply_association_options(options, scope)
      proc = options.delete(:proc)

      # applying proc syntax: -> {...}
      scope = scope.instance_exec(&proc) if proc

      options.slice(*VALID_ASSOCIATION_OPTIONS).each do |key, value|
        scope = scope.send(key, value)
      end

      scope
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dase-5.0.0 lib/dase/preloader_methods.rb
dase-5.1.0 lib/dase/preloader_methods.rb
dase-4.2.1 lib/dase/preloader_methods.rb
dase-4.2.0 lib/dase/preloader_methods.rb
dase-4.1.1 lib/dase/preloader_methods.rb