Permalink
Browse files

Add feeling lucky query with ! directive

  • Loading branch information...
1 parent e7b106f commit 5fc32371400dcea310202f7b45b5293d91cf9f82 @rwdaigle committed Oct 9, 2012
Showing with 19 additions and 1 deletion.
  1. +19 −1 app/controllers/gists_controller.rb
@@ -4,16 +4,34 @@ class GistsController < ApplicationController
before_filter :force_user_login
+ JUMP_CHAR = '!'
+
def index
@gists = current_user.gists.order("gh_created_at DESC").includes(:files)
end
def search
- @results = Gist.search(current_user, params[:q])
+ @results = Gist.search(current_user, normalized_query)
+ if(feeling_lucky_directive? && lucky_result = @results.first)
+ redirect_to lucky_result.url
+ log({ns: self.class, fn: __method__, measure: true, at: 'auto-jump', query: params[:q]}, {:'redirect-to' => lucky_result.url}, current_user)
+ end
end
def refresh
QC.enqueue("GistFetcher.fetch_user", current_user.id)
redirect_to search_gists_path
end
+
+ private
+
+ # Strip navigational search directives ("!") without modifying original query param
+ def normalized_query
+ @normalized_query ||= feeling_lucky_directive? ? params[:q][0..-2] : params[:q]
+ end
+
+ def feeling_lucky_directive?
+ params[:q] && JUMP_CHAR == params[:q].last
+ end
+
end

0 comments on commit 5fc3237

Please sign in to comment.