annotate lib/redmine/search.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children cbb26bc654de
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2009 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 module Redmine
Chris@0 19 module Search
Chris@0 20
Chris@0 21 mattr_accessor :available_search_types
Chris@0 22
Chris@0 23 @@available_search_types = []
Chris@0 24
Chris@0 25 class << self
Chris@0 26 def map(&block)
Chris@0 27 yield self
Chris@0 28 end
Chris@0 29
Chris@0 30 # Registers a search provider
Chris@0 31 def register(search_type, options={})
Chris@0 32 search_type = search_type.to_s
Chris@0 33 @@available_search_types << search_type unless @@available_search_types.include?(search_type)
Chris@0 34 end
Chris@0 35 end
Chris@0 36
Chris@0 37 module Controller
Chris@0 38 def self.included(base)
Chris@0 39 base.extend(ClassMethods)
Chris@0 40 end
Chris@0 41
Chris@0 42 module ClassMethods
Chris@0 43 @@default_search_scopes = Hash.new {|hash, key| hash[key] = {:default => nil, :actions => {}}}
Chris@0 44 mattr_accessor :default_search_scopes
Chris@0 45
Chris@0 46 # Set the default search scope for a controller or specific actions
Chris@0 47 # Examples:
Chris@0 48 # * search_scope :issues # => sets the search scope to :issues for the whole controller
Chris@0 49 # * search_scope :issues, :only => :index
Chris@0 50 # * search_scope :issues, :only => [:index, :show]
Chris@0 51 def default_search_scope(id, options = {})
Chris@0 52 if actions = options[:only]
Chris@0 53 actions = [] << actions unless actions.is_a?(Array)
Chris@0 54 actions.each {|a| default_search_scopes[controller_name.to_sym][:actions][a.to_sym] = id.to_s}
Chris@0 55 else
Chris@0 56 default_search_scopes[controller_name.to_sym][:default] = id.to_s
Chris@0 57 end
Chris@0 58 end
Chris@0 59 end
Chris@0 60
Chris@0 61 def default_search_scopes
Chris@0 62 self.class.default_search_scopes
Chris@0 63 end
Chris@0 64
Chris@0 65 # Returns the default search scope according to the current action
Chris@0 66 def default_search_scope
Chris@0 67 @default_search_scope ||= default_search_scopes[controller_name.to_sym][:actions][action_name.to_sym] ||
Chris@0 68 default_search_scopes[controller_name.to_sym][:default]
Chris@0 69 end
Chris@0 70 end
Chris@0 71 end
Chris@0 72 end