Module: SearchedFormat

Included in:
DocsHelper
Defined in:
app/helpers/searched_format.rb

Instance Method Summary collapse

Instance Method Details

#check_start_or_end(k) ⇒ Object

Checks if it is the start or end date of range


75
76
77
78
79
80
81
# File 'app/helpers/searched_format.rb', line 75

def check_start_or_end(k)
  if k.include?("startrange_")
    return "Later Than"
  elsif k.include?("endrange_")
    return "Earlier Than"
  end
end

Generates the x link with the appropriate path


123
124
125
# File 'app/helpers/searched_format.rb', line 123

def genXLink(path)
  return link_to(raw('<b style="color: red" class="x"> X</b>'), path, :class => "remove-filter")
end

#get_facet_hrname(k) ⇒ Object


50
51
52
53
54
55
56
# File 'app/helpers/searched_format.rb', line 50

def get_facet_hrname(k)
  # Get field display details
  field_spec = getFieldDetails(k.to_s.gsub("_facet", ""), @all_field_info)
  
  # Get facet hrname
  return field_spec["Human Readable Name"]
end

Generates the appropriate remove link for the situation


103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/helpers/searched_format.rb', line 103

def getRemoveLink(k, v)
  # If it's the last search term, go back to main page
  if lastSearchTerm?(k)
    return genXLink(docs_path)
  else
    # Check if one or more vals are chosen
    if params[k].is_a? Array
      return multValsSelected(k, v)
    else # For single vals per category
      return genXLink(search_path(params.except(k)))
    end
  end
end

#lastSearchTerm?(k) ⇒ Boolean

Checks if it is the last search term or not

Returns:

  • (Boolean)

118
119
120
# File 'app/helpers/searched_format.rb', line 118

def lastSearchTerm?(k)
  return params.except("page").length <= 4 && (!params[k].is_a?(Array) || params[k].length <= 1)
end

#multValsSelected(k, v) ⇒ Object

Link removes just one val if multiple are chosen


128
129
130
131
132
133
134
# File 'app/helpers/searched_format.rb', line 128

def multValsSelected(k, v)
  saveparams = params[k]
  params[k] = params[k] - [v] # Remove value from array                                                 
  link = genXLink(search_path(params))
  params[k] = saveparams # Set it back to normal
  return link
end

#process_allforindex_filter(params, k, v) ⇒ Object

Processes the filter for the index


59
60
61
62
63
64
# File 'app/helpers/searched_format.rb', line 59

def process_allforindex_filter(params, k, v)
  _, dataspec = get_search_param(params)
  dataset_name = dataspec.dataset_name
  hrname = "All " + dataset_name
  return removeFormat(hrname, k, v, "search")
end

#process_datefield_filter(k, v) ⇒ Object

Generates filter for date field queries


67
68
69
70
71
72
# File 'app/helpers/searched_format.rb', line 67

def process_datefield_filter(k, v)
  param_item, dataspec = get_date_index(k)
  dataset_name = dataspec.dataset_name
  hrname = getHR(param_item, dataspec)+" "+ check_start_or_end(k)+" ("+dataset_name+")"
  return removeFormat(hrname, k, v, "search")
end

#process_facet_field(params, k, v) ⇒ Object

Generates remove links for facet fields


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/searched_format.rb', line 34

def process_facet_field(params, k, v)
  outhtml = ""
  
  # Handle multiple sets of facets
  if v.is_a?(Array)
    v.each do |v1|
      outhtml += removeFormat(get_facet_hrname(k), k, v1, "filter")
    end
  else
    # Handle single facets
    outhtml += removeFormat(get_facet_hrname(k), k, v, "filter")
  end

  return outhtml
end

#process_searchfield_filter(params, k, v) ⇒ Object

Generates filter for search by field


84
85
86
87
88
89
90
# File 'app/helpers/searched_format.rb', line 84

def process_searchfield_filter(params, k, v)
  param_item, dataspec = get_search_param(params)
  field_key = param_item.keys.first
  dataset_name = dataspec.dataset_name
  hrname = getHR(field_key, dataspec)+" ("+dataset_name+")"
  return removeFormat(hrname, k, v, "search")
end

#removeFormat(hrname, k, v, type) ⇒ Object

Formats the x link for search terms (link removes term from search)


93
94
95
96
97
98
99
100
# File 'app/helpers/searched_format.rb', line 93

def removeFormat(hrname, k, v, type)
  outstr = '<div class="search-filter">'
  outstr += '<span class="filter">' + strip_tags(v) + '</span>'
  outstr += getRemoveLink(k, v)
  outstr += '<br>'
  outstr += '<span class="category">' + hrname + " ["+type+"] </span>"
  outstr += '</div>'
end

#render_filtersObject

Renders the filters for the dataspec


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/searched_format.rb', line 3

def render_filters
  outhtml = ""

  # Go through and add to output
  params.except("utf8", "action", "controller", "page").each do |k, v|
    # For facet params
    if k.include?("_facet")
      outhtml += process_facet_field(params, k, v)
      
    # For search all
    elsif k == "q"
      outhtml += removeFormat("All Fields", k, v, "search")
      
    # Search all in particular index
    elsif k.include?("all_sindex_")
      outhtml += process_allforindex_filter(params, k, v)

    # Check if it is a date
    elsif k.include?("startrange_") || k.include?("endrange_")
      outhtml += process_datefield_filter(k, v)

    # For a specific non-empty search term
    elsif params[k] != ""
      outhtml += process_searchfield_filter(params, k, v)
    end
  end

  return raw(outhtml)
end