Module: TableFormat

Included in:
DocFormat, DocsHelper
Defined in:
app/helpers/table_format.rb

Instance Method Summary collapse

Instance Method Details

#categoryView(t, doc, dataspec, full_doc) ⇒ Object

Prepares facet view


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/helpers/table_format.rb', line 125

def categoryView(t, doc, dataspec, full_doc)
  # Get list of categories that are highlighted (and highlighted section)
  highlighted_categories = get_highlighted_categories(full_doc, t["Field Name"])
  
  output = ''
  
  if dataspec.facet_fields.include?(t["Field Name"])
    # Gen all links
    facet_links = linkedFacets(doc[t["Field Name"]], t["Field Name"], highlighted_categories)

    # Generate full field with all links
    if facet_links != "" && !facet_links.empty?
      output += facetPrepare(t, facet_links)
    end
  end

  return output
end

#dateView(t, doc) ⇒ Object

Prepares date view


105
106
107
# File 'app/helpers/table_format.rb', line 105

def dateView(t, doc)
  return '<span class="date">'+ t["Human Readable Name"]+ ': <span class="list_date">'+doc[t["Field Name"]]+'</span></span>'
end

#facetLinkGen(field_vals, field_name, highlighted_categories) ⇒ Object

Generate facet link for category results


43
44
45
46
47
48
49
# File 'app/helpers/table_format.rb', line 43

def facetLinkGen(field_vals, field_name, highlighted_categories)
  # Bold category name if highlighted
  facet_name = highlighted_categories[field_vals] ? raw(highlighted_categories[field_vals]) : field_vals.strip

  # Return link
  return link_to(facet_name, search_path((field_name+"_facet").to_sym => field_vals))
end

#facetPrepare(t, facet_links) ⇒ Object

Format and return the facet


158
159
160
# File 'app/helpers/table_format.rb', line 158

def facetPrepare(t, facet_links)
  return '<div class="facet'+ t["Field Name"] +'">'+ image_tag(t["Icon"]+"-24.png") + facet_links +' </div>'
end

#get_highlighted_categories(full_doc, fieldname) ⇒ Object

Gets list of the highlighted categories


145
146
147
148
149
150
151
152
153
154
155
# File 'app/helpers/table_format.rb', line 145

def get_highlighted_categories(full_doc, fieldname)
  highlightlist = Hash.new
  
  if isHighlighted?(full_doc, fieldname)
    full_doc["highlight"][fieldname].each do |field|
      highlightlist[ActionView::Base.full_sanitizer.sanitize(field)] = field
    end
  end

  return highlightlist
end

#getText(doc, field, full_doc) ⇒ Object

Gets text for the field (highlighted if it should be)


10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/table_format.rb', line 10

def getText(doc, field, full_doc)
  if isHighlighted?(full_doc, field)
    return raw(full_doc["highlight"][field].first.to_s)
  else
    # Handles both with _source and without
    begin
      return doc["_source"][field]
    rescue
      return doc[field]
    end
  end
end

#isHighlighted?(doc, field) ⇒ Boolean

Checks if field is highlighted in restuls

Returns:

  • (Boolean)

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

def isHighlighted?(doc, field)
  return doc["highlight"] && doc["highlight"][field] && !doc["highlight"][field].empty?
end

#linkedFacets(field_vals, field_name, highlighted_categories) ⇒ Object

Generates links to data filtered by facet val


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/table_format.rb', line 24

def linkedFacets(field_vals, field_name, highlighted_categories)
  outstr = ""

  # Generate links for each value in list of facet vals
  if field_vals.is_a?(Array)
    field_vals.each do |i|
      outstr += facetLinkGen(i, field_name, highlighted_categories)
      outstr += ", " if i != field_vals.last
    end
  # For single values
  else
    if field_vals
      outstr += facetLinkGen(field_vals, field_name, highlighted_categories)
    end
  end
  return outstr
end

#longTextView(t, doc, full_doc) ⇒ Object

Prepares longer text view


120
121
122
# File 'app/helpers/table_format.rb', line 120

def longTextView(t, doc, full_doc)
  return truncate(getText(doc, "doc_text", full_doc), length: 200)
end

#notEmpty?(processed_value, raw_value) ⇒ Boolean

Check if either value is empty/nill

Returns:

  • (Boolean)

72
73
74
75
76
77
78
79
# File 'app/helpers/table_format.rb', line 72

def notEmpty?(processed_value, raw_value)
  if raw_value != nil
    # Return true if processed val is not nil and raw is not empty (or nil)
    return (processed_value != nil) && (!raw_value.empty?)
  else
    return false
  end
end

#pictureView(t, doc) ⇒ Object

Prepares picture view


100
101
102
# File 'app/helpers/table_format.rb', line 100

def pictureView(t, doc)
  return '<img src="'+doc[t["Field Name"]]+'" class="picture"></img>'
end

#processType(t, display_type, doc, full_doc, dataspec) ⇒ Object

Processes type appropriately using case statement


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/table_format.rb', line 82

def processType(t, display_type, doc, full_doc, dataspec)
  case display_type
  when "Title"
    return titleView(t, doc, full_doc)
  when "Short Text", "Description"
    return shortTextView(t, doc, full_doc)
  when "Date"
    return dateView(t, doc)
  when "Long Text"
    return longTextView(t, doc, full_doc)
  when "Picture"
    return pictureView(t, doc)
  when "Category"
    return categoryView(t, doc, dataspec, full_doc)
  end
end

#shortTextView(t, doc, full_doc) ⇒ Object

Prepares description/short text view


115
116
117
# File 'app/helpers/table_format.rb', line 115

def shortTextView(t, doc, full_doc)
    return getText(doc, t["Field Name"], full_doc)
end

#showByType(display_type, doc, full_doc, is_item_field) ⇒ Object

Display the item in the appropriate way for the display type


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/table_format.rb', line 52

def showByType(display_type, doc, full_doc, is_item_field)
  output = ""
  dataspec = get_dataspec(full_doc)
  
  tableItems(dataspec).each do |t|
    # Get fields of appropriate display and item field type
    if (t["Display Type"] == display_type) && (is_item_field == dataspec.item_fields.include?(t["Field Name"]))
      # Get processed value and raw val
      processed_value = processType(t, display_type, doc, full_doc, dataspec)
      raw_value = doc[t["Field Name"]]

      # Add to output if it is not nil or a category type
      output += processed_value if notEmpty?(processed_value, raw_value)
    end
  end

  return raw(output)
end

#tableItems(dataspec) ⇒ Object

Get list of fields in results and their names


163
164
165
166
167
168
169
170
171
172
173
# File 'app/helpers/table_format.rb', line 163

def tableItems(dataspec)
  itemarr = Array.new
  
  # Get list of all fields in results
  sortFields(dataspec.fields_in_results, @all_field_info).each do |i|
    # Get details (HR name, field name, etc) for field
    itemarr.push(getFieldDetails(i, dataspec.field_info))
  end

  return itemarr
end

#titleView(t, doc, full_doc) ⇒ Object

Prepares title view


110
111
112
# File 'app/helpers/table_format.rb', line 110

def titleView(t, doc, full_doc)
  return link_to(getText(doc, t["Field Name"], full_doc), doc_path(full_doc["_id"]), class: "list_title", target: "_blank")
end