Skip to content

Instantly share code, notes, and snippets.

@floere
Created April 23, 2013 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floere/5447472 to your computer and use it in GitHub Desktop.
Save floere/5447472 to your computer and use it in GitHub Desktop.
An example of how to exclude blocked users from search results.
# SETUP.
#
require 'picky'
# Users have a name and a list of blocked_ids.
#
User = Struct.new(:id, :name, :blocked_ids) do
# Generates a list of users which have not blocked this user.
# Note: Also excludes self.
#
def visible_to
(Users.select { |user| !user.blocked_ids.include? id } - [self]).map!(&:id).map!(&:to_s)
end
end
Users = [
User.new(1, 'Joost Test', [3]),
User.new(2, 'Florian Test', [4, 5]),
User.new(3, 'Peer Test', [1]),
User.new(4, 'Poon Test', [6]),
User.new(5, 'Arie Test', [3]),
User.new(6, 'Hendrik Test', []),
]
puts "Joost is not visible to Peer, since Peer has blocked Joost."
puts "Joost is visible to the following ids:"
p Users.first.visible_to
puts "So, not visible to himself or Peer."
data = Picky::Index.new :users do
category :id
category :name
category :visible_to, tokenize: false
end
# Add users to index.
#
Users.each { |user| data.replace user }
# SEARCHING.
#
puts
puts "Let's run a few searches:"
users = Picky::Search.new data
print 'Joost does not find Peer or himself: '
p users.search('visible_to:1 test').ids
print 'Peer does not find Joost or himself: '
p users.search('visible_to:3 test').ids
print 'Florian does not find himself or Poon or Arie: '
p users.search('visible_to:2 test').ids
print 'Hendrik does not find himself: '
p users.search('visible_to:6 hendrik').ids
print 'But Arie does find Hendrik: '
p users.search('visible_to:5 hendrik').ids
print 'However, Poon has blocked Hendrik: '
p users.search('visible_to:4 hendrik').ids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment