Module: Esse::Plugins::ActiveRecord::RepositoryClassMethods

Defined in:
lib/esse/plugins/active_record.rb

Instance Method Summary collapse

Instance Method Details

#collection(*args, **kwargs, &block) ⇒ Object

Parameters:

  • model_class (Class)

    The ActiveRecord Relation or model class

  • options (Hash)

    The options



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/esse/plugins/active_record.rb', line 12

def collection(*args, **kwargs, &block)
  unless model_or_relation?(args.first)
    return super
  end
  model_class = args.shift

  repo = Class.new(Esse::ActiveRecord::Collection)
  repo.base_scope = -> { model_class }
  repo.batch_size = kwargs.delete(:batch_size) if kwargs.key?(:batch_size)
  repo.connect_with = kwargs.delete(:connect_with) if kwargs.key?(:connect_with)
  repo.class_eval(&block) if block

  super(repo, *args, **kwargs)
end

#dataset(**params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/esse/plugins/active_record.rb', line 27

def dataset(**params)
  if @collection_proc.nil?
    raise NotImplementedError, "Can't call `dataset' on a repository without a collection defined"
  elsif @collection_proc.is_a?(Class) && @collection_proc < Esse::ActiveRecord::Collection
    @collection_proc.new(**params).dataset
  elsif defined? super
    super
  else
    raise NoMethodError, "undefined method `dataset' for #{self}"
  end
end