Class: ReeDao::Agg

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_dao/package/ree_dao/functions/agg.rb

Instance Method Summary collapse

Instance Method Details

#call(dao, ids_or_scope, **opts, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/functions/agg.rb', line 24

def call(dao, ids_or_scope, **opts, &block)
  scope = if ids_or_scope.is_a?(Array) && ids_or_scope.any? { _1.is_a?(Integer) }
    return [] if ids_or_scope.empty?
    dao.where(id: ids_or_scope)
  elsif ids_or_scope.is_a?(Integer)
    dao.where(id: ids_or_scope)
  else
    ids_or_scope
  end

  list = scope.is_a?(Sequel::Dataset) ? scope.all : scope

  if opts[:to_dto]
    list = list.map do |item|
      list = opts[:to_dto].call(item)
    end
  end

  load_associations(dao.unfiltered, list, **opts, &block) if block_given?

  if ids_or_scope.is_a?(Array)
    list.sort_by { ids_or_scope.index(_1.id) }
  else
    list
  end
end