Class: Ultrasphinx::Configure

Inherits:
Object
  • Object
show all
Extended by:
Associations
Defined in:
lib/ultrasphinx/configure.rb

Class Method Summary collapse

Methods included from Associations

get_association, get_association_model

Class Method Details

.load_constantsObject

Force all the indexed models to load and register in the MODEL_CONFIGURATION hash.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ultrasphinx/configure.rb', line 9

def load_constants
  
  Dir.chdir "#{RAILS_ROOT}/app/models/" do
    Dir["**/*.rb"].each do |filename|
      open(filename) do |file| 
        begin
          if file.grep(/^\s+is_indexed/).any?
            filename = filename[0..-4]
            begin                
              File.basename(filename).camelize.constantize
            rescue LoadError, NameError => e
              filename.camelize.constantize
            end
          end
        rescue Object => e
          say "warning: critical autoload error on #{filename}; try referencing \"#{filename.camelize}\" directly in the console"
          say e.backtrace.join("\n") if RAILS_ENV == "development"
        end
      end 
    end
  end
  
  # Build the field-to-type mappings.
  Ultrasphinx::Fields.instance.configure(MODEL_CONFIGURATION)
end

.runObject

Main SQL builder.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ultrasphinx/configure.rb', line 37

def run       

  load_constants
        
  say "rebuilding configurations for #{RAILS_ENV} environment" 
  # stable sort classes by name rather than rely on hash order
  model_list = MODEL_CONFIGURATION.keys.sort
  say "available models are #{model_list.to_sentence}"
  File.open(CONF_PATH, "w") do |conf|              
    conf.puts global_header            
    say "generating SQL"    

    INDEXES.each do |index|
      sources = []
      cached_groups = Fields.instance.groups.join("\n")

      model_list.each_with_index do |model, class_id|
        options = MODEL_CONFIGURATION[model]
        klass = model.constantize
        source = "#{model.tableize.gsub('/', '__')}_#{index}"
 
        if index != DELTA_INDEX or options['delta']
          # If we are building the delta, we only want to include the models that requested it
          conf.puts build_source(index, Fields.instance, model, options, class_id, klass, source, cached_groups)
          sources << source                
        end
      end
      
      if sources.any?
        # Don't generate a delta index if there are no delta tables
        conf.puts build_index(index, sources)
      end
      
    end
  end              
end