Sha256: 24a7b1b0ace8578e15fea74037ee2811bf40960b7271c555967060d9e52998f3

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

class WorkerPlugins::SwitchQuery < WorkerPlugins::ApplicationService
  arguments :query, :workplace

  def perform
    if resources_to_add.count.zero?
      result = WorkerPlugins::RemoveQuery.execute!(query: query, workplace: workplace)
      succeed!(
        destroyed: result.fetch(:destroyed),
        mode: :destroyed
      )
    else
      result = WorkerPlugins::AddQuery.execute!(query: query, workplace: workplace)
      succeed!(
        created: result.fetch(:created),
        mode: :created
      )
    end
  end

  def ids_added_already_query
    workplace
      .workplace_links
      .where(resource_type: model_class.name, resource_id: query_with_selected_ids)
  end

  def ids_added_already
    WorkerPlugins::SelectColumnWithTypeCast.execute!(
      column_name_to_select: :resource_id,
      column_to_compare_with: model_class.column_for_attribute(:id),
      query: ids_added_already_query
    )
  end

  def model_class
    @model_class ||= query.klass
  end

  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
    )
  end

  def resources_to_add
    @resources_to_add ||= query.where.not(id: ids_added_already)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
worker_plugins-0.0.6 app/services/worker_plugins/switch_query.rb
worker_plugins-0.0.5 app/services/worker_plugins/switch_query.rb