Class: WorkerPlugins::RemoveQuery

Inherits:
ApplicationService show all
Defined in:
app/services/worker_plugins/remove_query.rb

Instance Method Summary collapse

Methods inherited from ApplicationService

#db_now_value, #mysql?, #postgres?, #quote, #quote_column, #quote_table, #relation_unscoped?, #sqlite?

Instance Method Details



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/worker_plugins/remove_query.rb', line 8

def links_scope
  scope = workplace.workplace_links.where(resource_type: model_class.name)
  # When the caller's query applies no scoping, the `resource_id IN (SELECT
  # ... FROM <target_table>)` subquery would materialize every row of the
  # target model — the `resource_type = ?` filter alone is enough. Orphaned
  # links (whose resource row has since been deleted) are deleted alongside
  # live ones, which matches caller intent ("remove everything matching")
  # and is the correct thing to do with dead references anyway.
  return scope if relation_unscoped?(@query)

  scope.where(resource_id: query_with_selected_ids)
end

#model_classObject



21
22
23
# File 'app/services/worker_plugins/remove_query.rb', line 21

def model_class
  query.klass
end

#performObject



4
5
6
# File 'app/services/worker_plugins/remove_query.rb', line 4

def perform
  succeed!(affected_count: links_scope.delete_all)
end

#query_with_selected_idsObject



25
26
27
28
29
30
31
# File 'app/services/worker_plugins/remove_query.rb', line 25

def query_with_selected_ids
  WorkerPlugins::SelectColumnWithTypeCast.execute!(
    column_name_to_select: :id,
    column_to_compare_with: WorkerPlugins::WorkplaceLink.column_for_attribute(:resource_id),
    query: query.except(:order)
  )
end