BigW Consortium Gitlab

event_filter.rb 1.25 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
    end

    def push
      'push'
    end

    def merged
      'merged'
    end

17 18 19 20
    def issue
      'issue'
    end

21 22 23 24 25 26 27 28 29
    def comments
      'comments'
    end

    def team
      'team'
    end
  end

30
  def initialize(params)
31 32 33
    @params = if params
                params.dup
              else
34
                [] # EventFilter.default_filter
35 36 37
              end
  end

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

41
    case params
42
    when EventFilter.push
43
      events.where(action: Event::PUSHED)
44
    when EventFilter.merged
45
      events.where(action: Event::MERGED)
46
    when EventFilter.comments
47
      events.where(action: Event::COMMENTED)
48
    when EventFilter.team
49 50 51
      events.where(action: [Event::JOINED, Event::LEFT, Event::EXPIRED])
    when EventFilter.issue
      events.where(action: [Event::CREATED, Event::UPDATED, Event::CLOSED, Event::REOPENED])
52 53 54
    end
  end

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

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

    filter
  end

67
  def active?(key)
68 69 70 71 72
    if params.present?
      params.include? key
    else
      key == EventFilter.all
    end
73 74
  end
end