Class: Todos::Destroy::UnauthorizedFeaturesService

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/todos/destroy/unauthorized_features_service.rb

Constant Summary collapse

BATCH_SIZE =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_id, user_id = nil) ⇒ UnauthorizedFeaturesService

Returns a new instance of UnauthorizedFeaturesService.



10
11
12
13
# File 'app/services/todos/destroy/unauthorized_features_service.rb', line 10

def initialize(project_id, user_id = nil)
  @project_id = project_id
  @user_id = user_id
end

Instance Attribute Details

#project_idObject (readonly)

Returns the value of attribute project_id.



6
7
8
# File 'app/services/todos/destroy/unauthorized_features_service.rb', line 6

def project_id
  @project_id
end

#user_idObject (readonly)

Returns the value of attribute user_id.



6
7
8
# File 'app/services/todos/destroy/unauthorized_features_service.rb', line 6

def user_id
  @user_id
end

Instance Method Details

#executeObject

rubocop: disable CodeReuse/ActiveRecord



16
17
18
19
20
21
22
23
24
25
# File 'app/services/todos/destroy/unauthorized_features_service.rb', line 16

def execute
  return if user_id && authorized_users.where(user_id: user_id).exists?

  related_todos.each_batch(of: BATCH_SIZE) do |batch|
    pending_delete = without_authorized(batch).includes(:target, :user).reject do |todo|
      Ability.allowed?(todo.user, :read_todo, todo, scope: :user)
    end
    Todo.where(id: pending_delete).delete_all if pending_delete.present?
  end
end