event_filter.rb 1.14 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 48 49 50
    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]
51 52
    end

53
    events.where(action: actions)
54 55
  end

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

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

    filter
  end

68
  def active?(key)
69 70 71
    params.include? key
  end
end