Project

General

Profile

Patch #821 » search_all_projects_one_query.patch

Peter Van den Bosch, 2008-05-17 01:53

View differences:

app/controllers/search_controller.rb (working copy)
@question.strip!
@all_words = params[:all_words] || (params[:submit] ? false : true)
@titles_only = !params[:titles_only].nil?
@all_projects = !@project || !params[:all_projects].nil?
offset = nil
begin; offset = params[:offset].to_time if params[:offset]; rescue; end
......
return
end
if @project
# only show what the user is allowed to view
@object_types = %w(issues news documents changesets wiki_pages messages)
@object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
@scope = @object_types.select {|t| params[t]}
@scope = @object_types if @scope.empty?
@object_types = %w(issues news documents changesets wiki_pages messages)
# only show what the user is allowed to view
if !@all_projects
@object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
else
@object_types = @scope = %w(projects)
@object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, nil, {:global => true})}
end
#allow searching for projects when not inside one
if !@project
@object_types << "projects"
end
@scope = @object_types.select {|t| params[t]}
@scope = @object_types if @scope.empty?
# extract tokens from the question
# eg. hello "bye bye" => ["hello", "bye bye"]
@tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')}
......
like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}
@results = []
limit = 10
if @project
@scope.each do |s|
@results += s.singularize.camelcase.constantize.search(like_tokens, @project,
:all_words => @all_words,
:titles_only => @titles_only,
:limit => (limit+1),
:offset => offset,
:before => params[:previous].nil?)
@scope.each do |s|
if @all_projects
projects_to_search = Project.find :all
projects_to_search.select {|p| User.current.allowed_to?("view_#{s}".to_sym, p)}
else
projects_to_search = @project
end
@results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime}
if params[:previous].nil?
@pagination_previous_date = @results[0].event_datetime if offset && @results[0]
if @results.size > limit
@pagination_next_date = @results[limit-1].event_datetime
@results = @results[0, limit]
end
else
@pagination_next_date = @results[-1].event_datetime if offset && @results[-1]
if @results.size > limit
@pagination_previous_date = @results[-(limit)].event_datetime
@results = @results[-(limit), limit]
end
@results += s.singularize.camelcase.constantize.search(like_tokens, projects_to_search,
:all_words => @all_words,
:titles_only => @titles_only,
:limit => (limit+1),
:offset => offset,
:before => params[:previous].nil?)
end
@results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime}
if params[:previous].nil?
@pagination_previous_date = @results[0].event_datetime if offset && @results[0]
if @results.size > limit
@pagination_next_date = @results[limit-1].event_datetime
@results = @results[0, limit]
end
else
operator = @all_words ? ' AND ' : ' OR '
@results += Project.find(:all,
:limit => limit,
:conditions => [ (["(#{Project.visible_by(User.current)}) AND (LOWER(name) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort]
) if @scope.include? 'projects'
# if only one project is found, user is redirected to its overview
redirect_to :controller => 'projects', :action => 'show', :id => @results.first and return if @results.size == 1
@pagination_next_date = @results[-1].event_datetime if offset && @results[-1]
if @results.size > limit
@pagination_previous_date = @results[-(limit)].event_datetime
@results = @results[-(limit), limit]
end
end
else
@question = ""
app/views/search/index.rhtml (working copy)
<br />
<label><%= check_box_tag 'all_words', 1, @all_words %> <%= l(:label_all_words) %></label>
<label><%= check_box_tag 'titles_only', 1, @titles_only %> <%= l(:label_search_titles_only) %></label>
<% if @project %>
<label><%= check_box_tag 'all_projects',1,@all_projects %> <%= l(:label_all_projects) %></label>
<% end%>
</p>
<%= submit_tag l(:button_submit), :name => 'submit' %>
<% end %>
......
<% @results.each do |e| %>
<li><p><%= link_to highlight_tokens(truncate(e.event_title, 255), @tokens), e.event_url %><br />
<%= highlight_tokens(e.event_description, @tokens) %><br />
<% if @all_projects and e.respond_to?('project') %>
<span class="author"><%= l(:label_in_project) %> <%= e.project.name %><br />
<% end %>
<span class="author"><%= format_time(e.event_datetime) %></span></p></li>
<% end %>
</ul>
lang/en.yml (working copy)
label_roadmap_no_issues: No issues for this version
label_search: Search
label_result_plural: Results
label_in_project: in
label_all_projects: All projects
label_all_words: All words
label_wiki: Wiki
label_wiki_edit: Wiki edit
vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb (working copy)
if options[:offset]
sql = "(#{sql}) AND (#{searchable_options[:date_column]} " + (options[:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')"
end
if project.is_a?(Array)
sql += " AND #{searchable_options[:project_key]} IN (#{project.collect(&:id).join(',')})"
else
sql += " AND #{searchable_options[:project_key]} = #{project.id}"
end
find_options[:conditions] = [sql, * (tokens * token_clauses.size).sort]
results = with_scope(:find => {:conditions => ["#{searchable_options[:project_key]} = ?", project.id]}) do
find(:all, find_options)
end
results = find(:all, find_options)
if searchable_options[:with] && !options[:titles_only]
searchable_options[:with].each do |model, assoc|
results += model.to_s.camelcase.constantize.search(tokens, project, options).collect {|r| r.send assoc}
(5-5/5)