To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / redmine / search.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (2.54 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 21 | 0:513646585e45 | Chris | mattr_accessor :available_search_types
|
| 22 | 909:cbb26bc654de | Chris | |
| 23 | 0:513646585e45 | Chris | @@available_search_types = []
|
| 24 | |||
| 25 | class << self |
||
| 26 | def map(&block) |
||
| 27 | yield self |
||
| 28 | end
|
||
| 29 | 909:cbb26bc654de | Chris | |
| 30 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 37 | 0:513646585e45 | Chris | 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 | 909:cbb26bc654de | Chris | |
| 46 | 0:513646585e45 | Chris | # 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 |