Revision 1298:4f746d8966dd lib/plugins/acts_as_searchable/lib
| lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb | ||
|---|---|---|
| 1 | 1 |
# Redmine - project management software |
| 2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang
|
|
| 2 |
# Copyright (C) 2006-2013 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 |
| ... | ... | |
| 72 | 72 |
tokens = [] << tokens unless tokens.is_a?(Array) |
| 73 | 73 |
projects = [] << projects unless projects.nil? || projects.is_a?(Array) |
| 74 | 74 |
|
| 75 |
find_options = {:include => searchable_options[:include]}
|
|
| 76 |
find_options[:order] = "#{searchable_options[:order_column]} " + (options[:before] ? 'DESC' : 'ASC')
|
|
| 77 |
|
|
| 78 | 75 |
limit_options = {}
|
| 79 | 76 |
limit_options[:limit] = options[:limit] if options[:limit] |
| 80 |
if options[:offset] |
|
| 81 |
limit_options[:conditions] = "(#{searchable_options[:date_column]} " + (options[:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')"
|
|
| 82 |
end |
|
| 83 | 77 |
|
| 84 | 78 |
columns = searchable_options[:columns] |
| 85 | 79 |
columns = columns[0..0] if options[:titles_only] |
| ... | ... | |
| 87 | 81 |
token_clauses = columns.collect {|column| "(LOWER(#{column}) LIKE ?)"}
|
| 88 | 82 |
|
| 89 | 83 |
if !options[:titles_only] && searchable_options[:search_custom_fields] |
| 90 |
searchable_custom_field_ids = CustomField.find(:all, |
|
| 91 |
:select => 'id', |
|
| 92 |
:conditions => { :type => "#{self.name}CustomField",
|
|
| 93 |
:searchable => true }).collect(&:id) |
|
| 84 |
searchable_custom_field_ids = CustomField.where(:type => "#{self.name}CustomField", :searchable => true).pluck(:id)
|
|
| 94 | 85 |
if searchable_custom_field_ids.any? |
| 95 | 86 |
custom_field_sql = "#{table_name}.id IN (SELECT customized_id FROM #{CustomValue.table_name}" +
|
| 96 | 87 |
" WHERE customized_type='#{self.name}' AND customized_id=#{table_name}.id AND LOWER(value) LIKE ?" +
|
| ... | ... | |
| 101 | 92 |
|
| 102 | 93 |
sql = (['(' + token_clauses.join(' OR ') + ')'] * tokens.size).join(options[:all_words] ? ' AND ' : ' OR ')
|
| 103 | 94 |
|
| 104 |
find_options[:conditions] = [sql, * (tokens.collect {|w| "%#{w.downcase}%"} * token_clauses.size).sort]
|
|
| 95 |
tokens_conditions = [sql, * (tokens.collect {|w| "%#{w.downcase}%"} * token_clauses.size).sort]
|
|
| 105 | 96 |
|
| 106 |
scope = self |
|
| 97 |
scope = self.scoped
|
|
| 107 | 98 |
project_conditions = [] |
| 108 | 99 |
if searchable_options.has_key?(:permission) |
| 109 | 100 |
project_conditions << Project.allowed_to_condition(user, searchable_options[:permission] || :view_project) |
| ... | ... | |
| 120 | 111 |
results = [] |
| 121 | 112 |
results_count = 0 |
| 122 | 113 |
|
| 123 |
scope = scope.scoped({:conditions => project_conditions}).scoped(find_options)
|
|
| 124 |
results_count = scope.count(:all) |
|
| 125 |
results = scope.find(:all, limit_options) |
|
| 114 |
scope = scope. |
|
| 115 |
includes(searchable_options[:include]). |
|
| 116 |
order("#{searchable_options[:order_column]} " + (options[:before] ? 'DESC' : 'ASC')).
|
|
| 117 |
where(project_conditions). |
|
| 118 |
where(tokens_conditions) |
|
| 119 |
|
|
| 120 |
results_count = scope.count |
|
| 121 |
|
|
| 122 |
scope_with_limit = scope.limit(options[:limit]) |
|
| 123 |
if options[:offset] |
|
| 124 |
scope_with_limit = scope_with_limit.where("#{searchable_options[:date_column]} #{options[:before] ? '<' : '>'} ?", options[:offset])
|
|
| 125 |
end |
|
| 126 |
results = scope_with_limit.all |
|
| 126 | 127 |
|
| 127 | 128 |
[results, results_count] |
| 128 | 129 |
end |
Also available in: Unified diff