2020-01-03 04:07:33 -05:00
# frozen_string_literal: true
# == Milestoneable concern
#
# Contains functionality related to objects that can be assigned Milestones
#
# Used by Issuable
#
module Milestoneable
extend ActiveSupport :: Concern
included do
belongs_to :milestone
validate :milestone_is_valid
2020-07-21 17:09:12 -04:00
scope :any_milestone , - > { where . not ( milestone_id : nil ) }
2020-01-03 04:07:33 -05:00
scope :with_milestone , - > ( title ) { left_joins_milestones . where ( milestones : { title : title } ) }
2022-07-06 14:08:29 -04:00
scope :without_particular_milestones , - > ( titles ) { left_outer_joins ( :milestone ) . where ( " milestones.title NOT IN (?) OR milestone_id IS NULL " , titles ) }
2020-01-03 04:07:33 -05:00
scope :any_release , - > { joins_milestone_releases }
2022-10-25 05:10:14 -04:00
scope :with_release , - > ( tag , project_id ) { joins_milestone_releases . where ( milestones : { releases : { tag : tag , project_id : project_id } } ) }
2021-10-20 14:12:31 -04:00
scope :without_particular_release , - > ( tag , project_id ) { joins_milestone_releases . where . not ( milestones : { releases : { tag : tag , project_id : project_id } } ) }
2020-01-03 04:07:33 -05:00
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-04-26 08:09:44 -04:00
. where ( milestone_releases : { release_id : nil } )
2020-01-03 04:07:33 -05:00
end
scope :joins_milestone_releases , - > do
joins ( " JOIN milestone_releases ON #{ table_name } .milestone_id = milestone_releases.milestone_id
JOIN releases ON milestone_releases . release_id = releases . id " ).distinct
end
private
def milestone_is_valid
2021-04-15 11:09:11 -04:00
errors . add ( :milestone_id , 'is invalid' ) if respond_to? ( :milestone_id ) && ! milestone_available?
2020-01-03 04:07:33 -05:00
end
end
def milestone_available?
2021-04-15 11:09:11 -04:00
return true if milestone_id . blank?
2020-01-03 04:07:33 -05:00
project_id == milestone & . project_id || project . ancestors_upto . compact . include? ( milestone & . group )
end
##
# Overridden on EE module
#
def supports_milestone?
2021-01-15 10:10:31 -05:00
respond_to? ( :milestone_id )
2020-01-03 04:07:33 -05:00
end
end
2021-05-11 17:10:21 -04:00
Milestoneable . prepend_mod_with ( 'Milestoneable' )