Module: CategoryLink

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

Instance Method Summary collapse

Instance Method Details

#genMultSelected(val, categories_chosen, linkname, category_name) ⇒ Object

Handles generation of links for facets when multiple values are selected



46
47
48
49
50
51
# File 'app/helpers/category_link.rb', line 46

def genMultSelected(val, categories_chosen, linkname, category_name)
  outval = categories_chosen.dup
  outval.delete(val["term"])
  outval = outval[0] if categories_chosen.count <= 2
  return genLink(linkname, prepNewPath(params.except(category_name), category_name, outval), true)
end

#is_selected?(categories_chosen, val) ⇒ Boolean

Check if facet is selected

Returns:

  • (Boolean)


19
20
21
22
23
# File 'app/helpers/category_link.rb', line 19

def is_selected?(categories_chosen, val)
  single_match = categories_chosen == val["term"]
  multiple_match = (categories_chosen.is_a?(Array) && categories_chosen.include?(val["term"]))
  return single_match || multiple_match
end

#notSelected(val, categories_chosen, category_name, linkname) ⇒ Object

If facet is not selected, generate link



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

def notSelected(val, categories_chosen, category_name, linkname)
  if categories_chosen # Check if another facet in the same category is selected
    return pickMeToo(categories_chosen, val, category_name, linkname)
  else
    return pickMeFirst(val, category_name, linkname)
  end
end

#pickMeFirst(val, category_name, linkname) ⇒ Object

Generates link if no others are picked in category



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

def pickMeFirst(val, category_name, linkname)
  val_link = genLink(linkname, prepNewPath(params, category_name, val["term"]), false)
  params.except(category_name)

  return val_link
end

#pickMeToo(categories_chosen, val, category_name, linkname) ⇒ Object

Generates link for if other vals in category are chosen (but not this one yet)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/category_link.rb', line 72

def pickMeToo(categories_chosen, val, category_name, linkname)
  # Handle multiple categories chosen vs one
  if categories_chosen.is_a?(Array)
    categories_chosen = categories_chosen.dup.push(val["term"])
  else
    categories_chosen = [categories_chosen, val["term"]]
  end

  # Generate link
  val_link = genLink(linkname, prepNewPath(params, category_name, categories_chosen), false)
  params.except(category_name).merge(category_name => categories_chosen)

  return val_link
end

#prepNewPath(par, category_name, chosen) ⇒ Object

New search path for facet (to avoid pagination issue)



26
27
28
# File 'app/helpers/category_link.rb', line 26

def prepNewPath(par, category_name, chosen)
  return search_path(par.merge(category_name => chosen, :page => 1))
end

#selected(val, categories_chosen, category_name, linkname) ⇒ Object

Generate link for selected facet



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/category_link.rb', line 31

def selected(val, categories_chosen, category_name, linkname)
  # Check if there are other facets selected (in this category or others)
  if params.except("controller", "action", "utf8", "page", category_name).length > 0 || categories_chosen.is_a?(Array)
    # Are there other facets selected in this category?
    if categories_chosen.is_a?(Array)
      genMultSelected(val, categories_chosen, linkname, category_name)
    else # If no others in category selected
      return genLink(linkname, search_path(params.except(category_name, :page)), true)
    end
  else # If no others selected
    return genLink(linkname, root_path, true)
  end
end

Generate link html for single term



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/helpers/category_link.rb', line 4

def termLink(val, categories_chosen, category_name)
  linkname = "#{val["term"]} (#{val["count"].to_s})"

  # Only generate links for non-blank terms
  if !val["term"].empty?
    # Check if link is selected or not                                                                    
    if is_selected?(categories_chosen, val)
      return selected(val, categories_chosen, category_name, linkname)
    else
      return notSelected(val, categories_chosen, category_name, linkname)
    end
  end
end