##// END OF EJS Templates
Contextual quick search (#3263)....
Jean-Philippe Lang -
r2829:07aa3c55bdc7
parent child
Show More
@@ -0,0 +1,55
1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 module Redmine
19 module Search
20 module Controller
21 def self.included(base)
22 base.extend(ClassMethods)
23 end
24
25 module ClassMethods
26 @@default_search_scopes = Hash.new {|hash, key| hash[key] = {:default => nil, :actions => {}}}
27 mattr_accessor :default_search_scopes
28
29 # Set the default search scope for a controller or specific actions
30 # Examples:
31 # * search_scope :issues # => sets the search scope to :issues for the whole controller
32 # * search_scope :issues, :only => :index
33 # * search_scope :issues, :only => [:index, :show]
34 def default_search_scope(id, options = {})
35 if actions = options[:only]
36 actions = [] << actions unless actions.is_a?(Array)
37 actions.each {|a| default_search_scopes[controller_name.to_sym][:actions][a.to_sym] = id.to_s}
38 else
39 default_search_scopes[controller_name.to_sym][:default] = id.to_s
40 end
41 end
42 end
43
44 def default_search_scopes
45 self.class.default_search_scopes
46 end
47
48 # Returns the default search scope according to the current action
49 def default_search_scope
50 @default_search_scope ||= default_search_scopes[controller_name.to_sym][:actions][action_name.to_sym] ||
51 default_search_scopes[controller_name.to_sym][:default]
52 end
53 end
54 end
55 end
@@ -26,6 +26,7 class ApplicationController < ActionController::Base
26 26 before_filter :user_setup, :check_if_login_required, :set_localization
27 27 filter_parameter_logging :password
28 28
29 include Redmine::Search::Controller
29 30 include Redmine::MenuManager::MenuController
30 31 helper Redmine::MenuManager::MenuHelper
31 32
@@ -16,6 +16,7
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class BoardsController < ApplicationController
19 default_search_scope :messages
19 20 before_filter :find_project, :authorize
20 21
21 22 helper :messages
@@ -16,6 +16,7
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class DocumentsController < ApplicationController
19 default_search_scope :documents
19 20 before_filter :find_project, :only => [:index, :new]
20 21 before_filter :find_document, :except => [:index, :new]
21 22 before_filter :authorize
@@ -17,6 +17,7
17 17
18 18 class IssuesController < ApplicationController
19 19 menu_item :new_issue, :only => :new
20 default_search_scope :issues
20 21
21 22 before_filter :find_issue, :only => [:show, :edit, :reply]
22 23 before_filter :find_issues, :only => [:bulk_edit, :move, :destroy]
@@ -17,6 +17,7
17 17
18 18 class MessagesController < ApplicationController
19 19 menu_item :boards
20 default_search_scope :messages
20 21 before_filter :find_board, :only => [:new, :preview]
21 22 before_filter :find_message, :except => [:new, :preview]
22 23 before_filter :authorize, :except => [:preview, :edit, :destroy]
@@ -16,6 +16,7
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class NewsController < ApplicationController
19 default_search_scope :news
19 20 before_filter :find_news, :except => [:new, :index, :preview]
20 21 before_filter :find_project, :only => [:new, :preview]
21 22 before_filter :authorize, :except => [:index, :preview]
@@ -24,6 +24,8 class InvalidRevisionParam < Exception; end
24 24
25 25 class RepositoriesController < ApplicationController
26 26 menu_item :repository
27 default_search_scope :changesets
28
27 29 before_filter :find_repository, :except => :edit
28 30 before_filter :find_project, :only => :edit
29 31 before_filter :authorize
@@ -18,6 +18,7
18 18 require 'diff'
19 19
20 20 class WikiController < ApplicationController
21 default_search_scope :wiki_pages
21 22 before_filter :find_wiki, :authorize
22 23 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
23 24
@@ -31,6 +31,7
31 31 <div id="header">
32 32 <div id="quick-search">
33 33 <% form_tag({:controller => 'search', :action => 'index', :id => @project}, :method => :get ) do %>
34 <%= hidden_field_tag(controller.default_search_scope, 1, :id => nil) if controller.default_search_scope %>
34 35 <%= link_to l(:label_search), {:controller => 'search', :action => 'index', :id => @project}, :accesskey => accesskey(:search) %>:
35 36 <%= text_field_tag 'q', @question, :size => 20, :class => 'small', :accesskey => accesskey(:quick_search) %>
36 37 <% end %>
@@ -1093,4 +1093,11 class IssuesControllerTest < ActionController::TestCase
1093 1093 assert_equal 2, TimeEntry.find(1).issue_id
1094 1094 assert_equal 2, TimeEntry.find(2).issue_id
1095 1095 end
1096
1097 def test_default_search_scope
1098 get :index
1099 assert_tag :div, :attributes => {:id => 'quick-search'},
1100 :child => {:tag => 'form',
1101 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
1102 end
1096 1103 end
General Comments 0
You need to be logged in to leave comments. Login now