Skip to content

Commit

Permalink
Adding auto-split on keywords field(s) passed into #searchable. Now o…
Browse files Browse the repository at this point in the history
…nto the words to strip out.e
  • Loading branch information
Mark Coates committed Aug 1, 2011
1 parent 328712b commit 3693e02
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
21 changes: 21 additions & 0 deletions lib/clortho.rb
@@ -1,9 +1,15 @@
module MongoMapper
module Plugins
autoload :Clortho, 'clortho'

module Clortho
extend ActiveSupport::Concern

included do
class_inheritable_accessor :searchable_with_options
write_inheritable_attribute :searchable_with_options, []
set_callback :create, :before, :inject_default_keywords
extend ClassMethods
end

module ClassMethods
Expand All @@ -15,6 +21,8 @@ def searchable(*args)
key :"#{arg}_keywords", String, default: ""
key :"#{arg}_keywords_array", Array, default: []

searchable_with_options << [arg.to_sym, options]

class_eval <<-CODE
class << self
end
Expand All @@ -25,6 +33,19 @@ class << self
end

module InstanceMethods

@defaults_set = false

def inject_default_keywords
searchable_with_options.each do |field|
if !self.send(field[0]).nil?
keywords = 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

end

end
Expand Down
7 changes: 1 addition & 6 deletions test/helper.rb
Expand Up @@ -23,9 +23,4 @@
require 'models/post'

class Test::Unit::TestCase
end

@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
lacus ultricies rhoncus. In ut metus purus, at venenatis est. Donec vel elementum turpis. Nullam feugiat
massa quis elit egestas."
end
5 changes: 5 additions & 0 deletions test/models/post.rb
Expand Up @@ -10,4 +10,9 @@ class Post
searchable :summary # works with one...
searchable :body, :title # or multiple arguments

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
lacus ultricies rhoncus. In ut metus purus, at venenatis est. Donec vel elementum turpis. Nullam feugiat
massa quis elit egestas."

end
9 changes: 7 additions & 2 deletions test/test_clortho.rb
Expand Up @@ -4,10 +4,10 @@ class TestClortho < Test::Unit::TestCase
def setup
@posts = [
(@fridge = Post.new( title: 'Is your refrigerator running? Better catch it',
body: @lipsum,
body: Post::LIPSUM,
authors: ['Thomas Mann', 'Jim Byrd'])),
(@colonial = Post.new( title: 'The Colonial: In Full Swing',
body: @lipsum,
body: Post::LIPSUM,
authors: ['Rebecca Simmons']))
]
save_posts
Expand All @@ -31,6 +31,11 @@ def setup
end
end

should 'have body_keywords equal to LIPSUM' do
assert_equal Post::LIPSUM, @colonial.body_keywords
assert_equal Post::LIPSUM, @fridge.body_keywords
end

private
def save_posts
@posts.each{ |post| post.save }
Expand Down

0 comments on commit 3693e02

Please sign in to comment.