BigW Consortium Gitlab

event_filter.rb 1.06 KB
Newer Older
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
class EventFilter
  attr_accessor :params

  class << self
    def default_filter
      %w{ push issues merge_requests team}
    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 30 31 32 33
    @params = if params
                params.dup
              else
                []#EventFilter.default_filter
              end
  end

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

    filter = params.dup

    actions = []
40 41
    actions << Event::PUSHED if filter.include? 'push'
    actions << Event::MERGED if filter.include? 'merged'
42 43

    if filter.include? 'team'
44 45
      actions << Event::JOINED
      actions << Event::LEFT
46 47
    end

48
    actions << Event::COMMENTED if filter.include? 'comments'
49

50
    events.where(action: actions)
51 52
  end

53
  def options(key)
54 55 56 57 58 59 60 61 62 63 64
    filter = params.dup

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

    filter
  end

65
  def active?(key)
66 67 68
    params.include? key
  end
end