Skip to content

Commit

Permalink
Now filtering on options[:exclude] Array, removing each 'excluded' word.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Coates committed Aug 1, 2011
1 parent 3693e02 commit 0865492
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/clortho.rb
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/models/post.rb
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/test_clortho.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down

0 comments on commit 0865492

Please sign in to comment.