Sha256: 96c85d6c9613fb2b3cf8ad31fe76d19a6693582b401c19f9d06614c3654592ea

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require "batch-loader"
require "batch_loader_active_record/version"
require "batch_loader_active_record/association_manager"

module BatchLoaderActiveRecord
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def belongs_to_lazy(*args)
      belongs_to(*args).tap do |reflections|
        manager = AssociationManager.new(model: self, reflection: reflections.values.last)
        define_method(manager.accessor_name) { manager.belongs_to_batch_loader(self) }
      end
    end

    def association_accessor(name)
      reflection = reflect_on_association(name) or raise "Can't find association #{name.inspect}"
      manager = AssociationManager.new(model: self, reflection: reflection)
      case reflection.macro
      when :belongs_to
        define_method(manager.accessor_name) { manager.belongs_to_batch_loader(self) }
      when :has_one
        define_method(manager.accessor_name) { manager.has_one_to_batch_loader(self) }
      when :has_many
        define_method(manager.accessor_name) do |instance_scope = nil|
          manager.has_many_to_batch_loader(self, instance_scope)
        end
      else
        raise NotImplementedError, "association kind #{reflection.macro.inspect} is not yet supported"
      end
    end

    def has_one_lazy(*args)
      has_one(*args).tap do |reflections|
        manager = AssociationManager.new(model: self, reflection: reflections.values.last)
        define_method(manager.accessor_name) { manager.has_one_to_batch_loader(self) }
      end
    end

    def has_many_lazy(*args)
      has_many(*args).tap do |reflections|
        manager = AssociationManager.new(model: self, reflection: reflections.values.last)
        define_method(manager.accessor_name) do |instance_scope = nil|
          manager.has_many_to_batch_loader(self, instance_scope)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
batch-loader-active-record-0.3.1 lib/batch_loader_active_record.rb