Module: ModsDisplay::Helpers::RecordHelper

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

Instance Method Summary collapse

Instance Method Details

#display_content_field(field) ⇒ Object



6
7
8
9
10
# File 'lib/mods_display/helpers/record_helper.rb', line 6

def display_content_field(field)
  return unless field.respond_to?(:label, :values) && field.values.any?(&:present?)

  (field.label) + display_content_values(field.values)
end

#display_content_label(label) ⇒ Object



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

def (label)
   :dt, label
end

#display_content_values(values) ⇒ Object



16
17
18
19
20
# File 'lib/mods_display/helpers/record_helper.rb', line 16

def display_content_values(values)
  values.map do |value|
     :dd, value
  end.join('').html_safe
end


84
85
86
87
# File 'lib/mods_display/helpers/record_helper.rb', line 84

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


89
90
91
92
93
94
95
96
97
98
# File 'lib/mods_display/helpers/record_helper.rb', line 89

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


100
101
102
103
104
105
106
# File 'lib/mods_display/helpers/record_helper.rb', line 100

def link_to_mods_subject(subject, buffer, &block)
  subject_text = subject.respond_to?(:name) ? subject.name : subject
  link = block_given? ? 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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mods_display/helpers/record_helper.rb', line 109

def link_urls_and_email(val)
  val = val.dup
  # 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
  val
end

#mods_display_content(values, delimiter = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mods_display/helpers/record_helper.rb', line 26

def mods_display_content(values, delimiter = nil)
  if delimiter
    (:dd, values.map do |value|
      link_urls_and_email(value) if value.present?
    end.compact.join(delimiter).html_safe)
  else
    Array[values].flatten.map do |value|
      (:dd, link_urls_and_email(value.to_s).html_safe) if value.present?
    end.join.html_safe
  end
end

#mods_display_label(label) ⇒ Object



22
23
24
# File 'lib/mods_display/helpers/record_helper.rb', line 22

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

#mods_display_name(names, &block) ⇒ Object



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

def mods_display_name(names, &block)
  names.map do |name|
    (:dd) do
      block_given? ? yield(name.name) : name.name
    end
  end.join.html_safe
end

#mods_genre_field(genre, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/mods_display/helpers/record_helper.rb', line 74

def mods_genre_field(genre, &block)
  return unless genre.values.any?(&:present?)

  fields = genre.values.map do |genre_line|
     :dd, link_mods_genres(genre_line, &block)
  end

  mods_display_label(genre.label) + safe_join(fields)
end

#mods_name_field(field, &block) ⇒ Object



44
45
46
47
48
# File 'lib/mods_display/helpers/record_helper.rb', line 44

def mods_name_field(field, &block)
  return unless field.respond_to?(:label, :values) && field.values.any?(&:present?)

  mods_display_label(field.label) + mods_display_name(field.values, &block)
end

#mods_record_field(field, delimiter = nil) ⇒ Object



38
39
40
41
42
# File 'lib/mods_display/helpers/record_helper.rb', line 38

def mods_record_field(field, delimiter = nil)
  return unless field.respond_to?(:label, :values) && field.values.any?(&:present?)

  mods_display_label(field.label) + mods_display_content(field.values, delimiter)
end

#mods_subject_field(subject, &block) ⇒ Object



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

def mods_subject_field(subject, &block)
  return unless subject.values.any?(&:present?)

  fields = subject.values.map do |subject_line|
     :dd, safe_join(link_mods_subjects(subject_line, &block), ' > ')
  end

  (mods_display_label(subject.label) + safe_join(fields))
end

#sanitize_mods_name_label(label) ⇒ Object

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



60
61
62
# File 'lib/mods_display/helpers/record_helper.rb', line 60

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