Skip to content

Instantly share code, notes, and snippets.

@mcritchlow
Last active August 29, 2015 14:06
Show Gist options
  • Save mcritchlow/44b16855ad99220753f2 to your computer and use it in GitHub Desktop.
Save mcritchlow/44b16855ad99220753f2 to your computer and use it in GitHub Desktop.
Facet Checked
class Facet
def checked_one_liner?(field,value,params)
params['f'] && params['f'][field] && params['f'][field].include?(value)
end
def checked?(field,value,params)
params.has_key?('f') && params['f'].has_key?(field) && params['f'][field].include?(value)
end
end
params = {"f"=>{"unit_sim"=>["Library Digital Collections"]}, "q"=>"fish", "spellcheck.q"=>"fish", "utf8"=>"✓", "action"=>"index", "controller"=>"catalog"}
params_without_f = {"g"=>{"unit_sim"=>["Library Digital Collections"]}, "q"=>"fish", "spellcheck.q"=>"fish", "utf8"=>"✓", "action"=>"index", "controller"=>"catalog"}
f = Facet.new
puts "Using checked?"
puts f.checked?('unit_sim','Library Digital Collections',params)
puts f.checked?('unit_sim','Research Collections',params)
puts f.checked?('unit_sim','',params)
puts f.checked?('unit_sim',nil,params)
puts f.checked?('collection_sim','Library Digital Collections',params)
puts f.checked?('collection_sim','Library Digital Collections',params_without_f)
puts f.checked?('unit_sim','Library Digital Collections',params_without_f)
puts "\nUsing checked_one_liner?"
puts f.checked_one_liner?('unit_sim','Library Digital Collections',params)
puts f.checked_one_liner?('unit_sim','Research Collections',params)
puts f.checked_one_liner?('unit_sim','',params)
puts f.checked_one_liner?('unit_sim',nil,params)
# none of these return false
puts f.checked_one_liner?('collection_sim','Library Digital Collections',params)
puts f.checked_one_liner?('collection_sim','Library Digital Collections',params_without_f)
puts f.checked_one_liner?('unit_sim','Library Digital Collections',params_without_f)
$ ruby facet.rb
Using checked?
true
false
false
false
false
false
false
Using checked_one_liner?
true
false
false
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment