Patterns with Valuable - the ModelSearch

By: Johnathon Wright on: August 24, 2012

I find that I write a lot of "advanced search" stuff, and that modeling an advanced search, and having that be in a separate class, is very good for my sanity.

h2. DataSearch model

The schema is below if you're interested. These models usually start in lib, and then move to app/search once I have four or five.

describe DataSearch do it 'finds data' do datum = Factory.create(:datum) DataSearch.new.results.should include(datum) end end

So this is a "sanity check" test. I typically test what isn't included. But that could mean that I could get half-way through, write some poorly-thought-out condition that excludes everything, and then have to spend 30 minutes tracking it down. Instead, I just start by asserting that everything is included by default. In some cases, you want nothing to result if there are no conditions -- no problem, just write a test that does return some result.

``` class DataSearch < Valuable has_collection :codes

def results Datum.find(:all) end ```

h3. I want only certain fields


describe DataSearch do it 'filters by code' do datum = FactoryGirl.create(:datum, :code => 'hr') DataSearch.new(:codes => ['midi-chlorians']).results.should_not include(datum) end

end

and then


class DataSearch < Valuable

has_collection :codes

def condition_params { :codes => codes } end

def conditions out = [] out << 'code IN (:codes)' if codes.not.empty? out.join(' AND ') end

def results Datum.find(:all, :conditions => [conditions, condition_params]) end

end

h3. Filter by visit


describe DataSearch do it 'filters by visit' do one = FactoryGirl.create(:order) two = FactoryGirl.create(:order)

doppleganger = FactoryGirl.create(:datum, :order => one)
DataSearch.new(:visit_id => two.visit_id).results.should_not include(doppleganger)

end

end

and then


class DataSearch < Valuable

hascollection :codes hasvalue :visit_id, :klass => :integer

def conditionparams { :codes => codes, :visitid => visit_id } end

def conditions out = [] out << 'code IN (:codes)' if codes.not.empty? out << 'orders.visitid = :visitid' if visit_id out.join(' AND ') end

def joins out = [] out << 'LEFT OUTER JOIN orders ON data.orderid = orders.id' if visitid out.join(' ') unless out.empty? end

def results Datum.find(:all, :conditions => [conditions, condition_params], :joins => joins, :group => 'data.id') end

end

h2. The Schema

h3. Timepoint

A patient at a point in time. * patient_id * date * physician

h3. Order

a Physician orders some bloodwork or some other test. * timepointid * panelname

h3. Datum

  • order_id
  • code
  • value
  • units

Datum.validatesuniquenessof :code, :scope => :order_id





Comments:

kyrie irving shoes said: I'm also commenting to make you understand of the wonderful discovery my friend's daughter went through viewing your web site. She mastered plenty of things, which included how it is like to have an amazing helping mood to let most people very easily comprehend specified hard to do matters. You truly did more than our own expectations. Thank you for showing these good, trustworthy, informative and even fun guidance on this topic to Gloria. kyrie irving shoes [url=http://www.kyrieirving-shoes.us.com]kyrie irving shoes[/url]

jordan shoes said: I precisely wanted to appreciate you once more. I do not know the things that I would've gone through in the absence of the actual strategies revealed by you about my situation. It has been an absolute challenging matter in my position, but looking at the very well-written tactic you processed it made me to jump for fulfillment. I am happier for the work and pray you recognize what a powerful job you are always getting into teaching other individuals through your blog post. More than likely you have never come across all of us. jordan shoes

off white jordan 1 said: I enjoy you because of your whole work on this web page. Debby take interest in getting into investigations and it's easy to understand why. Most people notice all concerning the lively tactic you present efficient tips and tricks by means of this website and increase response from some others on that issue and our favorite simple princess is truly becoming educated a lot of things. Take advantage of the remaining portion of the new year. You are always conducting a very good job. off white jordan 1

jordan shoes said: I enjoy you because of each of your efforts on this site. My niece takes pleasure in getting into investigation and it's simple to grasp why. We learn all about the powerful tactic you offer both useful and interesting guidance by means of your web site and improve contribution from visitors about this article and our simple princess is without a doubt understanding a great deal. Enjoy the remaining portion of the new year. You are doing a terrific job. jordan shoes

christian louboutin outlet said: Thank you a lot for giving everyone a very nice chance to discover important secrets from this website. It can be very excellent and also packed with amusement for me personally and my office fellow workers to search your web site not less than three times in a week to read the latest things you have got. And definitely, I am just usually impressed with your stunning information served by you. Selected 4 facts in this article are basically the most impressive we've ever had. christian louboutin outlet

goyard tote said: I really wanted to compose a brief remark to be able to thank you for these great secrets you are writing at this website. My extensive internet look up has finally been rewarded with excellent knowledge to go over with my contacts. I 'd mention that we website visitors are undoubtedly lucky to exist in a great community with so many wonderful professionals with very beneficial solutions. I feel extremely grateful to have encountered your site and look forward to some more brilliant times reading here. Thank you again for everything. goyard tote

nike sneakers said: I precisely had to appreciate you once again. I'm not certain the things that I might have handled in the absence of these tactics contributed by you over such question. It was before an absolute traumatic condition in my circumstances, nevertheless viewing your professional form you resolved that forced me to cry with fulfillment. I'm happy for the information and then believe you comprehend what a powerful job you have been accomplishing educating the mediocre ones all through a web site. I'm certain you've never got to know all of us. nike sneakers

balenciaga shoes said: Needed to write you that little bit of note in order to give many thanks yet again for your pleasing pointers you have contributed here. It has been simply remarkably open-handed of people like you in giving freely all that most of us could possibly have supplied for an ebook in order to make some profit for themselves, mostly considering that you might well have done it in the event you desired. Those solutions additionally acted like the good way to be certain that some people have a similar keenness much like my personal own to figure out somewhat more in terms of this problem. I think there are several more enjoyable opportunities ahead for individuals that read through your site. balenciaga shoes

supreme new york said: I precisely wished to say thanks once more. I'm not certain the things that I would have handled without the type of tricks documented by you over this concern. Entirely was an absolute frightful circumstance for me, however , understanding a professional form you dealt with the issue took me to jump with gladness. I'm just grateful for your information and expect you find out what a great job that you're putting in training most people by way of a web site. Most likely you have never got to know all of us. supreme new york

golden goose said: I and my friends appeared to be going through the excellent secrets and techniques located on your web site and then unexpectedly developed a terrible suspicion I had not thanked the website owner for those strategies. All the men are already so thrilled to learn them and have now in fact been using those things. Many thanks for genuinely so accommodating as well as for considering such important useful guides millions of individuals are really wanting to discover. My sincere regret for not expressing appreciation to you sooner. golden goose

yeezy 350 said: I have to show appreciation to you for rescuing me from this particular challenge. Right after searching throughout the the web and obtaining principles that were not productive, I was thinking my life was done. Being alive without the approaches to the problems you've fixed by way of the website is a serious case, as well as the ones that would have in a wrong way damaged my career if I hadn't noticed your website. That mastery and kindness in maneuvering all the pieces was very helpful. I'm not sure what I would've done if I hadn't encountered such a solution like this. I am able to now look forward to my future. Thank you very much for this expert and effective help. I will not think twice to recommend your blog post to any individual who should receive tips about this matter. yeezy 350

kyrie irving shoes said: I precisely desired to say thanks yet again. I'm not certain what I might have worked on in the absence of those recommendations shared by you on my topic. Previously it was a real hard dilemma for me, nevertheless being able to see the very well-written manner you processed the issue took me to jump over delight. I am just happy for the work and as well , believe you are aware of a powerful job that you are doing training many people with the aid of a web site. Most likely you have never got to know any of us. kyrie irving shoes

curry 7 said: I wanted to compose you that very little remark to give thanks yet again for your remarkable thoughts you have shared above. It's quite shockingly open-handed with people like you to make unreservedly exactly what many people could possibly have supplied for an e-book in order to make some dough for themselves, mostly now that you might have done it if you ever decided. The inspiring ideas in addition served to be a fantastic way to be aware that someone else have the identical zeal similar to mine to see a great deal more concerning this condition. I'm certain there are numerous more enjoyable moments up front for those who see your website. curry 7

chrome hearts store said: I simply wished to say thanks again. I'm not certain the things I would have used without the ideas documented by you concerning such theme. Entirely was an absolute frightful matter in my opinion, however , being able to see the professional fashion you processed it made me to jump for joy. I am just thankful for your guidance and believe you comprehend what a great job you happen to be doing instructing men and women thru your blog. Most probably you haven't come across all of us. chrome hearts store

yeezy 350 said: I wanted to draft you a little word in order to thank you yet again relating to the amazing techniques you have shared at this time. It has been certainly remarkably generous with people like you to make freely what many individuals could possibly have distributed as an e book to help with making some profit for themselves, especially given that you might well have tried it if you desired. These concepts also served like the great way to understand that someone else have similar dream the same as my own to find out more and more when considering this issue. I'm sure there are numerous more pleasurable times up front for many who examine your blog. yeezy 350

yeezy 700 said: I want to express my admiration for your kind-heartedness for people who need assistance with your subject matter. Your real dedication to getting the message throughout appeared to be quite invaluable and has allowed others like me to arrive at their targets. The valuable recommendations denotes much to me and further more to my mates. Thank you; from everyone of us. yeezy 700

yeezy boost 350 said: I would like to express my appreciation for your kindness supporting those individuals that require help on this niche. Your special dedication to passing the message all around had been amazingly advantageous and have specifically helped professionals just like me to realize their dreams. Your personal valuable tips and hints denotes a great deal to me and far more to my peers. Thanks a ton; from all of us. yeezy boost 350

golden goose said: A lot of thanks for each of your efforts on this site. Betty really loves participating in investigations and it is easy to see why. A lot of people hear all regarding the lively way you create very useful thoughts through the blog and in addition cause response from some other people on the area plus our own princess is actually studying a great deal. Enjoy the rest of the new year. You're the one doing a brilliant job. golden goose

longchamp bags said: I must show my appreciation to the writer just for rescuing me from this circumstance. Just after researching through the the web and seeing thoughts which are not helpful, I assumed my life was done. Being alive without the presence of solutions to the problems you have resolved all through your short article is a serious case, as well as the ones that might have negatively damaged my career if I hadn't come across your blog post. That training and kindness in controlling all the stuff was invaluable. I am not sure what I would have done if I hadn't come across such a point like this. I'm able to at this moment look forward to my future. Thanks a lot so much for this expert and results-oriented help. I won't hesitate to propose the blog to anyone who should receive tips about this subject. longchamp bags

kyrie 5 said: My wife and i got now joyful when Peter managed to deal with his preliminary research via the precious recommendations he grabbed when using the web page. It is now and again perplexing to just possibly be making a gift of methods which usually some people might have been trying to sell. And we remember we need the blog owner to thank because of that. The most important illustrations you've made, the straightforward web site navigation, the relationships your site aid to promote - it's got mostly impressive, and it is aiding our son in addition to the family believe that the theme is interesting, which is certainly particularly pressing. Many thanks for the whole lot! kyrie 5

supreme outlet said: I simply wanted to make a brief note to be able to express gratitude to you for those precious tips and tricks you are showing on this website. My considerable internet look up has now been recognized with really good ideas to talk about with my family members. I would assume that we readers are undoubtedly endowed to live in a fantastic community with so many perfect professionals with insightful tactics. I feel extremely happy to have discovered the web pages and look forward to many more cool moments reading here. Thanks a lot again for all the details. supreme outlet

cheap jordans said: I intended to compose you this tiny word to be able to thank you as before for these fantastic secrets you have discussed on this page. It has been really surprisingly open-handed with people like you to present unreservedly what a few individuals would've made available as an ebook to generate some bucks for themselves, primarily considering the fact that you could have done it in the event you wanted. The secrets likewise served as a easy way to realize that most people have a similar passion just as mine to understand significantly more around this condition. I know there are millions of more enjoyable opportunities up front for those who see your site. cheap jordans

golden goose said: Thank you so much for providing individuals with an extraordinarily breathtaking possiblity to read articles and blog posts from here. It really is very great and packed with amusement for me personally and my office peers to search the blog nearly thrice a week to see the fresh secrets you have got. And definitely, I'm just always amazed with the awesome tips and hints you give. Selected 1 facts in this posting are in truth the finest I've had. golden goose

hermes bag said: I have to point out my gratitude for your generosity giving support to visitors who really need assistance with this particular topic. Your real dedication to getting the message throughout appeared to be definitely helpful and has truly helped guys just like me to attain their endeavors. Your own informative report entails a lot to me and especially to my colleagues. Many thanks; from each one of us. hermes bag

air jordan said: Thanks a lot for giving everyone a very nice possiblity to read in detail from this site. It really is so lovely and stuffed with a great time for me personally and my office mates to visit your website on the least thrice weekly to read through the fresh issues you will have. Of course, I'm just always impressed with all the terrific principles you serve. Selected 3 tips on this page are honestly the most suitable we have had. air jordan

supreme clothing said: Thank you for each of your effort on this website. My aunt really likes making time for research and it is easy to understand why. All of us know all concerning the powerful method you provide good tips through your web blog and in addition increase participation from other individuals on that point plus our child is in fact understanding so much. Take pleasure in the rest of the year. You are always conducting a glorious job. supreme clothing

off white x jordan 1 said: I simply needed to say thanks yet again. I'm not certain the things I could possibly have gone through in the absence of those tricks provided by you directly on this subject matter. This has been a real frightful concern for me, but finding out the professional tactic you treated that forced me to weep over contentment. Now i'm happier for the support and even trust you find out what a great job you are always carrying out training other individuals via your website. Most likely you've never come across all of us. off white x jordan 1

yeezy supply said: My husband and i ended up being so more than happy that Chris managed to conclude his homework because of the precious recommendations he gained through the web site. It is now and again perplexing to simply be handing out information and facts that others have been trying to sell. And now we consider we've got the blog owner to be grateful to for that. The type of explanations you made, the simple blog menu, the relationships you can give support to engender - it's all sensational, and it's helping our son and our family do think that theme is entertaining, which is certainly especially pressing. Thanks for all! yeezy supply

longchamp said: I together with my friends were examining the best tips and tricks located on the blog and then all of a sudden developed a terrible feeling I had not expressed respect to the site owner for those strategies. The guys are actually for that reason passionate to see all of them and have undoubtedly been making the most of them. Thank you for truly being indeed considerate and then for considering variety of exceptional information millions of individuals are really wanting to be informed on. Our own honest regret for not saying thanks to earlier. longchamp

canada goose outlet said: I simply needed to thank you very much once again. I do not know the things that I would've carried out in the absence of the type of ways documented by you relating to that concern. It had been a real frustrating issue in my position, nevertheless viewing the skilled technique you treated that made me to jump over happiness. Extremely thankful for your advice and believe you know what a powerful job your are carrying out instructing men and women thru your websites. More than likely you have never got to know any of us. canada goose outlet

supreme hoodie said: Thank you for your own work on this web page. Debby take interest in getting into investigations and it's obvious why. We learn all regarding the dynamic medium you offer sensible tips by means of your web blog and as well as improve contribution from website visitors about this point while our own daughter is without question starting to learn a lot. Take pleasure in the rest of the year. You have been carrying out a dazzling job. supreme hoodie

jordan shoes said: I together with my buddies came analyzing the good information found on your site and then instantly developed a horrible suspicion I had not expressed respect to the blog owner for those strategies. Most of the ladies were for that reason very interested to learn all of them and have now seriously been using those things. Many thanks for really being well thoughtful and then for figuring out variety of beneficial resources millions of individuals are really needing to discover. Our own honest apologies for not expressing gratitude to sooner. jordan shoes

yeezy shoes said: I want to express my admiration for your kindness for persons that should have help on this particular matter. Your very own dedication to getting the message all over turned out to be especially productive and has helped guys just like me to achieve their endeavors. Your warm and helpful guide implies so much a person like me and much more to my office colleagues. Warm regards; from everyone of us. yeezy shoes

jordans said: Needed to send you the tiny note to give thanks once again for these splendid strategies you have shown on this website. It was strangely open-handed with people like you giving easily all numerous people would have offered for sale as an electronic book to help make some cash for themselves, and in particular considering the fact that you might well have tried it if you ever wanted. The basics also worked to be a great way to be certain that some people have similar interest much like my own to realize a good deal more when considering this problem. I'm certain there are millions of more fun times ahead for those who take a look at your blog. jordans

off white said: I have to show appreciation to this writer just for bailing me out of such a condition. Right after exploring through the the net and obtaining basics which were not productive, I assumed my entire life was well over. Existing minus the solutions to the problems you've resolved all through this report is a crucial case, and ones that might have badly damaged my career if I had not noticed the blog. Your main ability and kindness in playing with all the details was useful. I am not sure what I would have done if I hadn't come across such a point like this. I can also at this moment look forward to my future. Thank you very much for your specialized and effective guide. I will not think twice to recommend your web page to any person who will need guidelines on this problem. off white

hermes belt said: I'm commenting to let you be aware of of the excellent discovery my cousin's princess had reading through the blog. She came to understand so many details, which include what it is like to possess a great coaching spirit to make a number of people completely know specified complex things. You undoubtedly did more than our own expectations. Many thanks for imparting the valuable, trusted, informative and also unique guidance on the topic to Gloria. hermes belt
Back