Module: ModsDisplay::RecordHelper

Defined in:
app/helpers/mods_display/record_helper.rb

Defined Under Namespace

Classes: MetadataScrubber

Instance Method Summary collapse

Instance Method Details

#format_mods_html(val, tags: %w[a dl dd dt i b em strong cite br],, field: nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/mods_display/record_helper.rb', line 103

def format_mods_html(val, tags: %w[a dl dd dt i b em strong cite br], field: nil)
  scrubber = MetadataScrubber.new
  scrubber.tags = tags

  formatted_val = Loofah.fragment(val).scrub!(scrubber).to_s

  formatted_val = auto_link(formatted_val) unless formatted_val =~ /<a/ # we'll assume that linking has alraedy occured and we don't want to double link

  # Martin Wong data has significant linebreaks in abstracts and notes that we want
  # to preserve and display in HTML.
  #
  # Some user-deposited items have complex use and reproduction statements that also
  # need linebreaks preserved.
  #
  # See https://github.com/sul-dlss/mods_display/issues/78
  # and https://github.com/sul-dlss/mods_display/issues/145
  simple_formatted_fields = [ModsDisplay::Abstract, ModsDisplay::Contents, ModsDisplay::Note, ModsDisplay::AccessCondition]
  if simple_formatted_fields.any? { |klass| field&.field.is_a? klass } && formatted_val.include?("\n")
    simple_format(formatted_val, {}, sanitize: false)
  else
    formatted_val.html_safe
  end
end


71
72
73
74
# File 'app/helpers/mods_display/record_helper.rb', line 71

def link_mods_genres(genre, &block)
  link_buffer = []
  link_to_mods_subject(genre, link_buffer, &block)
end


77
78
79
80
81
82
83
84
# File 'app/helpers/mods_display/record_helper.rb', line 77

def link_mods_subjects(subjects, &block)
  link_buffer = []
  linked_subjects = []
  subjects.each do |subject|
    linked_subjects << link_to_mods_subject(subject, link_buffer, &block) if subject.present?
  end
  linked_subjects
end


87
88
89
90
91
92
93
# File 'app/helpers/mods_display/record_helper.rb', line 87

def link_to_mods_subject(subject, buffer = [])
  subject_text = subject.respond_to?(:name) ? subject.name : subject
  link = block_given? ? capture { yield(subject_text, buffer) } : subject_text
  buffer << subject_text.strip
  link << " (#{subject.roles.join(', ')})" if subject.respond_to?(:roles) && subject.roles.present?
  link
end

@private, but used in PURL currently



128
129
130
# File 'app/helpers/mods_display/record_helper.rb', line 128

def link_urls_and_email(val, tags: %w[a dl dd dt i b em strong cite br])
  format_mods_html(val, tags: tags)
end

#mods_display_content(values, delimiter = nil) ⇒ Object



12
13
14
# File 'app/helpers/mods_display/record_helper.rb', line 12

def mods_display_content(values, delimiter = nil)
  mods_record_field ModsDisplay::Values.new(values: Array(values)), delimiter
end

#mods_display_label(label) ⇒ Object



7
8
9
# File 'app/helpers/mods_display/record_helper.rb', line 7

def mods_display_label(label)
  (:dt, label.delete(':')) + "\n".html_safe
end

#mods_display_name(values, &block) ⇒ Object



35
36
37
# File 'app/helpers/mods_display/record_helper.rb', line 35

def mods_display_name(values, &block)
  mods_name_field(ModsDisplay::Values.new(values: Array(values)), &block)
end

#mods_genre_field(field, &block) ⇒ Object



58
59
60
61
62
# File 'app/helpers/mods_display/record_helper.rb', line 58

def mods_genre_field(field, &block)
  mods_record_field(field) do |genre_line|
    link_to_mods_subject(genre_line, &block)
  end
end

#mods_genre_row_field(field, delimiter: nil, &block) ⇒ Object



64
65
66
67
68
# File 'app/helpers/mods_display/record_helper.rb', line 64

def mods_genre_row_field(field, delimiter: nil, &block)
  mods_record_row_field(field, delimiter: delimiter) do |genre_line|
    link_to_mods_subject(genre_line, &block)
  end
end

#mods_name_field(field) ⇒ Object

this returns a role’s label and the display names for ModsDisplay:Name:Person



29
30
31
32
33
# File 'app/helpers/mods_display/record_helper.rb', line 29

def mods_name_field(field)
  mods_record_field(field) do |name|
    block_given? ? capture { yield(name.name) } : name.name
  end
end

#mods_record_definition_field(field, delimiter: nil, label_html_attributes: {}, &block) ⇒ Object



20
21
22
# File 'app/helpers/mods_display/record_helper.rb', line 20

def mods_record_definition_field(field, delimiter: nil, label_html_attributes: {}, &block)
  render ModsDisplay::FieldComponent.new(field: field, delimiter: delimiter, label_html_attributes: label_html_attributes, value_transformer: block)
end

#mods_record_field(field, delimiter = nil, component: ModsDisplay::FieldComponent, &block) ⇒ Object



16
17
18
# File 'app/helpers/mods_display/record_helper.rb', line 16

def mods_record_field(field, delimiter = nil, component: ModsDisplay::FieldComponent, &block)
  render component.new(field: field, delimiter: delimiter, value_transformer: block)
end

#mods_record_row_field(field, delimiter: ', ', &block) ⇒ Object



24
25
26
# File 'app/helpers/mods_display/record_helper.rb', line 24

def mods_record_row_field(field, delimiter: ', ', &block)
  render ModsDisplay::RowFieldComponent.new(field: field, delimiter: delimiter, value_transformer: block)
end

#mods_subject_field(field, &block) ⇒ Object



46
47
48
49
50
# File 'app/helpers/mods_display/record_helper.rb', line 46

def mods_subject_field(field, &block)
  mods_record_field(field) do |subject_line|
    safe_join(link_mods_subjects(subject_line, &block), ' > ')
  end
end

#mods_subject_row_field(field, delimiter: nil, &block) ⇒ Object



52
53
54
55
56
# File 'app/helpers/mods_display/record_helper.rb', line 52

def mods_subject_row_field(field, delimiter: nil, &block)
  mods_record_row_field(field, delimiter: delimiter) do |subject_line|
    safe_join(link_mods_subjects(subject_line, &block), ' > ')
  end
end

#sanitize_mods_name_label(label) ⇒ Object

We need this to remove the ending “:” from the role labels only in data from mods_display



42
43
44
# File 'app/helpers/mods_display/record_helper.rb', line 42

def sanitize_mods_name_label(label)
  label.sub(/:$/, '')
end