Skip to content

Commit

Permalink
Fixing filtering word boundaries and order of normalization of keywor…
Browse files Browse the repository at this point in the history
…ds. Thanks @therabidbanana.
  • Loading branch information
Mark Coates committed Aug 4, 2011
1 parent 9d398d2 commit 302140c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/clortho.rb
Expand Up @@ -8,6 +8,7 @@ module Clortho
class_inheritable_accessor :searchable_with_options
write_inheritable_attribute :searchable_with_options, []
set_callback :create, :before, :inject_default_keywords
set_callback :update, :before, :inject_default_keywords
extend ClassMethods
include InstanceMethods
end
Expand All @@ -17,7 +18,7 @@ module ExclusionConstants
VERBS = %w(have has had do does did be am is are was were having
been can could shall should will would may might must being)
# Article adjectives are normally fluff and offer no additional context or meaning
ADJECTIVES = %w(a an the)
ADJECTIVES = %w(an a the)
end

module ClassMethods
Expand Down Expand Up @@ -48,14 +49,12 @@ def search_#{arg}_keywords_for(*keywords)
end

module InstanceMethods

@defaults_set = false


def inject_default_keywords
searchable_with_options.each do |field|
if !self.send(field[0]).nil?
keywords = !field[1][:exclude].nil? ? filter_on_exclusions(field, self.send(field[0])) : self.send(field[0])
keywords = filter_and_normalize(keywords)
text = self.send(field[0]).to_s
keywords = filter_and_normalize(!field[1][:exclude].nil? ? filter_on_exclusions(field, filter_and_normalize(text)) : text)
self.send("#{field[0].to_s}_keywords=".to_sym, keywords) if keywords
self.send("#{field[0].to_s}_keywords_array=".to_sym, keywords.split.each{ |kw| kw.downcase }) if keywords
end
Expand All @@ -68,13 +67,13 @@ def filter_on_exclusions(field_and_options, keywords)

options[:exclude].each do |exclusion|
if exclusion.is_a? String
keywords.gsub!(exclusion.to_s, '')
keywords.gsub!(/(\b#{exclusion}\b)/, '')
elsif exclusion.is_a? Symbol
klass = self.class
ex = exclusion.to_s.upcase.to_sym
if Clortho::ExclusionConstants::const_get(ex)
Clortho::ExclusionConstants::const_get(ex).each do |e|
keywords.gsub!(e, '')
keywords.gsub!(/(\b#{e}\b)/, '')
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/test_clortho.rb
Expand Up @@ -87,6 +87,15 @@ def setup
assert @colonial.body_keywords !=~ /\-\-/
end

should 're-inject keywords on update' do
@fridge.about = 'Everyone will Wang Chung tonight.'
@colonial.about = 'The ape does bite a Man!'
@fridge.save
@colonial.save
assert @fridge.about_keywords == 'everyone wang chung tonight'
assert @colonial.about_keywords == 'ape bite man'
end

def teardown
Post.destroy_all
end
Expand Down

0 comments on commit 302140c

Please sign in to comment.