2018-10-06 19:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-15 09:51:16 -05:00
|
|
|
module Banzai
|
|
|
|
# Common methods for ReferenceFilters that support an optional cross-project
|
|
|
|
# reference.
|
|
|
|
module CrossProjectReference
|
|
|
|
# Given a cross-project reference string, get the Project record
|
|
|
|
#
|
2018-03-30 13:27:03 -04:00
|
|
|
# Defaults to value of `context[:project]`, or `context[:group]` if:
|
2015-12-15 09:51:16 -05:00
|
|
|
# * No reference is given OR
|
|
|
|
# * Reference given doesn't exist
|
|
|
|
#
|
|
|
|
# ref - String reference.
|
|
|
|
#
|
|
|
|
# Returns a Project, or nil if the reference can't be found
|
2017-12-01 04:21:05 -05:00
|
|
|
def parent_from_ref(ref)
|
2018-03-30 13:27:03 -04:00
|
|
|
return context[:project] || context[:group] unless ref
|
2018-10-02 22:56:51 -04:00
|
|
|
return context[:project] if context[:project]&.full_path == ref
|
2015-12-15 09:51:16 -05:00
|
|
|
|
2017-02-02 18:43:19 -05:00
|
|
|
Project.find_by_full_path(ref)
|
2015-12-15 09:51:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|