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
34
35
# File 'lib/ultrasphinx/configure.rb', line 9

def load_constants
  
  Dependencies.load_paths.grep(/\/models$/).each do |models_dir|
    Dir.chdir(models_dir) 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 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
  end
  
  # Build the field-to-type mappings.
  Fields.instance.configure(MODEL_CONFIGURATION)
end

.runObject

Main SQL builder.



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
73
# File 'lib/ultrasphinx/configure.rb', line 39

def run       

  load_constants
        
  say "rebuilding configurations for #{RAILS_ENV} environment" 
  say "available models are #{MODEL_CONFIGURATION.keys.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_CONFIGURATION.each_with_index do |model_and_options, class_id|              
        # This relies on hash sort order being deterministic per-machine
        model, options = model_and_options
        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