2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
# == Milestoneable concern
#
# Contains functionality related to objects that can be assigned Milestones
#
# Used by Issuable
#
module Milestoneable
extend ActiveSupport :: Concern
2023-03-04 22:38:38 +05:30
class_methods do
def milestone_releases_subquery
Milestone . joins ( :releases ) . where ( " #{ table_name } .milestone_id = milestones.id " )
end
end
2020-03-13 15:44:24 +05:30
included do
belongs_to :milestone
validate :milestone_is_valid
2020-10-24 23:57:45 +05:30
scope :any_milestone , - > { where . not ( milestone_id : nil ) }
2020-03-13 15:44:24 +05:30
scope :with_milestone , - > ( title ) { left_joins_milestones . where ( milestones : { title : title } ) }
2022-08-13 15:12:31 +05:30
scope :without_particular_milestones , - > ( titles ) { left_outer_joins ( :milestone ) . where ( " milestones.title NOT IN (?) OR milestone_id IS NULL " , titles ) }
2023-03-04 22:38:38 +05:30
scope :any_release , - > do
where ( " EXISTS (?) " , milestone_releases_subquery )
end
scope :with_release , - > ( tag , project_id ) do
where ( " EXISTS (?) " , milestone_releases_subquery . where ( releases : { tag : tag , project_id : project_id } ) )
end
scope :without_particular_release , - > ( tag , project_id ) do
where ( " EXISTS (?) " , milestone_releases_subquery . where . not ( releases : { tag : tag , project_id : project_id } ) )
end
2020-03-13 15:44:24 +05:30
scope :left_joins_milestones , - > { joins ( " LEFT OUTER JOIN milestones ON #{ table_name } .milestone_id = milestones.id " ) }
scope :order_milestone_due_desc , - > { left_joins_milestones . reorder ( Arel . sql ( 'milestones.due_date IS NULL, milestones.id IS NULL, milestones.due_date DESC' ) ) }
scope :order_milestone_due_asc , - > { left_joins_milestones . reorder ( Arel . sql ( 'milestones.due_date IS NULL, milestones.id IS NULL, milestones.due_date ASC' ) ) }
scope :without_release , - > do
joins ( " LEFT OUTER JOIN milestone_releases ON #{ table_name } .milestone_id = milestone_releases.milestone_id " )
2021-06-08 01:23:25 +05:30
. where ( milestone_releases : { release_id : nil } )
2020-03-13 15:44:24 +05:30
end
private
def milestone_is_valid
2021-04-29 21:17:54 +05:30
errors . add ( :milestone_id , 'is invalid' ) if respond_to? ( :milestone_id ) && ! milestone_available?
2020-03-13 15:44:24 +05:30
end
end
def milestone_available?
2021-04-29 21:17:54 +05:30
return true if milestone_id . blank?
2020-03-13 15:44:24 +05:30
project_id == milestone & . project_id || project . ancestors_upto . compact . include? ( milestone & . group )
end
##
# Overridden on EE module
#
def supports_milestone?
2021-03-08 18:12:59 +05:30
respond_to? ( :milestone_id )
2020-03-13 15:44:24 +05:30
end
end
2021-06-08 01:23:25 +05:30
Milestoneable . prepend_mod_with ( 'Milestoneable' )