summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2007-09-27 17:28:22 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2007-09-27 17:28:22 +0000
commita96421019f3a813b93798e4ec8099edd09f8a661 (patch)
tree102827aea563063b29ae21800bfff88fb16550f9 /lib
parentd723bea502faedb218f929b40849935d61c577e2 (diff)
downloadredmine-a96421019f3a813b93798e4ec8099edd09f8a661.tar.gz
redmine-a96421019f3a813b93798e4ec8099edd09f8a661.zip
Search engines now supports pagination.
Results are sorted in reverse chronological order. git-svn-id: http://redmine.rubyforge.org/svn/trunk@766 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib')
-rw-r--r--lib/plugins/acts_as_event/init.rb (renamed from lib/redmine/acts_as_event/init.rb)0
-rw-r--r--lib/plugins/acts_as_event/lib/acts_as_event.rb (renamed from lib/redmine/acts_as_event/lib/acts_as_event.rb)0
-rw-r--r--lib/plugins/acts_as_searchable/init.rb2
-rw-r--r--lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb89
-rw-r--r--lib/plugins/acts_as_watchable/init.rb (renamed from lib/redmine/acts_as_watchable/init.rb)0
-rw-r--r--lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb (renamed from lib/redmine/acts_as_watchable/lib/acts_as_watchable.rb)0
-rw-r--r--lib/redmine.rb2
7 files changed, 91 insertions, 2 deletions
diff --git a/lib/redmine/acts_as_event/init.rb b/lib/plugins/acts_as_event/init.rb
index 91051510a..91051510a 100644
--- a/lib/redmine/acts_as_event/init.rb
+++ b/lib/plugins/acts_as_event/init.rb
diff --git a/lib/redmine/acts_as_event/lib/acts_as_event.rb b/lib/plugins/acts_as_event/lib/acts_as_event.rb
index a0d1822ad..a0d1822ad 100644
--- a/lib/redmine/acts_as_event/lib/acts_as_event.rb
+++ b/lib/plugins/acts_as_event/lib/acts_as_event.rb
diff --git a/lib/plugins/acts_as_searchable/init.rb b/lib/plugins/acts_as_searchable/init.rb
new file mode 100644
index 000000000..063721756
--- /dev/null
+++ b/lib/plugins/acts_as_searchable/init.rb
@@ -0,0 +1,2 @@
+require File.dirname(__FILE__) + '/lib/acts_as_searchable'
+ActiveRecord::Base.send(:include, Redmine::Acts::Searchable)
diff --git a/lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb b/lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb
new file mode 100644
index 000000000..ee4d5d6f8
--- /dev/null
+++ b/lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb
@@ -0,0 +1,89 @@
+# redMine - project management software
+# Copyright (C) 2006-2007 Jean-Philippe Lang
+#
+# 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.
+#
+# 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.
+#
+# 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
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def acts_as_searchable(options = {})
+ return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods)
+
+ 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
+
+ if searchable_options[:project_key]
+ elsif column_names.include?('project_id')
+ searchable_options[:project_key] = "#{table_name}.project_id"
+ else
+ raise 'No project key defined.'
+ end
+
+ if searchable_options[:date_column]
+ elsif column_names.include?('created_on')
+ searchable_options[:date_column] = "#{table_name}.created_on"
+ else
+ raise 'No date column defined defined.'
+ end
+
+ send :include, Redmine::Acts::Searchable::InstanceMethods
+ end
+ end
+
+ module InstanceMethods
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def search(tokens, all_tokens, project, options={})
+ tokens = [] << tokens unless tokens.is_a?(Array)
+ find_options = {:include => searchable_options[:include]}
+ find_options[:limit] = options[:limit] if options[:limit]
+ find_options[:order] = "#{searchable_options[:date_column]} " + (options[:before] ? 'DESC' : 'ASC')
+
+ sql = ([ '(' + searchable_options[:columns].collect {|column| "(LOWER(#{column}) LIKE ?)"}.join(' OR ') + ')' ] * tokens.size).join(all_tokens ? ' AND ' : ' OR ')
+ if options[:offset]
+ sql = "(#{sql}) AND (#{searchable_options[:date_column]} " + (options[:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')"
+ end
+ find_options[:conditions] = [sql, * (tokens * searchable_options[:columns].size).sort]
+
+ results = with_scope(:find => {:conditions => ["#{searchable_options[:project_key]} = ?", project.id]}) do
+ find(:all, find_options)
+ end
+ if searchable_options[:with]
+ searchable_options[:with].each do |model, assoc|
+ results += model.to_s.camelcase.constantize.search(tokens, all_tokens, project, options).collect {|r| r.send assoc}
+ end
+ results.uniq!
+ end
+ results
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/redmine/acts_as_watchable/init.rb b/lib/plugins/acts_as_watchable/init.rb
index f39cc7d18..f39cc7d18 100644
--- a/lib/redmine/acts_as_watchable/init.rb
+++ b/lib/plugins/acts_as_watchable/init.rb
diff --git a/lib/redmine/acts_as_watchable/lib/acts_as_watchable.rb b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
index c789017e5..c789017e5 100644
--- a/lib/redmine/acts_as_watchable/lib/acts_as_watchable.rb
+++ b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
diff --git a/lib/redmine.rb b/lib/redmine.rb
index d14f886c2..8dd191c33 100644
--- a/lib/redmine.rb
+++ b/lib/redmine.rb
@@ -1,8 +1,6 @@
require 'redmine/access_control'
require 'redmine/menu_manager'
require 'redmine/mime_type'
-require 'redmine/acts_as_watchable/init'
-require 'redmine/acts_as_event/init'
require 'redmine/plugin'
begin