1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
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.
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.
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.
21 def self.included(base)
22 base.extend(ClassMethods)
26 @@default_search_scopes = Hash.new {|hash, key| hash[key] = {:default => nil, :actions => {}}}
27 mattr_accessor :default_search_scopes
29 # Set the default search scope for a controller or specific actions
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}
39 default_search_scopes[controller_name.to_sym][:default] = id.to_s
44 def default_search_scopes
45 self.class.default_search_scopes
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]