Skip to content

Commit

Permalink
Added live-search on new-ticket page, to reduce duplication.
Browse files Browse the repository at this point in the history
git-svn-id: http://store.sabretechllc.com/public/jonmagic/optik/trunk@294 331dd862-6008-0410-a02f-b57fe9264243
  • Loading branch information
dcparker committed Nov 8, 2007
1 parent 05ca83d commit 3efd00d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def search
end
@page_title = 'Search Results'
end

def similar_search
@query = params[:query] || request.raw_post || request.query_string
@tickets = Ticket.similar_search(@query, :limit => 8)
end

def goto
@ticket = params[:number]
Expand Down
18 changes: 18 additions & 0 deletions app/models/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ def self.fullsearch(query, options = {})
end
end

def self.similar_search(query, options = {})
if !query.to_s.strip.empty?
tokens = query.split
resultshash = {}
Ticket.find_tagged_with(options.merge(:any => tokens)).each do |result|
resultshash[result.id] ||= [0, nil]
resultshash[result.id][0] += 1
resultshash[result.id][1] = result
end
results = resultshash.keys.collect {|k| resultshash[k][1]}.sort {|b,a| a.updated_at <=> b.updated_at}
offset = options.delete(:offset) || 0
limit = options.delete(:limit) || 100000
return results[offset..(offset+limit)]
else
[]
end
end

# def self.search(query)
# if !query.to_s.strip.empty?
# tokens = query.split.collect {|c| "%#{c.downcase}%"}
Expand Down
9 changes: 8 additions & 1 deletion app/views/tickets/new.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@
<%= submit_tag "Create" %>
<%= end_form_tag %>
</div>
<div id="live_similar_search">
<!-- search concatenate of fields 'ticket[description]' and 'tags' -->
</div>
</div>
<%= observe_field 'tags',
:update => 'live_similar_search',
:url => { :controller => 'tickets', :action=> 'similar_search' },
:with => "'query=' + escape(value)" %>
7 changes: 7 additions & 0 deletions app/views/tickets/similar_search.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h4>Is your ticket a duplicate of any of the following?</h4>
<table id="table-search" class="table">
<%= render :partial => "tableheader" %>
<% for @ticket in @tickets %>
<%= render :partial => "list" %>
<% end %>
</table>

0 comments on commit 3efd00d

Please sign in to comment.