Revision 442:753f1380d6bc vendor/plugins/acts_as_searchable/lib/.svn/text-base
| vendor/plugins/acts_as_searchable/lib/.svn/text-base/acts_as_searchable.rb.svn-base | ||
|---|---|---|
| 1 |
# redMine - project management software
|
|
| 2 |
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
|
| 1 |
# Redmine - project management software
|
|
| 2 |
# Copyright (C) 2006-2011 Jean-Philippe Lang
|
|
| 3 | 3 |
# |
| 4 | 4 |
# This program is free software; you can redistribute it and/or |
| 5 | 5 |
# modify it under the terms of the GNU General Public License |
| ... | ... | |
| 45 | 45 |
searchable_options[:date_column] ||= "#{table_name}.created_on"
|
| 46 | 46 |
searchable_options[:order_column] ||= searchable_options[:date_column] |
| 47 | 47 |
|
| 48 |
# Permission needed to search this model |
|
| 49 |
searchable_options[:permission] = "view_#{self.name.underscore.pluralize}".to_sym unless searchable_options.has_key?(:permission)
|
|
| 50 |
|
|
| 51 | 48 |
# Should we search custom fields on this model ? |
| 52 | 49 |
searchable_options[:search_custom_fields] = !reflect_on_association(:custom_values).nil? |
| 53 | 50 |
|
| ... | ... | |
| 65 | 62 |
# projects argument can be either nil (will search all projects), a project or an array of projects |
| 66 | 63 |
# Returns the results and the results count |
| 67 | 64 |
def search(tokens, projects=nil, options={})
|
| 65 |
# TODO: make user an argument |
|
| 66 |
user = User.current |
|
| 68 | 67 |
tokens = [] << tokens unless tokens.is_a?(Array) |
| 69 | 68 |
projects = [] << projects unless projects.nil? || projects.is_a?(Array) |
| 70 | 69 |
|
| ... | ... | |
| 99 | 98 |
|
| 100 | 99 |
find_options[:conditions] = [sql, * (tokens.collect {|w| "%#{w.downcase}%"} * token_clauses.size).sort]
|
| 101 | 100 |
|
| 101 |
scope = self |
|
| 102 | 102 |
project_conditions = [] |
| 103 |
project_conditions << (searchable_options[:permission].nil? ? Project.visible_by(User.current) : |
|
| 104 |
Project.allowed_to_condition(User.current, searchable_options[:permission])) |
|
| 103 |
if searchable_options.has_key?(:permission) |
|
| 104 |
project_conditions << Project.allowed_to_condition(user, searchable_options[:permission] || :view_project) |
|
| 105 |
elsif respond_to?(:visible) |
|
| 106 |
scope = scope.visible(user) |
|
| 107 |
else |
|
| 108 |
ActiveSupport::Deprecation.warn "acts_as_searchable with implicit :permission option is deprecated. Add a visible scope to the #{self.name} model or use explicit :permission option."
|
|
| 109 |
project_conditions << Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym)
|
|
| 110 |
end |
|
| 111 |
# TODO: use visible scope options instead |
|
| 105 | 112 |
project_conditions << "#{searchable_options[:project_key]} IN (#{projects.collect(&:id).join(',')})" unless projects.nil?
|
| 113 |
project_conditions = project_conditions.empty? ? nil : project_conditions.join(' AND ')
|
|
| 106 | 114 |
|
| 107 | 115 |
results = [] |
| 108 | 116 |
results_count = 0 |
| 109 | 117 |
|
| 110 |
with_scope(:find => {:conditions => project_conditions.join(' AND ')}) do
|
|
| 118 |
with_scope(:find => {:conditions => project_conditions}) do
|
|
| 111 | 119 |
with_scope(:find => find_options) do |
| 112 |
results_count = count(:all) |
|
| 113 |
results = find(:all, limit_options) |
|
| 120 |
results_count = scope.count(:all)
|
|
| 121 |
results = scope.find(:all, limit_options)
|
|
| 114 | 122 |
end |
| 115 | 123 |
end |
| 116 | 124 |
[results, results_count] |
Also available in: Unified diff