Skip to content

Instantly share code, notes, and snippets.

@JonKernPA
Created August 5, 2010 03:43
Show Gist options
  • Save JonKernPA/509193 to your computer and use it in GitHub Desktop.
Save JonKernPA/509193 to your computer and use it in GitHub Desktop.
Demonstrating various searches in mongomapper
class MessageLog
include MongoMapper::Document
# Attributes ::::::::::::::::::::::::::::::::::::::::::::::::::::::
key :repeat_count, Integer, :default => 0
key :patient_name, String, :default => 'Unknown', :index => true
key :patient_num, String, :default => 'Unknown', :index => true
key :patient_emr, String, :default => 'Unknown', :index => true
# Doctor info (number, name, group)
key :doctor, String, :default => 'Unknown', :index => true
...
# Scopes :::::::::::::::::::::::::::::::::::::::::::::::::::::
scope :by_emr, lambda {|emr| where(:patient_emr => emr)}
end
And searches like this in the controller that allow numbers as text
(from a UI search form):
...
def search
@message_logs = nil
case params[:type]
when "name"
@message_logs = MessageLog.paginate( :page =>params[:page],
'$where' => "function() { return
this.patient.match(/#{params[:finder]}/i); }")
when "patient_num"
@message_logs = MessageLog.paginate(:patient_num =>
params[:finder], :page =>params[:page])
when "emr_num"
@message_logs = MessageLog.by_emr(params[:finder], :page
=>params[:page]).paginate
when "doctor"
@message_logs = MessageLog.paginate( :page =>params[:page],
'$where' => "function() { return
this.doctor.match(/#{params[:finder]}/i); }")
when "control_id"
@message_logs = MessageLog.paginate( :control_id =>
params[:finder], :page =>params[:page])
end
end
...
And for the stats, I search with a straight-up number:
@num_repeated = MessageLog.count( :repeat_count.gt => 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment