BigW Consortium Gitlab

event_filter.rb 1.07 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39
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

  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 = []
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

    events = 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