Top Level Namespace

Defined Under Namespace

Modules: ThinkingTank Classes: ThinkingTankRailtie

Instance Method Summary collapse

Instance Method Details

#load_modelsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/thinkingtank/tasks.rb', line 5

def load_models
    app_root = ThinkingTank::Configuration.instance.app_root
    dirs = ["#{app_root}/app/models/"] + Dir.glob("#{app_root}/vendor/plugins/*/app/models/")
    
    dirs.each do |base|
        Dir["#{base}**/*.rb"].each do |file|
            model_name = file.gsub(/^#{base}([\w_\/\\]+)\.rb/, '\1')

            next if model_name.nil?
            next if ::ActiveRecord::Base.send(:subclasses).detect { |model|
                model.name == model_name
            }

            begin
                model_name.camelize.constantize
            rescue LoadError
                model_name.gsub!(/.*[\/\\]/, '').nil? ? next : retry
            rescue NameError
                next
            rescue StandardError
                STDERR.puts "Warning: Error loading #{file}"
            end
        end
    end
end

#reindex(klass) ⇒ Object



73
74
75
76
77
78
# File 'lib/thinkingtank/tasks.rb', line 73

def reindex(klass)
    klass.find(:all).each do |obj|
        puts "re-indexing #{obj.class.name}:#{obj.id}"
        obj.update_index
    end
end

#reindex_modelsObject



31
32
33
34
35
36
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
# File 'lib/thinkingtank/tasks.rb', line 31

def reindex_models
    it = ThinkingTank::Configuration.instance.client
    if it.nil?
        puts "!!! Couldn't create a client. Does config/indextank.yml have the correct info?"
        return false
    end

    if it.exists?
        puts "Deleting existing index"
        it.delete
    end
    puts "Creating a new empty index"
    it.add
    puts "Waiting for the index to be ready (this might take a while)"
    while not it.running?
        print "."
        STDOUT.flush
        sleep 0.5
    end
    print "\n"
    STDOUT.flush


    subclasses = nil
    if ActiveRecord::Base.respond_to?(:descendants)
        # Rails 3.0.0 and higher
        subclasses = ActiveRecord::Base.descendants
    elsif Object.respond_to?(:subclasses_of)
        # Rails 2
        subclasses = Object.subclasses_of(ActiveRecord::Base)
    end

    if subclasses.nil?
        STDERR.puts "Couldn't detect models to index."
        return false
    else
        subclasses.each do |klass|
            reindex klass if klass.is_indexable?
        end
    end
end