Class: WorkerPlugins::Workplace

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/worker_plugins/workplace.rb

Instance Method Summary collapse

Instance Method Details



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

def add_links_to_objects(objects)
  require 'active-record-transactioner'

  # Cache inserted objects.
  inserted_ids = load_inserted_ids

  # Add given objects through transactions.
  ActiveRecordTransactioner.new do |trans|
    stream_each(objects) do |object|
      class_name = object.class.name.to_s
      inserted_ids[class_name] ||= {}

      unless inserted_ids[class_name].key?(object.id)
        inserted_ids[class_name][object.id] = true
        link = WorkerPlugins::WorkplaceLink.new(
          workplace: self,
          resource: object
        )
        trans.save!(link)
      end
    end
  end

  return
end

#each_query_for_resourcesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/worker_plugins/workplace.rb', line 54

def each_query_for_resources
  workplace_links.group('worker_plugins_workplace_links.resource_type').order('worker_plugins_workplace_links.id').each do |workplace_link|
    resource_type = workplace_link.resource_type
    constant = Object.const_get(resource_type)
    ids = workplace_links.select(:resource_id).where(resource_type: workplace_link.resource_type).map(&:resource_id)

    ids.each_slice(500) do |ids_slice|
      query = constant.where(id: ids_slice)

      yield(query: query, resource_type: resource_type)
    end
  end
end

#each_resource(args = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/worker_plugins/workplace.rb', line 34

def each_resource(args = {})
  count = 0

  links_query = workplace_links.group('worker_plugins_workplace_links.resource_type').order('worker_plugins_workplace_links.id')
  links_query = links_query.where(resource_type: args[:types]) if args[:types]

  links_query.each do |workplace_link|
    constant = Object.const_get(workplace_link.resource_type)
    ids = workplace_links.select(:resource_id).where(resource_type: workplace_link.resource_type).map(&:resource_id)

    ids.each_slice(500) do |ids_slice|
      constant.where(id: ids_slice).each do |resource|
        yield resource
        count += 1
        return if args[:limit] && count >= args[:limit]
      end
    end
  end
end

#truncateObject



68
69
70
# File 'app/models/worker_plugins/workplace.rb', line 68

def truncate
  workplace_links.delete_all
end