1 # Redmine - project management software
2 # Copyright (C) 2006-2011 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
21 mattr_accessor :available_search_types
22
23 @@available_search_types = []
24
25 class << self
26 def map(&block)
27 yield self
28 end
29
30 # Registers a search provider
31 def register(search_type, options={})
32 search_type = search_type.to_s
33 @@available_search_types << search_type unless @@available_search_types.include?(search_type)
34 end
35 end
36
37 module Controller
38 def self.included(base)
39 base.extend(ClassMethods)
40 end
41
42 module ClassMethods
43 @@default_search_scopes = Hash.new {|hash, key| hash[key] = {:default => nil, :actions => {}}}
44 mattr_accessor :default_search_scopes
45
46 # Set the default search scope for a controller or specific actions
47 # Examples:
48 # * search_scope :issues # => sets the search scope to :issues for the whole controller
49 # * search_scope :issues, :only => :index
50 # * search_scope :issues, :only => [:index, :show]
51 def default_search_scope(id, options = {})
52 if actions = options[:only]
53 actions = [] << actions unless actions.is_a?(Array)
54 actions.each {|a| default_search_scopes[controller_name.to_sym][:actions][a.to_sym] = id.to_s}
55 else
56 default_search_scopes[controller_name.to_sym][:default] = id.to_s
57 end
58 end
59 end
60
61 def default_search_scopes
62 self.class.default_search_scopes
63 end
64
65 # Returns the default search scope according to the current action
66 def default_search_scope
67 @default_search_scope ||= default_search_scopes[controller_name.to_sym][:actions][action_name.to_sym] ||
68 default_search_scopes[controller_name.to_sym][:default]
69 end
70 end
71 end
72 end