Class: Banzai::Filter::LabelReferenceFilter

Inherits:
AbstractReferenceFilter show all
Defined in:
lib/banzai/filter/label_reference_filter.rb

Overview

HTML filter that replaces label references with links.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractReferenceFilter

#call, data_reference, #find_object_cached, #object_class, object_class_title, #object_class_title, #object_link_filter, #object_link_text_extras, #object_link_title, object_name, object_sym, #object_sym, #project_from_ref_cache, referenced_by, #url_for_object_cached

Methods included from CrossProjectReference

#project_from_ref

Methods inherited from ReferenceFilter

#data_attribute, #each_node, #element_node?, #escape_once, #ignore_ancestor_query, #project, #reference_class, referenced_by, #replace_link_node_with_href, #replace_link_node_with_text, #replace_text_when_pattern_matches, #text_node?, user_can_reference?, user_can_see_reference?, #validate, #yield_valid_link

Class Method Details

.object_classObject


5
6
7
# File 'lib/banzai/filter/label_reference_filter.rb', line 5

def self.object_class
  Label
end

.references_in(text, pattern = Label.reference_pattern) ⇒ Object


13
14
15
16
17
# File 'lib/banzai/filter/label_reference_filter.rb', line 13

def self.references_in(text, pattern = Label.reference_pattern)
  text.gsub(pattern) do |match|
    yield match, $~[:label_id].to_i, $~[:label_name], $~[:project], $~
  end
end

Instance Method Details

#find_label(project_ref, label_id, label_name) ⇒ Object


31
32
33
34
35
36
37
# File 'lib/banzai/filter/label_reference_filter.rb', line 31

def find_label(project_ref, label_id, label_name)
  project = project_from_ref(project_ref)
  return unless project

  label_params = label_params(label_id, label_name)
  project.labels.find_by(label_params)
end

#find_object(project, id) ⇒ Object


9
10
11
# File 'lib/banzai/filter/label_reference_filter.rb', line 9

def find_object(project, id)
  project.labels.find(id)
end

#label_params(id, name) ⇒ Object

Parameters to pass to `Label.find_by` based on the given arguments

id - Integer ID to pass. If present, returns id name - String name to pass. If `id` is absent, finds by name without

surrounding quotes.

Returns a Hash.


46
47
48
49
50
51
52
# File 'lib/banzai/filter/label_reference_filter.rb', line 46

def label_params(id, name)
  if name
    { name: name.tr('"', '') }
  else
    { id: id.to_i }
  end
end

60
61
62
63
64
65
66
# File 'lib/banzai/filter/label_reference_filter.rb', line 60

def object_link_text(object, matches)
  if context[:project] == object.project
    LabelsHelper.render_colored_label(object)
  else
    LabelsHelper.render_colored_cross_project_label(object)
  end
end

#references_in(text, pattern = Label.reference_pattern) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/banzai/filter/label_reference_filter.rb', line 19

def references_in(text, pattern = Label.reference_pattern)
  text.gsub(pattern) do |match|
    label = find_label($~[:project], $~[:label_id], $~[:label_name])

    if label
      yield match, label.id, $~[:project], $~
    else
      match
    end
  end
end

#url_for_object(label, project) ⇒ Object


54
55
56
57
58
# File 'lib/banzai/filter/label_reference_filter.rb', line 54

def url_for_object(label, project)
  h = Gitlab::Routing.url_helpers
  h.namespace_project_issues_url(project.namespace, project, label_name: label.name,
                                                             only_path:  context[:only_path])
end