Sha256: 06858cda29cdd02d331c770a04f4bdbd1b081077cf1e6b5d51534ac9d145c49c

Contents?: true

Size: 1.39 KB

Versions: 10

Compression:

Stored size: 1.39 KB

Contents

# Additions to posified ActiveRecord models.
module Pose
  module ModelAdditions
    extend ActiveSupport::Concern

    included do
      has_many :pose_assignments, as: :posable, dependent: :delete_all
      has_many :pose_words, through: :pose_assignments

      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 = Pose::Helpers.query_terms search_text.to_s

      # Step 2: Add new words to the search index.
      Pose::Helpers.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::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

10 entries across 10 versions & 1 rubygems

Version Path
pose-1.3.4 lib/pose/model_additions.rb
pose-1.3.3 lib/pose/model_additions.rb
pose-1.3.2 lib/pose/model_additions.rb
pose-1.3.1 lib/pose/model_additions.rb
pose-1.3 lib/pose/model_additions.rb
pose-1.2.5 lib/pose/model_additions.rb
pose-1.2.4 lib/pose/model_additions.rb
pose-1.2.3 lib/pose/model_additions.rb
pose-1.2.2 lib/pose/model_additions.rb
pose-1.2.1 lib/pose/model_additions.rb