summaryrefslogtreecommitdiff
path: root/lib/event_filter.rb
blob: 515095af1c25f33ae93c4c0f0cd1e2443fc5bacc (plain)
1
2
3
4
5
6
7
8
9
10
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class EventFilter
  attr_accessor :params

  class << self
    def all
      'all'
    end

    def push
      'push'
    end

    def merged
      'merged'
    end

    def issue
      'issue'
    end

    def comments
      'comments'
    end

    def team
      'team'
    end
  end

  def initialize(params)
    @params = if params
                params.dup
              else
                [] # EventFilter.default_filter
              end
  end

  def apply_filter(events)
    return events if params.blank? || params == EventFilter.all

    case params
    when EventFilter.push
      events.where(action: Event::PUSHED)
    when EventFilter.merged
      events.where(action: Event::MERGED)
    when EventFilter.comments
      events.where(action: Event::COMMENTED)
    when EventFilter.team
      events.where(action: [Event::JOINED, Event::LEFT, Event::EXPIRED])
    when EventFilter.issue
      events.where(action: [Event::CREATED, Event::UPDATED, Event::CLOSED, Event::REOPENED])
    end
  end

  def options(key)
    filter = params.dup

    if filter.include? key
      filter.delete key
    else
      filter << key
    end

    filter
  end

  def active?(key)
    if params.present?
      params.include? key
    else
      key == EventFilter.all
    end
  end
end