diff plugins/redmine_bibliography/lib/bibliography/user_author_patch.rb @ 1441:8d721cac2925 biblio_alt_search_auth

Addresses Bug #763 - patched the User class adding a new scope (name_like), that doesn't use the email address to search for a user.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Fri, 11 Oct 2013 13:49:02 +0100
parents d0d6bbe9f2e0
children
line wrap: on
line diff
--- a/plugins/redmine_bibliography/lib/bibliography/user_author_patch.rb	Thu Oct 10 19:42:43 2013 +0100
+++ b/plugins/redmine_bibliography/lib/bibliography/user_author_patch.rb	Fri Oct 11 13:49:02 2013 +0100
@@ -4,15 +4,34 @@
   module UserAuthorPatch
     def self.included(base)
       base.send(:include, InstanceMethods)
-      extend ClassMethods
+
+      base.class_eval do
+        # adapted from the app/models/principals_model.rb
+        # to remove the email address from the search
+        scope :name_like, lambda {|q|
+          q = q.to_s
+          if q.blank?
+            where({})
+          else
+            pattern = "%#{q}%"
+            sql = %w(login firstname lastname).map {|column| "LOWER(#{  table_name}.    #{column}) LIKE LOWER(:p)"}.join(" OR ")
+            params = {:p => pattern}
+            if q =~ /^(.+)\s+(.+)$/
+              a, b = "#{$1}%", "#{$2}%"
+              sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND  LOWER    (#{table_name}.lastname) LIKE LOWER(:b))"
+              sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND  LOWER    (#{table_name}.lastname) LIKE LOWER(:a))"
+              params.merge!(:a => a, :b => b)
+            end
+          where(sql, params)
+          end
+        }
+      end #base.class_eval
 
     end #self.included
 
-    module ClassMethods
-    end
-
     module InstanceMethods
 
+      # todo: deprecated? ~lf.20131011
       def institution
         unless self.ssamr_user_detail.nil?
           institution_name = self.ssamr_user_detail.institution_name