require 'spec_helper' describe SimpleSearch::ActiveRecord, "#simplesearch" do it "work with arel where conditions" do posts_comments = Post.select('posts.*, comments.body as comment').joins(:comments) result = posts_comments.simplesearch('id_eq'=>1, 'subject_eq'=>'subject foo', 'body_eq'=>'body foo') result.first.id.should eq(1) result = posts_comments.simplesearch('id_gt'=>1, 'id_lt'=>3) result.first.id.should eq(2) result = posts_comments.simplesearch('id_ge'=>1, 'id_le'=>3) result.length.should eq(6) result = posts_comments.simplesearch('id_ne'=>1) result.length.should eq(4) result = posts_comments.simplesearch('id_in'=>'1,2') result.first.id.should eq(1) result.last.id.should eq(2) result = posts_comments.simplesearch('id_bt'=>'1..3') result.first.id.should eq(1) result.last.id.should eq(3) result = posts_comments.simplesearch('subject_sw'=>'subject') result.length.should eq(6) result = posts_comments.simplesearch('subject_ew'=>'foo') result.length.should eq(2) result = posts_comments.simplesearch('body_ct'=>'body') result.length.should eq(6) result = posts_comments.simplesearch('body_nc'=>'foo') result.length.should eq(4) result = posts_comments.simplesearch('comments.spam_flag_is'=>'NULL') result.length.should eq(4) result = posts_comments.simplesearch('comments.spam_flag_isnot'=>'NULL') result.length.should eq(2) end it "work with arel group conditions" do posts_comments = Post.select('posts.*, count(comments.id) as num_comment').joins(:comments) result = posts_comments.simplesearch('group_by'=>'posts.id') result.length.should eq(3) end it "work with arel order conditions" do posts_comments = Post.select('posts.*, comments.id as comment_id, comments.body as comment').joins(:comments) result = posts_comments.simplesearch('order_by'=>'comment_id') result.first.comment_id.should eq(11) result = posts_comments.simplesearch('order_by'=>'comment_id desc') result.first.comment_id.should eq(32) end it "work with arel limit and offset conditions" do posts_comments = Post.select('posts.*, comments.id as comment_id, comments.body as comment').joins(:comments) result = posts_comments.simplesearch('order_by'=>'comment_id', 'page_by'=>2) result.map {|r| r.comment_id}.should eq([11,12]) result = posts_comments.simplesearch('order_by'=>'comment_id', 'page'=> 1, 'page_by'=>2) result.map {|r| r.comment_id}.should eq([11,12]) result = posts_comments.simplesearch('order_by'=>'comment_id', 'page'=> 2, 'page_by'=>2) result.map {|r| r.comment_id}.should eq([21,22]) result = posts_comments.simplesearch('order_by'=>'comment_id', 'page'=> 3, 'page_by'=>2) result.map {|r| r.comment_id}.should eq([31,32]) end end