Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: module Redmine Chris@1494: module Acts Chris@1494: module Searchable Chris@1494: def self.included(base) Chris@1494: base.extend ClassMethods Chris@1494: end Chris@1494: Chris@1494: module ClassMethods Chris@1494: # Options: Chris@1494: # * :columns - a column or an array of columns to search Chris@1494: # * :project_key - project foreign key (default to project_id) Chris@1494: # * :date_column - name of the datetime column (default to created_on) Chris@1494: # * :sort_order - name of the column used to sort results (default to :date_column or created_on) Chris@1494: # * :permission - permission required to search the model (default to :view_"objects") Chris@1494: def acts_as_searchable(options = {}) Chris@1494: return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods) Chris@1494: Chris@1494: cattr_accessor :searchable_options Chris@1494: self.searchable_options = options Chris@1494: Chris@1494: if searchable_options[:columns].nil? Chris@1494: raise 'No searchable column defined.' Chris@1494: elsif !searchable_options[:columns].is_a?(Array) Chris@1494: searchable_options[:columns] = [] << searchable_options[:columns] Chris@1494: end Chris@1494: Chris@1494: searchable_options[:project_key] ||= "#{table_name}.project_id" Chris@1494: searchable_options[:date_column] ||= "#{table_name}.created_on" Chris@1494: searchable_options[:order_column] ||= searchable_options[:date_column] Chris@1494: Chris@1494: # Should we search custom fields on this model ? Chris@1494: searchable_options[:search_custom_fields] = !reflect_on_association(:custom_values).nil? Chris@1494: Chris@1494: send :include, Redmine::Acts::Searchable::InstanceMethods Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: module InstanceMethods Chris@1494: def self.included(base) Chris@1494: base.extend ClassMethods Chris@1494: end Chris@1494: Chris@1494: module ClassMethods Chris@1494: # Searches the model for the given tokens Chris@1494: # projects argument can be either nil (will search all projects), a project or an array of projects Chris@1494: # Returns the results and the results count Chris@1494: def search(tokens, projects=nil, options={}) Chris@1494: if projects.is_a?(Array) && projects.empty? Chris@1494: # no results Chris@1494: return [[], 0] Chris@1494: end Chris@1494: Chris@1494: # TODO: make user an argument Chris@1494: user = User.current Chris@1494: tokens = [] << tokens unless tokens.is_a?(Array) Chris@1494: projects = [] << projects unless projects.nil? || projects.is_a?(Array) Chris@1494: Chris@1494: limit_options = {} Chris@1494: limit_options[:limit] = options[:limit] if options[:limit] Chris@1494: Chris@1494: columns = searchable_options[:columns] Chris@1494: columns = columns[0..0] if options[:titles_only] Chris@1494: Chris@1494: token_clauses = columns.collect {|column| "(LOWER(#{column}) LIKE ?)"} Chris@1494: Chris@1494: if !options[:titles_only] && searchable_options[:search_custom_fields] Chris@1494: searchable_custom_fields = CustomField.where(:type => "#{self.name}CustomField", :searchable => true) Chris@1494: searchable_custom_fields.each do |field| Chris@1494: sql = "#{table_name}.id IN (SELECT customized_id FROM #{CustomValue.table_name}" + Chris@1494: " WHERE customized_type='#{self.name}' AND customized_id=#{table_name}.id AND LOWER(value) LIKE ?" + Chris@1494: " AND #{CustomValue.table_name}.custom_field_id = #{field.id})" + Chris@1494: " AND #{field.visibility_by_project_condition(searchable_options[:project_key], user)}" Chris@1494: token_clauses << sql Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: sql = (['(' + token_clauses.join(' OR ') + ')'] * tokens.size).join(options[:all_words] ? ' AND ' : ' OR ') Chris@1494: Chris@1494: tokens_conditions = [sql, * (tokens.collect {|w| "%#{w.downcase}%"} * token_clauses.size).sort] Chris@1494: Chris@1494: scope = self.scoped Chris@1494: project_conditions = [] Chris@1494: if searchable_options.has_key?(:permission) Chris@1494: project_conditions << Project.allowed_to_condition(user, searchable_options[:permission] || :view_project) Chris@1494: elsif respond_to?(:visible) Chris@1494: scope = scope.visible(user) Chris@1494: else Chris@1494: 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@1494: project_conditions << Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym) Chris@1494: end Chris@1494: # TODO: use visible scope options instead Chris@1494: project_conditions << "#{searchable_options[:project_key]} IN (#{projects.collect(&:id).join(',')})" unless projects.nil? Chris@1494: project_conditions = project_conditions.empty? ? nil : project_conditions.join(' AND ') Chris@1494: Chris@1494: results = [] Chris@1494: results_count = 0 Chris@1494: Chris@1494: scope = scope. Chris@1494: includes(searchable_options[:include]). Chris@1494: order("#{searchable_options[:order_column]} " + (options[:before] ? 'DESC' : 'ASC')). Chris@1494: where(project_conditions). Chris@1494: where(tokens_conditions) Chris@1494: Chris@1494: results_count = scope.count Chris@1494: Chris@1494: scope_with_limit = scope.limit(options[:limit]) Chris@1494: if options[:offset] Chris@1494: scope_with_limit = scope_with_limit.where("#{searchable_options[:date_column]} #{options[:before] ? '<' : '>'} ?", options[:offset]) Chris@1494: end Chris@1494: results = scope_with_limit.all Chris@1494: Chris@1494: [results, results_count] Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end