Skip to content

Commit

Permalink
Fixes #24 - terminology config
Browse files Browse the repository at this point in the history
Moves terminology information box content to a config yaml file, which
is fed into a partial when defined keywords are entered. Custom info
boxes can be displayed by overriding the provided template.
  • Loading branch information
anselmbradford committed May 31, 2014
1 parent 31720a1 commit 7e8468f
Show file tree
Hide file tree
Showing 14 changed files with 590 additions and 65 deletions.
72 changes: 49 additions & 23 deletions app/helpers/application_helper.rb
Expand Up @@ -73,31 +73,57 @@ def info_box_terms
}
end

# @return [String] Returns an underscored string that corresponds to the special
# info box partial.
# @return [Hash] Defines which query terms will display an info box
# on the results page for select keywords.
def info_box_terms
YAML.load(File.read(File.expand_path("#{Rails.root}/config/terminology.yml", __FILE__)))
end

# @return [Hash] Returns a hash that should be fed into a `render` command
# that will corresponds to the info box partial in /app/views/component/terminology,
# or nil if no search keyword is present.
def dynamic_partial
# Create 2 arrays: one containing the keys of the
# info_box_terms hash, and the other containing all the values
# in the hash.
main_terms = info_box_terms.keys
synonyms = info_box_terms.values.flatten

keyword = params[:keyword].downcase if params[:keyword].present?

# Check if the keyword matches any of the hash keys or values
if (main_terms + synonyms).include?(keyword)
# If the keyword matches a value, we find the corresponding key.
# The key is what the partial corresponds to.
if synonyms.include?(keyword)
partial = info_box_terms.find { |_,v| v.include? keyword }.first
# Otherwise, it means the keyword matches a key
else
partial = keyword
if params[:keyword].present?
keyword = params[:keyword].downcase
# Create 2 arrays: one containing the search terms of the info_box_terms,
# and the other containing all the synonymns of those terms.
main_terms = info_box_terms.keys
synonyms = []
info_box_terms.select { |k,v| synonyms.push(v['synonymns']) }
synonyms.flatten!

# Check if the keyword matches any of the terms or synonymns.
if (main_terms + synonyms).include?(keyword)

# If the keyword matches a value, we find the corresponding key.
# The key is what the partial name corresponds to.
if synonyms.include?(keyword)
partial = info_box_terms.find { |k,v| v['synonymns'].include? keyword }.first
# Otherwise, it means the keyword matches a key
else
partial = keyword
end

# Grab the specific term details for filling a template partial.
term = info_box_terms[partial]

# Replace spaces in the key string with underscores to match
# the partial name (see app/views/components/terminology),
# and return the path to the partial for use in the view.
dynamic_partial = partial.tr(' ','_')

# Specify the path to the partial.
partial_path = 'component/terminology'

# If a partial with the specified name does not exist and the term
# contains a title, description, and link in the yaml file, use the
# template partial.
if !File.exist?("#{Rails.root.join('app','views',partial_path,'_'+dynamic_partial+'.html.haml')}")
dynamic_partial = 'template'
end

{:partial => "#{partial_path}/#{dynamic_partial}", :locals => {:term=>term}}
end
# replace spaces in the key string with underscores to match
# the partial name (see app/views/components/terminology),
# and return the path to the partial for use in the view.
"component/terminology/#{partial.tr(' ','_')}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/component/organizations/results/_body.html.haml
Expand Up @@ -3,7 +3,7 @@
- if dynamic_partial
%section#terminology
%div
= render :partial => dynamic_partial
= render :partial => dynamic_partial[:partial], :locals => dynamic_partial[:locals]

- if @orgs.present?
- if @map_data.present?
Expand Down
8 changes: 0 additions & 8 deletions app/views/component/terminology/_calfresh.html.haml

This file was deleted.

7 changes: 0 additions & 7 deletions app/views/component/terminology/_market_match.haml

This file was deleted.

8 changes: 0 additions & 8 deletions app/views/component/terminology/_sfmnp.html.haml

This file was deleted.

10 changes: 0 additions & 10 deletions app/views/component/terminology/_snap.html.haml

This file was deleted.

8 changes: 8 additions & 0 deletions app/views/component/terminology/_template.html.haml
@@ -0,0 +1,8 @@
%dl
%dt
= term['title']
%dd
= term['description']
- if term['url'].present?
%p
= link_to "More info...", term['url'], :target => "_blank"
8 changes: 0 additions & 8 deletions app/views/component/terminology/_wic.html.haml

This file was deleted.

79 changes: 79 additions & 0 deletions config/terminology.yml
@@ -0,0 +1,79 @@
# Show informational terminology definition boxes for certain keyword
# searches. This can be helpful for such things as providing a definition
# for the federal SNAP program when the user searches for "food stamps,"
# for example.
#
# To create an entry follow this format:
#
# '<main-keyword>' :
# synonymns :
# - '<synonymn1>'
# - '<synonymn2>'
# - '<synonymn3 ... >'
# title: '<title for this term>'
# description :
# '<description of this term>'
# url : '<where to go for more info>'
#
# The <main-keyword> and all the entries under 'synonyms'
# will be keywords the user can search for to display the
# info box.
#
# The info (title, description, url) will be fed into the
# partial at app/views/component/terminology/_template.html.haml
#
# You can override the template partial by providing your
# own partial in the app/views/component/terminology/ directory.
# Give it the name _<main-keyword>.html.haml, with underscores
# in place of spaces in the name. Such as _market_match.html.haml,
# or _wic.html.haml.

'wic' :
synonymns :
- 'women, infants, and children'
- 'WIC/Women, Infants, & Children'
title: Women, Infants, and Children
description :
Women, Infants, and Children (WIC)
provides assistance for low-income pregnant, breastfeeding, and
non-breastfeeding postpartum women, and to eligible infants and
children up to age 5..
url : http://www.fns.usda.gov/wic

'sfmnp' :
synonymns :
- "senior farmers' market nutrition program"
- 'SFMNP/Food vouchers for seniors'
title : "Senior Farmers' Market Nutrition Program"
description :
"Senior Farmers' Market Nutrition Program (SFMNP)
provides low-income seniors with coupons that can be exchanged for eligible
foods at farmers' markets, roadside stands, and community-supported
agriculture programs."
url : http://www.fns.usda.gov/sfmnp

'calfresh' :
synonymns :
- 'food stamps'
- 'snap'
- 'calfresh/food stamps'
title : CalFresh
description :
Known federally as SNAP (Supplemental Nutrition Assistance Program),
CalFresh offers nutrition assistance to eligible, low income individuals
and families and provides economic benefits to communities.
url : http://www.calfresh.ca.gov

'market match' :
synonymns : ''
title : Market Match
description :
"Market Match offers farmers’ market customers an extra $5 when they spend
at least $10 on their CalFresh EBT card at participating farmer's markets."
url : http://www.pcfma.com/pcfma_marketmatch.php

'health care reform':
synonymns :
- 'aca'
- 'affordable care act'
- 'health insurance'

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7e8468f

Please sign in to comment.