2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-29 01:49:17 -04:00
|
|
|
class EventsFinder
|
2017-12-11 09:21:06 -05:00
|
|
|
prepend FinderMethods
|
|
|
|
prepend FinderWithCrossProjectAccess
|
2017-05-29 01:49:17 -04:00
|
|
|
attr_reader :source, :params, :current_user
|
|
|
|
|
2017-12-11 09:21:06 -05:00
|
|
|
requires_cross_project_access unless: -> { source.is_a?(Project) }
|
|
|
|
|
2017-05-29 01:49:17 -04:00
|
|
|
# Used to filter Events
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# source - which user or project to looks for events on
|
|
|
|
# current_user - only return events for projects visible to this user
|
|
|
|
# params:
|
|
|
|
# action: string
|
|
|
|
# target_type: string
|
|
|
|
# before: datetime
|
|
|
|
# after: datetime
|
|
|
|
#
|
|
|
|
def initialize(params = {})
|
|
|
|
@source = params.delete(:source)
|
|
|
|
@current_user = params.delete(:current_user)
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
events = source.events
|
|
|
|
|
|
|
|
events = by_current_user_access(events)
|
|
|
|
events = by_action(events)
|
|
|
|
events = by_target_type(events)
|
|
|
|
events = by_created_at_before(events)
|
|
|
|
events = by_created_at_after(events)
|
|
|
|
|
|
|
|
events
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_current_user_access(events)
|
2018-08-27 11:31:01 -04:00
|
|
|
events.merge(ProjectsFinder.new(current_user: current_user).execute) # rubocop: disable CodeReuse/Finder
|
2017-06-21 09:48:12 -04:00
|
|
|
.joins(:project)
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_action(events)
|
|
|
|
return events unless Event::ACTIONS[params[:action]]
|
|
|
|
|
|
|
|
events.where(action: Event::ACTIONS[params[:action]])
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_target_type(events)
|
|
|
|
return events unless Event::TARGET_TYPES[params[:target_type]]
|
|
|
|
|
|
|
|
events.where(target_type: Event::TARGET_TYPES[params[:target_type]])
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_created_at_before(events)
|
|
|
|
return events unless params[:before]
|
|
|
|
|
|
|
|
events.where('events.created_at < ?', params[:before].beginning_of_day)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
def by_created_at_after(events)
|
|
|
|
return events unless params[:after]
|
|
|
|
|
|
|
|
events.where('events.created_at > ?', params[:after].end_of_day)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|