VRFS-201 enhanced search feature to include friends
This commit is contained in:
parent
9ae702eaaf
commit
5e31dd5a06
|
|
@ -16,5 +16,32 @@ module JamRuby
|
|||
Friendship.create(:user_id => friend_id, :friend_id => user_id)
|
||||
end
|
||||
end
|
||||
|
||||
def self.search(query, user_id, options = { :limit => 10 })
|
||||
# only issue search if at least 2 characters are specified
|
||||
if query.nil? || query.length < 2
|
||||
return []
|
||||
end
|
||||
|
||||
# create 'anded' statement
|
||||
query = Search.create_tsquery(query)
|
||||
|
||||
if query.nil? || query.length == 0
|
||||
return []
|
||||
end
|
||||
|
||||
friends = Friendship.joins(
|
||||
%Q{
|
||||
INNER JOIN
|
||||
users
|
||||
ON friendships.friend_id = users.id
|
||||
WHERE friendships.user_id = '#{user_id}'
|
||||
AND users.name_tsv @@ to_tsquery('jamenglish', '#{query}')
|
||||
}
|
||||
)
|
||||
|
||||
friends = friends.limit(options[:limit])
|
||||
return friends
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
module JamRuby
|
||||
# not a active_record model; just a search result
|
||||
class Search
|
||||
attr_accessor :bands, :musicians, :fans, :recordings
|
||||
attr_accessor :bands, :musicians, :fans, :recordings, :friends
|
||||
|
||||
LIMIT = 10
|
||||
|
||||
|
|
@ -11,16 +11,22 @@ module JamRuby
|
|||
users = User.search(query, :limit => LIMIT)
|
||||
bands = Band.search(query, :limit => LIMIT)
|
||||
recordings = Recording.search(query, :limit => LIMIT)
|
||||
|
||||
return Search.new(users + bands + recordings)
|
||||
end
|
||||
|
||||
# performs a site-white search
|
||||
def self.search(query, user_id)
|
||||
friends = Friendship.search(query, user_id, :limit => LIMIT)
|
||||
return Search.new(friends)
|
||||
end
|
||||
|
||||
# search_results - results from a Tire search across band/user/recording
|
||||
def initialize(search_results)
|
||||
@bands = []
|
||||
@musicians = []
|
||||
@fans = []
|
||||
@recordings = []
|
||||
@friends = []
|
||||
|
||||
if search_results.nil?
|
||||
return
|
||||
|
|
@ -33,13 +39,15 @@ module JamRuby
|
|||
else
|
||||
@fans.push(result)
|
||||
end
|
||||
elsif result.class == Band
|
||||
elsif result.class == Band
|
||||
@bands.push(result)
|
||||
elsif result.class == Recording
|
||||
@recordings.push(result)
|
||||
elsif result.class == Friendship
|
||||
@friends.push(result.friend)
|
||||
else
|
||||
raise Exception, "unknown class #{result.class} returned in search results"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue