Module: Ximate::Search

Defined in:
lib/ximate/search.rb

Instance Method Summary collapse

Instance Method Details

#define_index(locale = I18n.default_locale, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ximate/search.rb', line 16

def define_index(locale = I18n.default_locale, &block)
  if ActiveRecord::Base.connection.tables.include?(self.table_name)
    table = self.table_name.to_sym
    DATA[locale.to_sym] ||= {}
    DATA[locale.to_sym][table] ||= {}

    extend  ClassMethods
    include InstanceMethods

    after_save { |proc| proc.update_index(I18n.locale, &block) }

    calculate_indices = Proc.new do
      now = Time.now
      self.to_s.classify.constantize.find_in_batches(batch_size: 100) do |group|
        group.each do |p|
          p.update_index(locale, &block)
        end
      end
      puts "\b\b=> Build XIMATE hash data for '#{table}' in #{Time.now - now}s." if OPTIONS[:logger]
    end

    OPTIONS[:async] ? Thread.new{calculate_indices.call} : calculate_indices.call
  end
end