Module: Redmine::Acts::Searchable::InstanceMethods::ClassMethods
- Defined in:
- vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb
Instance Method Summary
-
- (Object) search(tokens, projects = nil, options = {})
Searches the model for the given tokens projects argument can be either nil (will search all projects), a project or an array of projects Returns the results and the results count.
Instance Method Details
- (Object) search(tokens, projects = nil, options = {})
Searches the model for the given tokens projects argument can be either nil (will search all projects), a project or an array of projects Returns the results and the results count
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb', line 67 def search(tokens, projects=nil, ={}) tokens = [] << tokens unless tokens.is_a?(Array) projects = [] << projects unless projects.nil? || projects.is_a?(Array) = {:include => [:include]} [:order] = "#{searchable_options[:order_column]} " + ([:before] ? 'DESC' : 'ASC') = {} [:limit] = [:limit] if [:limit] if [:offset] [:conditions] = "(#{searchable_options[:date_column]} " + ([:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')" end columns = [:columns] columns = columns[0..0] if [:titles_only] token_clauses = columns.collect {|column| "(LOWER(#{column}) LIKE ?)"} if ![:titles_only] && [:search_custom_fields] searchable_custom_field_ids = CustomField.find(:all, :select => 'id', :conditions => { :type => "#{self.name}CustomField", :searchable => true }).collect(&:id) if searchable_custom_field_ids.any? custom_field_sql = "#{table_name}.id IN (SELECT customized_id FROM #{CustomValue.table_name}" + " WHERE customized_type='#{self.name}' AND customized_id=#{table_name}.id AND LOWER(value) LIKE ?" + " AND #{CustomValue.table_name}.custom_field_id IN (#{searchable_custom_field_ids.join(',')}))" token_clauses << custom_field_sql end end sql = (['(' + token_clauses.join(' OR ') + ')'] * tokens.size).join([:all_words] ? ' AND ' : ' OR ') [:conditions] = [sql, * (tokens.collect {|w| "%#{w.downcase}%"} * token_clauses.size).sort] project_conditions = [] project_conditions << ([:permission].nil? ? Project.visible_by(User.current) : Project.allowed_to_condition(User.current, [:permission])) project_conditions << "#{searchable_options[:project_key]} IN (#{projects.collect(&:id).join(',')})" unless projects.nil? results = [] results_count = 0 with_scope(:find => {:conditions => project_conditions.join(' AND ')}) do with_scope(:find => ) do results_count = count(:all) results = find(:all, ) end end [results, results_count] end |