From ae09fe05152b17cf061919c901c8e2e879a7c576 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 15 May 2010 13:28:34 -0400 Subject: [PATCH] item search throws exception if not given query --- app/models/item.rb | 2 ++ spec/models/item_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/app/models/item.rb b/app/models/item.rb index 644c565d..286bdc5d 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -14,6 +14,8 @@ class Item < ActiveRecord::Base end def self.search(query) + query = query.strip if query + raise ArgumentError, "Please provide a search query" if query.blank? query_conditions = [Condition.new] in_phrase = false query.each_char do |c| diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb index c6bd3b10..e8b16e2f 100644 --- a/spec/models/item_spec.rb +++ b/spec/models/item_spec.rb @@ -147,5 +147,14 @@ describe Item do ['One two four', 'Zero one'] ] end + + specify "should return raise exception for a query with no conditions" do + Factory.create :item + [ + lambda { Item.search('').all }, + lambda { Item.search(nil).all }, + lambda { Item.search(' ').all } + ].each { |l| l.should raise_error(ArgumentError) } + end end end