2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ResourceMilestoneEventFinder
|
2020-07-28 23:09:34 +05:30
|
|
|
def initialize(current_user, eventable)
|
2020-06-23 00:09:42 +05:30
|
|
|
@current_user = current_user
|
|
|
|
@eventable = eventable
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
# Returns the ResourceMilestoneEvents of the eventable
|
|
|
|
# visible to the user.
|
|
|
|
#
|
|
|
|
# @return ResourceMilestoneEvent::ActiveRecord_AssociationRelation
|
2020-06-23 00:09:42 +05:30
|
|
|
def execute
|
2020-07-28 23:09:34 +05:30
|
|
|
eventable.resource_milestone_events.include_relations
|
|
|
|
.where(milestone_id: readable_milestone_ids) # rubocop: disable CodeReuse/ActiveRecord
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
attr_reader :current_user, :eventable
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def readable_milestone_ids
|
|
|
|
readable_milestones = events_milestones.select do |milestone|
|
|
|
|
parent_availabilities[key_for_parent(milestone.parent)]
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
readable_milestones.map(&:id).uniq
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
# 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-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def relevant_milestone_parents
|
|
|
|
events_milestones.map(&:parent).uniq
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def parent_availabilities
|
2020-07-28 23:09:34 +05:30
|
|
|
@parent_availabilities ||= relevant_milestone_parents.to_h do |parent|
|
2020-06-23 00:09:42 +05:30
|
|
|
[key_for_parent(parent), Ability.allowed?(current_user, :read_milestone, parent)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def unique_milestone_ids_from_events
|
|
|
|
eventable.resource_milestone_events.select(:milestone_id).distinct
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def key_for_parent(parent)
|
|
|
|
"#{parent.class.name}_#{parent.id}"
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|