Added ArvadosModel.search_for_user to perform full-text search on a model.
[arvados.git] / services / api / app / models / arvados_model.rb
index 4f2aa72161ef903f638cb4e62d4996d774c3e415..4e406de9d5c55b538b7f41ca1b3f3271ea3e4234 100644 (file)
@@ -45,6 +45,31 @@ class ArvadosModel < ActiveRecord::Base
     end.compact
   end
 
+  # searchable? indicates whether a model is subject to full-text
+  # search through the workbench.  Models which include data that is
+  # interesting to a full-text search (e.g. Collection, Specimen, Trait)
+  # should override this to return 'true'.
+  def self.searchable?
+    false
+  end
+
+  # search all "searchable" columns of this table for query_str.
+  def self.search_for_user(user, query_str)
+    return [] unless self.searchable?
+    ilikes = []
+    ilike_params = []
+    self.searchable_columns.each do |column|
+      ilikes << "#{table_name}.#{column} ilike ?"
+      ilike_params << "%#{query_str}%"
+    end
+    if ilikes.empty?
+      return []
+    else
+      query_conditions = [ ilikes.join(' or ') ] + ilike_params
+      return self.readable_by(user).where(query_conditions)
+    end
+  end
+
   def eager_load_associations
     self.class.columns.each do |col|
       re = col.name.match /^(.*)_kind$/