Module: ModsDisplay::RecordHelper

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

Instance Method Summary collapse

Instance Method Details



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

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


54
55
56
57
58
59
60
61
# File 'app/helpers/mods_display/record_helper.rb', line 54

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


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

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

rubocop:disable Layout/LineLength @private, but used in PURL currently



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/mods_display/record_helper.rb', line 74

def link_urls_and_email(val, tags: %w[a dl dd dt i b em strong br])
  val = val.gsub(%r{<[^/> ]+}) do |possible_tag|
    # Allow potentially valid HTML tags through to the sanitizer step, and HTML escape the rest
    if tags.include? possible_tag[1..]
      possible_tag
    else
      "&lt;#{possible_tag[1..]}"
    end
  end

  # http://daringfireball.net/2010/07/improved_regex_for_matching_urls
  url = %r{(?i)\b(?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\([^\s()<>]+|\([^\s()<>]+\)*\))+(?:\([^\s()<>]+|\([^\s()<>]+\)*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])}i
  # http://www.regular-expressions.info/email.html
  email = %r{[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b}i
  matches = [val.scan(url), val.scan(email)].flatten.uniq
  unless val =~ /<a/ # we'll assume that linking has alraedy occured and we don't want to double link
    matches.each do |match|
      if match =~ email
        val.gsub!(match, "<a href='mailto:#{match}'>#{match}</a>")
      else
        val.gsub!(match, "<a href='#{match}'>#{match}</a>")
      end
    end
  end

  sanitize val, tags: tags, attributes: %w[href]
end

#mods_display_content(values, delimiter = nil) ⇒ Object



10
11
12
# File 'app/helpers/mods_display/record_helper.rb', line 10

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

#mods_display_label(label) ⇒ Object



5
6
7
# File 'app/helpers/mods_display/record_helper.rb', line 5

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

#mods_display_name(values, &block) ⇒ Object



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

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

#mods_genre_field(field, &block) ⇒ Object



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

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

#mods_name_field(field) ⇒ Object



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

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

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



14
15
16
# File 'app/helpers/mods_display/record_helper.rb', line 14

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

#mods_subject_field(field, &block) ⇒ Object



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

def mods_subject_field(field, &block)
  mods_record_field(field) 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



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

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