Class: WorkerPlugins::Workplace

Inherits:
ApplicationRecord show all
Defined in:
app/models/worker_plugins/workplace.rb

Instance Method Summary collapse

Instance Method Details

#each_query_for_resourcesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/worker_plugins/workplace.rb', line 31

def each_query_for_resources
  resource_ids_by_type = workplace_links
    .order(:id)
    .pluck(:resource_type, :resource_id)
    .each_with_object({}) do |(resource_type, resource_id), grouped_ids|
      grouped_ids[resource_type] ||= []
      grouped_ids[resource_type] << resource_id
    end

  resource_ids_by_type.each do |resource_type, ids|
    constant = Object.const_get(resource_type)

    ids.each_slice(500) do |ids_slice|
      yield(query: constant.where(id: ids_slice), resource_type:)
    end
  end
end

#each_resource(limit: nil, types: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/worker_plugins/workplace.rb', line 11

def each_resource(limit: nil, types: nil)
  count = 0

  links_query = workplace_links.order(:id)
  links_query = links_query.where(resource_type: types) if types
  links_query.find_in_batches do |workplace_links|
    resources_by_type_and_id = load_resources_by_type_and_id(workplace_links)

    workplace_links.each do |workplace_link|
      resource = resources_by_type_and_id
        .fetch(workplace_link.resource_type)[workplace_link.resource_id.to_s]
      next unless resource

      yield resource
      count += 1
      return if limit && count >= limit # rubocop:disable Lint/NonLocalExitFromIterator
    end
  end
end

#truncateObject



49
50
51
# File 'app/models/worker_plugins/workplace.rb', line 49

def truncate
  workplace_links.delete_all
end