2018-10-06 23:10:08 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-15 14:51:16 +00: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 17:27:03 +00:00
|
|
|
# Defaults to value of `context[:project]`, or `context[:group]` if:
|
2015-12-15 14:51:16 +00: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 09:21:05 +00:00
|
|
|
def parent_from_ref(ref)
|
2018-03-30 17:27:03 +00:00
|
|
|
return context[:project] || context[:group] unless ref
|
2018-10-03 02:56:51 +00:00
|
|
|
return context[:project] if context[:project]&.full_path == ref
|
2015-12-15 14:51:16 +00:00
|
|
|
|
2017-02-02 23:43:19 +00:00
|
|
|
Project.find_by_full_path(ref)
|
2015-12-15 14:51:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|