-
Notifications
You must be signed in to change notification settings - Fork 26
Advanced Challenge Search
run rails server and go http://localhost:3000/challenges page
I tried to use lunar at first, it gave me a very clean code, but unfortunately some of funtions like category search did not work. In order to use lunar, I had to add and fix some of functions. After some considerations, I decided not to use. One of reasons is that lunar gem did not seem to be active. Instead, I borrowed ideas from it, and implemented by myself.
And soe's approach was very helful as well.
It gives Challenge model a search function. I wanted to seperate Search module from Challenge. All functions regarding handling redis or searching is included in ChallengeSearchable module. I used 'redis' prefix for methods in this module. Seemed reasonable at first, but now I am not sure.
.redis_sync_all
method gets all challenges from database using api, and synchronizes all challenges to REDIS.
Challenge.redis_sync_all
For a challenge instance #redis_sync
, #redis_insert
, or .redis_remove
methods can be used
challenge.redis_insert # insert challenge to REDIS
challenge.redis_sync # insert or update challenge to REDIS
Challenge.redis_remove(challenge.challenge_id) # remove challenge from REDIS
Do not use #redis_remove
directly, it can cause unexpected result when challenge is different from REDIS. For instance, after inserting a challenge to REDIS, and let's say challenge's title has changed. If you call #redis_remove
with modified challenge, it will not delete original keywords. To be safe, use Challenge.redis_remove(challenge.challenge.id)
. It will retrieve challenge from REDIS, and will remove data from REDIS.
After synchronizing, you can get challenge from REDIS without using remote API.
Challenge.redis_find(challenge_id)
#search
method and be used like follows.
Challenge.search query: "ruby", state: "closed"
Challenge.search categories: ["ruby", "java"]
Challenge.search community: "Appirio"
Challenge.search prize_money: {min: 1000, max: 3000}
Challenge.search participants: {min: 2}
Challenge.search participants: 3
Challenge.search query: "ruby", sort_by: "prize_money", order: "DESC"
Challenge.search query: "ruby", sort_by: "challenge_type"
ChallengeSearchable module needs to be more modulized. Needs test codes. (sorry for that)
Search form is very similar to soe's solution. I just added community, price, and participants options. Challenge List is similar to original site's.