Class: Collavre::Creatives::Filters::DateFilter

Inherits:
BaseFilter
  • Object
show all
Defined in:
app/services/collavre/creatives/filters/date_filter.rb

Instance Method Summary collapse

Methods inherited from BaseFilter

#initialize

Constructor Details

This class inherits a constructor from Collavre::Creatives::Filters::BaseFilter

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


5
6
7
8
9
# File 'app/services/collavre/creatives/filters/date_filter.rb', line 5

def active?
  params[:due_before].present? ||
    params[:due_after].present? ||
    params[:has_due_date].present?
end

#matchObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/collavre/creatives/filters/date_filter.rb', line 11

def match
  # Label의 target_date 기반 필터링
  if params[:has_due_date] == "false"
    # target_date가 없는 creative
    # 1. creatives with labels that have no target_date
    # 2. creatives with no tags at all
    with_null_date = scope.left_joins(tags: :label)
                          .where(labels: { target_date: nil })
                          .pluck(:id)
    without_tags = scope.left_joins(:tags)
                        .where(tags: { id: nil })
                        .pluck(:id)
    return (with_null_date + without_tags).uniq
  end

  result = scope.joins(:tags)
                .joins("INNER JOIN labels ON tags.label_id = labels.id")

  if params[:has_due_date] == "true"
    result = result.where.not(labels: { target_date: nil })
  end

  if params[:due_before].present?
    if (due_before = safe_parse_date(params[:due_before]))
      result = result.where("labels.target_date <= ?", due_before)
    end
  end

  if params[:due_after].present?
    if (due_after = safe_parse_date(params[:due_after]))
      result = result.where("labels.target_date >= ?", due_after)
    end
  end

  result.distinct.pluck(:id)
end