OSDN Git Service

Move database.yml to template
[redminele/redminele.git] / redmine / lib / redmine / search.rb
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