From 086549224d2daecd5d455b05f308ad8534632045 Mon Sep 17 00:00:00 2001 From: Mark Coates Date: Mon, 1 Aug 2011 12:48:57 -0400 Subject: [PATCH] Now filtering on options[:exclude] Array, removing each 'excluded' word. --- lib/clortho.rb | 13 ++++++++++++- test/models/post.rb | 4 +++- test/test_clortho.rb | 7 +++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/clortho.rb b/lib/clortho.rb index 81b04f9..d2c0dac 100644 --- a/lib/clortho.rb +++ b/lib/clortho.rb @@ -39,13 +39,24 @@ module InstanceMethods def inject_default_keywords searchable_with_options.each do |field| if !self.send(field[0]).nil? - keywords = self.send(field[0]) + keywords = !field[1][:exclude].nil? ? filter_on_exclusions(field, self.send(field[0])) : self.send(field[0]) self.send("#{field[0].to_s}_keywords=".to_sym, keywords) if keywords self.send("#{field[0].to_s}_keywords_array=".to_sym, keywords.split) if keywords end end end + def filter_on_exclusions(field_and_options, keywords) + field, options = field_and_options + value = keywords + if options[:exclude] + options[:exclude].each do |exclusion| + value.gsub!(exclusion.to_s, '') + end + end + return value + end + end end diff --git a/test/models/post.rb b/test/models/post.rb index 7cfdfad..5209609 100644 --- a/test/models/post.rb +++ b/test/models/post.rb @@ -6,9 +6,11 @@ class Post key :body, String key :summary, String key :authors, Array + key :about, String searchable :summary # works with one... - searchable :body, :title # or multiple arguments + searchable :body, :title # ...or multiple arguments... + searchable :about, :exclude => [:a, :lipsum] # or options like :exclude. LIPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus porttitor, ipsum a commodo aliquet, velit ligula porttitor eros, sit amet consequat purus massa sit amet quam. Aliquam tempus magna faucibus diff --git a/test/test_clortho.rb b/test/test_clortho.rb index b37bc32..a0741e9 100644 --- a/test/test_clortho.rb +++ b/test/test_clortho.rb @@ -5,9 +5,11 @@ def setup @posts = [ (@fridge = Post.new( title: 'Is your refrigerator running? Better catch it', body: Post::LIPSUM, + about: 'Hello a lipsum world', authors: ['Thomas Mann', 'Jim Byrd'])), (@colonial = Post.new( title: 'The Colonial: In Full Swing', body: Post::LIPSUM, + about: 'Hello a lipsum world', authors: ['Rebecca Simmons'])) ] save_posts @@ -36,6 +38,11 @@ def setup assert_equal Post::LIPSUM, @fridge.body_keywords end + should 'have about_keywords equal to "Hello World"' do + assert_equal "Hello world", @colonial.about_keywords + assert_equal "Hello world", @fridge.about_keywords + end + private def save_posts @posts.each{ |post| post.save }