Sha256: d8c10d7e10d1a4296f59a7c08c630b24a28324b642af6c8fe2208939bf1ec899

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

class RailsExecution::Tasks::FilterService

  def initialize(tasks_query_object, params, current_user_id = nil)
    @tasks_query_object = tasks_query_object
    @params = params
    @current_user_id = current_user_id
  end

  def call
    filter_by_owner
    filter_by_label
    filter_by_keyword
    order_by_recently_updated

    return @tasks_query_object.descending
  end

  private

  attr_reader :params
  attr_reader :current_user_id

  def filter_by_owner
    if params[:my_task] == '1' && current_user_id
      @tasks_query_object = @tasks_query_object.where(owner_id: current_user_id)
    elsif params[:owner_id].present?
      @tasks_query_object = @tasks_query_object.where(owner_id: params[:owner_id])
    end
  end

  def filter_by_label
    return if params[:label_id].blank?

    filtered_task_ids = RailsExecution::TaskLabel.where(label_id: params[:label_id]).select(:task_id)
    @tasks_query_object = @tasks_query_object.where(id: filtered_task_ids)
  end

  def filter_by_keyword
    return if params[:keyword].blank?

    @tasks_query_object = @tasks_query_object.where("rails_execution_tasks.title LIKE ?", "%#{params[:keyword]}%")
  end

  def order_by_recently_updated
    sort_by = params[:recently_updated] == 'asc' ? { updated_at: :asc } : { updated_at: :desc }
    @tasks_query_object = @tasks_query_object.order(sort_by)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_execution-0.1.12 app/services/rails_execution/tasks/filter_service.rb