Class: Interactors::Api::FilterEvents

Inherits:
Object
  • Object
show all
Includes:
Interpipe::Interactor, RolePlaying::Context
Defined in:
app/interactors/api/filter_events.rb

Overview

Filters events by page (offset, limit) also depending on a requested format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interpipe::Interactor

included, #initialize, #setup

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events


22
23
24
# File 'app/interactors/api/filter_events.rb', line 22

def events
  @events
end

Instance Method Details

#perform(events:, params: {}, format: :jsonapi) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/interactors/api/filter_events.rb', line 24

def perform(events: , params: {}, format: :jsonapi)
  @format = format

  # XXX: This is a hack for backward compatibility. Parameter "deleted"
  # used to be a Boolean and we added additional value "all".
  # In next version of API, "cancelled" and "deleted" should be two
  # separate parameters.
  param_deleted = coerce_param_deleted(params[:deleted])
  @deleted = param_deleted == :all
  @cancelled = @deleted || param_deleted

  @type = params[:event_type]
  @events = DateFilteredDataset.played_by(events) do |dataset|
    dataset
      .filter_by_date(**params.only(:from, :to, :with_original_date).to_h.rekey!)
      .then_if(hide_deleted?) { |q|
        # XXX: Temporary(?) hack until separate "cancelled" attribute is added.
        if hide_cancelled?
          q.where(deleted: false)
        else
          q.where('deleted IS FALSE OR deleted IS TRUE AND applied_schedule_exception_ids IS NOT NULL'.lit)
        end
      }
      .then_if(@type) { |q| q.where(event_type: @type) }
      .then { |q| q.order(:starts_at, :id) }
  end
end

#to_hObject


52
53
54
# File 'app/interactors/api/filter_events.rb', line 52

def to_h
  { events: events }
end