Module: DocFormat

Includes:
TableFormat
Included in:
DocsHelper
Defined in:
app/helpers/doc_format.rb

Instance Method Summary collapse

Methods included from TableFormat

#categoryView, #dateView, #facetLinkGen, #facetPrepare, #getText, #get_highlighted_categories, #isHighlighted?, #linkedFacets, #longTextView, #notEmpty?, #pictureView, #processType, #shortTextView, #showByType, #tableItems, #titleView

Instance Method Details

#format_text(text) ⇒ Object

Replaces newlines with br


52
53
54
# File 'app/helpers/doc_format.rb', line 52

def format_text(text)
  text.gsub("\n", "<br />")
end

#getFirstDocObject

Gets the first doc in list, useful for getting item fields


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

def getFirstDoc
  return @link_type["Link Type"] == "mult_items" ? @docs.first["_source"] : @doc 
end

#gf(item, field) ⇒ Object

Gets the field with _source


64
65
66
# File 'app/helpers/doc_format.rb', line 64

def gf(item, field)
  return item["_source"][field]
end

#handleLinkFields(doc, field) ⇒ Object

Handles both single links and arrays of links


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/doc_format.rb', line 89

def handleLinkFields(doc, field)
  # Get link field content
  links = doc[field["Field Name"]]
  outstr = ''

  # Handle multiple links
  if links.is_a?(Array)
    links.each do |u|
      outstr = outstr + prepareLink(u.to_s) + '<br />'
    end
  else # Handle single links
    outstr += prepareLink(links.to_s)
  end

  return outstr
end

#item_field_sort(items, sort_field) ⇒ Object

Sorts item field by creation date


57
58
59
60
61
# File 'app/helpers/doc_format.rb', line 57

def item_field_sort(items, sort_field)
  return sorted = items.sort do |b, a|
    gf(a, sort_field) && gf(b, sort_field) ? gf(a, sort_field) <=> gf(b, sort_field) : gf(a, sort_field) ? -1 : 1
  end
end

#prepareField(field, doc) ⇒ Object

Wraps field name in tags and gets doc text for showing in doc


107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/doc_format.rb', line 107

def prepareField(field, doc)
  outstr = ''

  # Handle picture fields
  if field["Display Type"] == "Picture"
    outstr += preparePictureFields(doc, field)
  else # Handle fields that need a field name first
    outstr += prepareTextFields(doc, field)
  end
end

#prepareFieldName(field) ⇒ Object

Format field names


79
80
81
# File 'app/helpers/doc_format.rb', line 79

def prepareFieldName(field)
  return '<strong>'+field["Human Readable Name"]+': </strong>'
end

#prepareIcon(f) ⇒ Object

Prepare to print icon


47
48
49
# File 'app/helpers/doc_format.rb', line 47

def prepareIcon(f)
  return image_tag(f["Icon"]+"-24.png")+' '
end

Generate a link


69
70
71
# File 'app/helpers/doc_format.rb', line 69

def prepareLink(this_url)
  return link_to this_url, this_url, target: "_blank"
end

#prepareLongText(doc, field) ⇒ Object

Format longer text fields


84
85
86
# File 'app/helpers/doc_format.rb', line 84

def prepareLongText(doc, field)
  return raw(format_text(doc[field["Field Name"]])) if doc[field["Field Name"]]
end

#preparePictureFields(doc, field) ⇒ Object

Prepare a picture field


74
75
76
# File 'app/helpers/doc_format.rb', line 74

def preparePictureFields(doc, field)
  return image_tag(doc[field["Field Name"]], :class => "picture")
end

#prepareTextFields(doc, field) ⇒ Object

Handles text fields that need a field name first


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/helpers/doc_format.rb', line 119

def prepareTextFields(doc, field)
  outstr = prepareFieldName(field)

  # Long text
  if checkIfX(field, @truncated_fields)
    outstr += prepareLongText(doc, field)

  # Links
  elsif field["Display Type"] == "Link"
    outstr += handleLinkFields(doc, field)

  else # Any other text
    outstr += doc[field["Field Name"]].to_s
  end

  return outstr
end

#printData(doc, print_conditions, print_type, dataspec, fields) ⇒ Object

Prints the fields in the sidebar or text


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/doc_format.rb', line 5

def printData(doc, print_conditions, print_type, dataspec, fields)
  output = ''

  # Go through sorted list
  sortFields(fields, dataspec.field_info).each do |field|
    f = getFieldDetails(field, dataspec.field_info) # Full field details
    
    # Print if field it fulfills conditions
    if print_conditions.call(f, doc)
      case print_type
      when "fields"
        output += printFields(f, doc).to_s
      when "sidebar"
        output += printSidebarItem(f, doc).to_s
      end
    end
  end
  
  return output
end

#printFields(f, doc_content) ⇒ Object

Prints the field name and content if filled in


27
28
29
30
31
32
# File 'app/helpers/doc_format.rb', line 27

def printFields(f, doc_content)
  field_content = doc_content[f["Field Name"]]
  if field_content != nil && !field_content.to_s.empty? && field_content != "[]"
    return '<p>'+raw(prepareField(f, doc_content))+'</p>'
  end
end

#printSidebarItem(f, doc) ⇒ Object

Prints the sidebar item


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

def printSidebarItem(f, doc)
  if !doc[f["Field Name"]].empty?
    return '<p>'+prepareIcon(f)+prepareFieldName(f)+linkedFacets(doc[f["Field Name"]], f["Field Name"], {})+'</p>'
  end
end