event_filter.rb 1.2 KB
Newer Older
1 2 3 4
class EventFilter
  attr_accessor :params

  class << self
5 6
    def all
      'all'
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    end

    def push
      'push'
    end

    def merged
      'merged'
    end

    def comments
      'comments'
    end

    def team
      'team'
    end
  end

26
  def initialize(params)
27 28 29
    @params = if params
                params.dup
              else
30
                [] # EventFilter.default_filter
31 32 33
              end
  end

34
  def apply_filter(events)
35 36 37 38 39
    return events unless params.present?

    filter = params.dup
    actions = []

40 41 42 43 44 45 46 47
    case filter
    when EventFilter.push
      actions = [Event::PUSHED]
    when EventFilter.merged
      actions = [Event::MERGED]
    when EventFilter.comments
      actions = [Event::COMMENTED]
    when EventFilter.team
48
      actions = [Event::JOINED, Event::LEFT, Event::EXPIRED]
49
    when EventFilter.all
50 51 52 53 54 55 56 57
      actions = [
        Event::PUSHED,
        Event::MERGED,
        Event::COMMENTED,
        Event::JOINED,
        Event::LEFT,
        Event::EXPIRED
      ]
58 59
    end

G
Guilherme Garnier 已提交
60
    events.where(action: actions)
61 62
  end

63
  def options(key)
64 65 66 67 68 69 70 71 72 73 74
    filter = params.dup

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

    filter
  end

75
  def active?(key)
76 77 78
    params.include? key
  end
end