##// END OF EJS Templates
Adds closed_on to issues API responses (#824)....
Adds closed_on to issues API responses (#824). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11411 e93f8b46-1217-0410-a6f0-8f06a7374b81

File last commit:

r10939:e396a0eebe07
r11181:7a1af68178a1
Show More
acts_as_searchable.rb
134 lines | 5.9 KiB | text/x-ruby | RubyLexer
Jean-Philippe Lang
Makes search providers use visible scopes....
r5207 # Redmine - project management software
Jean-Philippe Lang
Copyright for 2013 (#12788)....
r10939 # Copyright (C) 2006-2013 Jean-Philippe Lang
Jean-Philippe Lang
Search engines now supports pagination....
r755 #
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171 #
Jean-Philippe Lang
Search engines now supports pagination....
r755 # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171 #
Jean-Philippe Lang
Search engines now supports pagination....
r755 # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module Redmine
module Acts
module Searchable
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171 def self.included(base)
Jean-Philippe Lang
Search engines now supports pagination....
r755 base.extend ClassMethods
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171 end
Jean-Philippe Lang
Search engines now supports pagination....
r755
module ClassMethods
Jean-Philippe Lang
Search engine: display total results count (#906) and count by result type....
r1664 # Options:
# * :columns - a column or an array of columns to search
# * :project_key - project foreign key (default to project_id)
# * :date_column - name of the datetime column (default to created_on)
# * :sort_order - name of the column used to sort results (default to :date_column or created_on)
# * :permission - permission required to search the model (default to :view_"objects")
Jean-Philippe Lang
Search engines now supports pagination....
r755 def acts_as_searchable(options = {})
return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods)
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Search engines now supports pagination....
r755 cattr_accessor :searchable_options
self.searchable_options = options
if searchable_options[:columns].nil?
raise 'No searchable column defined.'
elsif !searchable_options[:columns].is_a?(Array)
searchable_options[:columns] = [] << searchable_options[:columns]
end
Jean-Philippe Lang
Fixed: searchable model can't be loaded if table is not yet created (#1421)....
r1665 searchable_options[:project_key] ||= "#{table_name}.project_id"
searchable_options[:date_column] ||= "#{table_name}.created_on"
Jean-Philippe Lang
Search engine: display total results count (#906) and count by result type....
r1664 searchable_options[:order_column] ||= searchable_options[:date_column]
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Search engine: issue custom fields can now be searched....
r981 # Should we search custom fields on this model ?
searchable_options[:search_custom_fields] = !reflect_on_association(:custom_values).nil?
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Search engines now supports pagination....
r755 send :include, Redmine::Acts::Searchable::InstanceMethods
end
end
module InstanceMethods
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
Jean-Philippe Lang
Search engine: display total results count (#906) and count by result type....
r1664 # Searches the model for the given tokens
Jean-Philippe Lang
Ability to search all projects or the projects the user belongs to (#791)....
r1420 # projects argument can be either nil (will search all projects), a project or an array of projects
Jean-Philippe Lang
Search engine: display total results count (#906) and count by result type....
r1664 # Returns the results and the results count
Jean-Philippe Lang
Fixed: search engine may reveal private projects (#1613)....
r1635 def search(tokens, projects=nil, options={})
Jean-Philippe Lang
Additional tests for SearchController and handle my_projects scope without memberships....
r8146 if projects.is_a?(Array) && projects.empty?
# no results
return [[], 0]
end
Jean-Philippe Lang
Makes search providers use visible scopes....
r5207 # TODO: make user an argument
user = User.current
Jean-Philippe Lang
Search engines now supports pagination....
r755 tokens = [] << tokens unless tokens.is_a?(Array)
Jean-Philippe Lang
Ability to search all projects or the projects the user belongs to (#791)....
r1420 projects = [] << projects unless projects.nil? || projects.is_a?(Array)
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Search engine: display total results count (#906) and count by result type....
r1664 limit_options = {}
limit_options[:limit] = options[:limit] if options[:limit]
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Search engine: added a checkbox to search titles only (usefull when searching on common words)....
r829 columns = searchable_options[:columns]
Jean-Philippe Lang
Fixed: 'search titles only' box ignored after one search is done on titles only....
r1667 columns = columns[0..0] if options[:titles_only]
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Search engine: issue custom fields can now be searched....
r981 token_clauses = columns.collect {|column| "(LOWER(#{column}) LIKE ?)"}
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Search engine: issue custom fields can now be searched....
r981 if !options[:titles_only] && searchable_options[:search_custom_fields]
Jean-Philippe Lang
Replaces find(:all) calls....
r10693 searchable_custom_field_ids = CustomField.where(:type => "#{self.name}CustomField", :searchable => true).pluck(:id)
Jean-Philippe Lang
Search engine: issue custom fields can now be searched....
r981 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
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Fixed search with all words (broken in r994)....
r1078 sql = (['(' + token_clauses.join(' OR ') + ')'] * tokens.size).join(options[:all_words] ? ' AND ' : ' OR ')
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Replaces find(:all) calls in acts_as_searchable....
r10700 tokens_conditions = [sql, * (tokens.collect {|w| "%#{w.downcase}%"} * token_clauses.size).sort]
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Replaces find(:all) calls in acts_as_searchable....
r10700 scope = self.scoped
Jean-Philippe Lang
Ability to search all projects or the projects the user belongs to (#791)....
r1420 project_conditions = []
Jean-Philippe Lang
Makes search providers use visible scopes....
r5207 if searchable_options.has_key?(:permission)
project_conditions << Project.allowed_to_condition(user, searchable_options[:permission] || :view_project)
elsif respond_to?(:visible)
scope = scope.visible(user)
else
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."
project_conditions << Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym)
end
# TODO: use visible scope options instead
Jean-Philippe Lang
Ability to search all projects or the projects the user belongs to (#791)....
r1420 project_conditions << "#{searchable_options[:project_key]} IN (#{projects.collect(&:id).join(',')})" unless projects.nil?
Jean-Philippe Lang
Makes search providers use visible scopes....
r5207 project_conditions = project_conditions.empty? ? nil : project_conditions.join(' AND ')
Toshi MARUYAMA
remove trailing white-spaces from vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb...
r9171
Jean-Philippe Lang
Search engine: display total results count (#906) and count by result type....
r1664 results = []
results_count = 0
Jean-Philippe Lang
Use #scoped instead of .with_scope...
r8340
Jean-Philippe Lang
Replaces find(:all) calls in acts_as_searchable....
r10700 scope = scope.
includes(searchable_options[:include]).
order("#{searchable_options[:order_column]} " + (options[:before] ? 'DESC' : 'ASC')).
where(project_conditions).
where(tokens_conditions)
results_count = scope.count
scope_with_limit = scope.limit(options[:limit])
if options[:offset]
scope_with_limit = scope_with_limit.where("#{searchable_options[:date_column]} #{options[:before] ? '<' : '>'} ?", options[:offset])
end
results = scope_with_limit.all
Jean-Philippe Lang
Use #scoped instead of .with_scope...
r8340
Jean-Philippe Lang
Search engine: display total results count (#906) and count by result type....
r1664 [results, results_count]
Jean-Philippe Lang
Search engines now supports pagination....
r755 end
end
end
end
end
end