Chris@1295
|
1 # Redmine - project management software
|
Chris@1295
|
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
|
Chris@1295
|
3 #
|
Chris@1295
|
4 # This program is free software; you can redistribute it and/or
|
Chris@1295
|
5 # modify it under the terms of the GNU General Public License
|
Chris@1295
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@1295
|
7 # of the License, or (at your option) any later version.
|
Chris@1295
|
8 #
|
Chris@1295
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@1295
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@1295
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@1295
|
12 # GNU General Public License for more details.
|
Chris@1295
|
13 #
|
Chris@1295
|
14 # You should have received a copy of the GNU General Public License
|
Chris@1295
|
15 # along with this program; if not, write to the Free Software
|
Chris@1295
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@1295
|
17
|
Chris@1295
|
18 module Redmine
|
Chris@1295
|
19 module Acts
|
Chris@1295
|
20 module Searchable
|
Chris@1295
|
21 def self.included(base)
|
Chris@1295
|
22 base.extend ClassMethods
|
Chris@1295
|
23 end
|
Chris@1295
|
24
|
Chris@1295
|
25 module ClassMethods
|
Chris@1295
|
26 # Options:
|
Chris@1295
|
27 # * :columns - a column or an array of columns to search
|
Chris@1295
|
28 # * :project_key - project foreign key (default to project_id)
|
Chris@1295
|
29 # * :date_column - name of the datetime column (default to created_on)
|
Chris@1295
|
30 # * :sort_order - name of the column used to sort results (default to :date_column or created_on)
|
Chris@1295
|
31 # * :permission - permission required to search the model (default to :view_"objects")
|
Chris@1295
|
32 def acts_as_searchable(options = {})
|
Chris@1295
|
33 return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods)
|
Chris@1295
|
34
|
Chris@1295
|
35 cattr_accessor :searchable_options
|
Chris@1295
|
36 self.searchable_options = options
|
Chris@1295
|
37
|
Chris@1295
|
38 if searchable_options[:columns].nil?
|
Chris@1295
|
39 raise 'No searchable column defined.'
|
Chris@1295
|
40 elsif !searchable_options[:columns].is_a?(Array)
|
Chris@1295
|
41 searchable_options[:columns] = [] << searchable_options[:columns]
|
Chris@1295
|
42 end
|
Chris@1295
|
43
|
Chris@1295
|
44 searchable_options[:project_key] ||= "#{table_name}.project_id"
|
Chris@1295
|
45 searchable_options[:date_column] ||= "#{table_name}.created_on"
|
Chris@1295
|
46 searchable_options[:order_column] ||= searchable_options[:date_column]
|
Chris@1295
|
47
|
Chris@1295
|
48 # Should we search custom fields on this model ?
|
Chris@1295
|
49 searchable_options[:search_custom_fields] = !reflect_on_association(:custom_values).nil?
|
Chris@1295
|
50
|
Chris@1295
|
51 send :include, Redmine::Acts::Searchable::InstanceMethods
|
Chris@1295
|
52 end
|
Chris@1295
|
53 end
|
Chris@1295
|
54
|
Chris@1295
|
55 module InstanceMethods
|
Chris@1295
|
56 def self.included(base)
|
Chris@1295
|
57 base.extend ClassMethods
|
Chris@1295
|
58 end
|
Chris@1295
|
59
|
Chris@1295
|
60 module ClassMethods
|
Chris@1295
|
61 # Searches the model for the given tokens
|
Chris@1295
|
62 # projects argument can be either nil (will search all projects), a project or an array of projects
|
Chris@1295
|
63 # Returns the results and the results count
|
Chris@1295
|
64 def search(tokens, projects=nil, options={})
|
Chris@1295
|
65 if projects.is_a?(Array) && projects.empty?
|
Chris@1295
|
66 # no results
|
Chris@1295
|
67 return [[], 0]
|
Chris@1295
|
68 end
|
Chris@1295
|
69
|
Chris@1295
|
70 # TODO: make user an argument
|
Chris@1295
|
71 user = User.current
|
Chris@1295
|
72 tokens = [] << tokens unless tokens.is_a?(Array)
|
Chris@1295
|
73 projects = [] << projects unless projects.nil? || projects.is_a?(Array)
|
Chris@1295
|
74
|
Chris@1295
|
75 limit_options = {}
|
Chris@1295
|
76 limit_options[:limit] = options[:limit] if options[:limit]
|
Chris@1295
|
77
|
Chris@1295
|
78 columns = searchable_options[:columns]
|
Chris@1295
|
79 columns = columns[0..0] if options[:titles_only]
|
Chris@1295
|
80
|
Chris@1295
|
81 token_clauses = columns.collect {|column| "(LOWER(#{column}) LIKE ?)"}
|
Chris@1295
|
82
|
Chris@1295
|
83 if !options[:titles_only] && searchable_options[:search_custom_fields]
|
Chris@1295
|
84 searchable_custom_field_ids = CustomField.where(:type => "#{self.name}CustomField", :searchable => true).pluck(:id)
|
Chris@1295
|
85 if searchable_custom_field_ids.any?
|
Chris@1295
|
86 custom_field_sql = "#{table_name}.id IN (SELECT customized_id FROM #{CustomValue.table_name}" +
|
Chris@1295
|
87 " WHERE customized_type='#{self.name}' AND customized_id=#{table_name}.id AND LOWER(value) LIKE ?" +
|
Chris@1295
|
88 " AND #{CustomValue.table_name}.custom_field_id IN (#{searchable_custom_field_ids.join(',')}))"
|
Chris@1295
|
89 token_clauses << custom_field_sql
|
Chris@1295
|
90 end
|
Chris@1295
|
91 end
|
Chris@1295
|
92
|
Chris@1295
|
93 sql = (['(' + token_clauses.join(' OR ') + ')'] * tokens.size).join(options[:all_words] ? ' AND ' : ' OR ')
|
Chris@1295
|
94
|
Chris@1295
|
95 tokens_conditions = [sql, * (tokens.collect {|w| "%#{w.downcase}%"} * token_clauses.size).sort]
|
Chris@1295
|
96
|
Chris@1295
|
97 scope = self.scoped
|
Chris@1295
|
98 project_conditions = []
|
Chris@1295
|
99 if searchable_options.has_key?(:permission)
|
Chris@1295
|
100 project_conditions << Project.allowed_to_condition(user, searchable_options[:permission] || :view_project)
|
Chris@1295
|
101 elsif respond_to?(:visible)
|
Chris@1295
|
102 scope = scope.visible(user)
|
Chris@1295
|
103 else
|
Chris@1295
|
104 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."
|
Chris@1295
|
105 project_conditions << Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym)
|
Chris@1295
|
106 end
|
Chris@1295
|
107 # TODO: use visible scope options instead
|
Chris@1295
|
108 project_conditions << "#{searchable_options[:project_key]} IN (#{projects.collect(&:id).join(',')})" unless projects.nil?
|
Chris@1295
|
109 project_conditions = project_conditions.empty? ? nil : project_conditions.join(' AND ')
|
Chris@1295
|
110
|
Chris@1295
|
111 results = []
|
Chris@1295
|
112 results_count = 0
|
Chris@1295
|
113
|
Chris@1295
|
114 scope = scope.
|
Chris@1295
|
115 includes(searchable_options[:include]).
|
Chris@1295
|
116 order("#{searchable_options[:order_column]} " + (options[:before] ? 'DESC' : 'ASC')).
|
Chris@1295
|
117 where(project_conditions).
|
Chris@1295
|
118 where(tokens_conditions)
|
Chris@1295
|
119
|
Chris@1295
|
120 results_count = scope.count
|
Chris@1295
|
121
|
Chris@1295
|
122 scope_with_limit = scope.limit(options[:limit])
|
Chris@1295
|
123 if options[:offset]
|
Chris@1295
|
124 scope_with_limit = scope_with_limit.where("#{searchable_options[:date_column]} #{options[:before] ? '<' : '>'} ?", options[:offset])
|
Chris@1295
|
125 end
|
Chris@1295
|
126 results = scope_with_limit.all
|
Chris@1295
|
127
|
Chris@1295
|
128 [results, results_count]
|
Chris@1295
|
129 end
|
Chris@1295
|
130 end
|
Chris@1295
|
131 end
|
Chris@1295
|
132 end
|
Chris@1295
|
133 end
|
Chris@1295
|
134 end
|