Sha256: 905cb84e089c3dae24fda18c2e2df5c4a1c9d8f4a7c585399441e84371268eb3

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

# Additions to posified ActiveRecord classes.
module Pose
  module ModelClassAdditions
    extend ActiveSupport::Concern

    included do
      has_many :pose_assignments, class_name: 'Pose::Assignment', as: :posable, dependent: :delete_all
      has_many :pose_words, through: :pose_assignments, class_name: 'Pose::Word', source: :word

      after_save :update_pose_index
      before_destroy :delete_pose_index

      cattr_accessor :pose_content
    end

    # Updates the associated words for this object in the database.
    def update_pose_index
      update_pose_words if Pose.perform_search?
    end

    # Removes this objects from the search index.
    def delete_pose_index
      self.pose_words.clear if Pose.perform_search?
    end

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

      # Step 1: get an array of all words for the current object.
      search_text = instance_eval &(self.class.pose_content)
      new_words = Helpers.query_terms search_text.to_s

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pose-2.0.1 lib/pose/model_class_additions.rb
pose-2.0.0 lib/pose/model_class_additions.rb