diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2008-05-18 16:15:22 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2008-05-18 16:15:22 +0000 |
commit | 073818f8bc46580e118a7550f1faaa55b3be13d9 (patch) | |
tree | 6ea9ab7a218ecdac7f4fba61f339e17d0459f04d /vendor | |
parent | 1907c31138d065aea93f53b8e7565e06a44e49f8 (diff) | |
download | redmine-073818f8bc46580e118a7550f1faaa55b3be13d9.tar.gz redmine-073818f8bc46580e118a7550f1faaa55b3be13d9.zip |
Ability to search all projects or the projects the user belongs to (#791).
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1435 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'vendor')
-rw-r--r-- | vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb b/vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb index dff76b913..1eb64c30f 100644 --- a/vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb +++ b/vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb @@ -49,6 +49,9 @@ module Redmine raise 'No date column defined defined.' end + # Permission needed to search this model + searchable_options[:permission] = "view_#{self.name.underscore.pluralize}".to_sym unless searchable_options.has_key?(:permission) + # Should we search custom fields on this model ? searchable_options[:search_custom_fields] = !reflect_on_association(:custom_values).nil? @@ -62,8 +65,12 @@ module Redmine end module ClassMethods - def search(tokens, project, options={}) + # Search the model for the given tokens + # projects argument can be either nil (will search all projects), a project or an array of projects + def search(tokens, projects, options={}) tokens = [] << tokens unless tokens.is_a?(Array) + projects = [] << projects unless projects.nil? || projects.is_a?(Array) + find_options = {:include => searchable_options[:include]} find_options[:limit] = options[:limit] if options[:limit] find_options[:order] = "#{searchable_options[:date_column]} " + (options[:before] ? 'DESC' : 'ASC') @@ -92,12 +99,17 @@ module Redmine end find_options[:conditions] = [sql, * (tokens * token_clauses.size).sort] - results = with_scope(:find => {:conditions => ["#{searchable_options[:project_key]} = ?", project.id]}) do + project_conditions = [] + project_conditions << (searchable_options[:permission].nil? ? Project.visible_by(User.current) : + Project.allowed_to_condition(User.current, searchable_options[:permission])) + project_conditions << "#{searchable_options[:project_key]} IN (#{projects.collect(&:id).join(',')})" unless projects.nil? + + results = with_scope(:find => {:conditions => project_conditions.join(' AND ')}) do find(:all, find_options) end if searchable_options[:with] && !options[:titles_only] searchable_options[:with].each do |model, assoc| - results += model.to_s.camelcase.constantize.search(tokens, project, options).collect {|r| r.send assoc} + results += model.to_s.camelcase.constantize.search(tokens, projects, options).collect {|r| r.send assoc} end results.uniq! end |