summaryrefslogtreecommitdiff
path: root/lib/event_filter.rb
blob: 96e70e37e8fe89af6d0abf02a207c37c5825fa54 (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
class EventFilter
  attr_accessor :params

  class << self
    def all
      'all'
    end

    def push
      'push'
    end

    def merged
      'merged'
    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 unless params.present?

    filter = params.dup
    actions = []

    case filter
    when EventFilter.push
      actions = [Event::PUSHED]
    when EventFilter.merged
      actions = [Event::MERGED]
    when EventFilter.comments
      actions = [Event::COMMENTED]
    when EventFilter.team
      actions = [Event::JOINED, Event::LEFT]
    when EventFilter.all
      actions = [Event::PUSHED, Event::MERGED, Event::COMMENTED, Event::JOINED, Event::LEFT]
    end

    events.where(action: actions)
  end

  def options(key)
    filter = params.dup

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

    filter
  end

  def active?(key)
    params.include? key
  end
end