Module: Pose::InstanceMethods

Defined in:
lib/pose.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) delete_pose_index

Removes this objects from the search index.


29
30
31
32
# File 'lib/pose.rb', line 29

def delete_pose_index
  return if Rails.env == 'test' and !CONFIGURATION[:search_in_tests]
  self.words.clear
end

- (Object) update_pose_index

Updates the associated words for this object in the database.


20
21
22
23
24
25
26
# File 'lib/pose.rb', line 20

def update_pose_index

  # Don't do this in tests.
  return if Rails.env == 'test' and !CONFIGURATION[:search_in_tests]

  update_pose_words
end

- (Object) update_pose_words

Helper method. Updates the search words with the text returned by search_strings.


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pose.rb', line 36

def update_pose_words

  # Step 1: get an array of all words for the current object.
  new_words = []
  search_strings = self.pose_content
  search_strings.flatten.each do |text|
    next unless text
    text.to_s.split(' ').each do |word|
      new_words.concat Pose.root_word(word)
    end
  end
  new_words.uniq!

  # Step 2: Add new words to the search index.
  Pose.get_words_to_add(self.pose_words, new_words).each do |word_to_add|
    self.pose_words << PoseWord.find_or_create_by_text(word_to_add)
  end

  # Step 3: Remove now obsolete words from search index.
  Pose.get_words_to_remove(self.pose_words, new_words).each do |word_to_remove|
    self.pose_words.delete word_to_remove
  end
end