2020-05-26 11:08:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ResourceMilestoneEventFinder
|
2020-06-23 11:08:41 -04:00
|
|
|
def initialize(current_user, eventable)
|
2020-05-26 11:08:17 -04:00
|
|
|
@current_user = current_user
|
|
|
|
@eventable = eventable
|
|
|
|
end
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
# Returns the ResourceMilestoneEvents of the eventable
|
|
|
|
# visible to the user.
|
|
|
|
#
|
|
|
|
# @return ResourceMilestoneEvent::ActiveRecord_AssociationRelation
|
2020-05-26 11:08:17 -04:00
|
|
|
def execute
|
2020-06-23 11:08:41 -04:00
|
|
|
eventable.resource_milestone_events.include_relations
|
|
|
|
.where(milestone_id: readable_milestone_ids) # rubocop: disable CodeReuse/ActiveRecord
|
2020-05-26 11:08:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
attr_reader :current_user, :eventable
|
2020-05-26 11:08:17 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
def readable_milestone_ids
|
|
|
|
readable_milestones = events_milestones.select do |milestone|
|
|
|
|
parent_availabilities[key_for_parent(milestone.parent)]
|
|
|
|
end
|
2020-05-26 11:08:17 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
readable_milestones.map(&:id).uniq
|
2020-05-26 11:08:17 -04:00
|
|
|
end
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def events_milestones
|
|
|
|
@events_milestones ||= Milestone.where(id: unique_milestone_ids_from_events)
|
|
|
|
.includes(:project, :group)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-05-26 11:08:17 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
def relevant_milestone_parents
|
|
|
|
events_milestones.map(&:parent).uniq
|
2020-05-26 11:08:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def parent_availabilities
|
2020-06-23 11:08:41 -04:00
|
|
|
@parent_availabilities ||= relevant_milestone_parents.to_h do |parent|
|
2020-05-26 11:08:17 -04:00
|
|
|
[key_for_parent(parent), Ability.allowed?(current_user, :read_milestone, parent)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def unique_milestone_ids_from_events
|
|
|
|
eventable.resource_milestone_events.select(:milestone_id).distinct
|
2020-05-26 11:08:17 -04:00
|
|
|
end
|
2020-06-23 11:08:41 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-05-26 11:08:17 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
def key_for_parent(parent)
|
|
|
|
"#{parent.class.name}_#{parent.id}"
|
2020-05-26 11:08:17 -04:00
|
|
|
end
|
|
|
|
end
|