heliosphere 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/heliosphere.rb CHANGED
@@ -13,7 +13,7 @@ module Heliosphere
13
13
  end
14
14
 
15
15
  def self.reindex
16
- server.start_and_reindex
16
+ server.reindex
17
17
  end
18
18
 
19
19
  def self.wait(&block)
@@ -12,6 +12,7 @@ module Heliosphere
12
12
 
13
13
  def define(&block)
14
14
  instance_eval(&block)
15
+ extend_active_record
15
16
  apply
16
17
  end
17
18
 
@@ -28,6 +29,27 @@ module Heliosphere
28
29
  end
29
30
  end
30
31
 
32
+ def extend_active_record
33
+ ActiveRecord::Base.class_eval %Q{
34
+ def index_on_save?
35
+ @index_on_save = true unless instance_variable_defined?(:@index_on_save)
36
+ @index_on_save
37
+ end
38
+
39
+ def save_without_index
40
+ @index_on_save = false
41
+ save
42
+ @index_on_save = true
43
+ end
44
+
45
+ def save_without_index!
46
+ @index_on_save = false
47
+ save!
48
+ @index_on_save = true
49
+ end
50
+ }
51
+ end
52
+
31
53
  def index(model, &block)
32
54
  model_definitions[model] = block
33
55
  end
@@ -4,10 +4,10 @@ module Heliosphere
4
4
 
5
5
  class Observer < ActiveRecord::Observer
6
6
 
7
- observe *Heliosphere.indexer.model_names
7
+ observe *Heliosphere.indexer.models
8
8
 
9
9
  def after_save(object)
10
- running? { Sunspot.index(object) }
10
+ running? { Sunspot.index(object) if object.index_on_save? }
11
11
  end
12
12
 
13
13
  def after_destroy(object)
@@ -1,16 +1,20 @@
1
- require 'heliosphere/observer'
2
-
3
1
  module Heliosphere
4
2
 
5
3
  class Railtie < Rails::Railtie
6
4
 
7
- initializer "heliosphere.add_observer" do |app|
5
+ config.after_initialize do |app|
6
+ require 'heliosphere/observer'
7
+
8
8
  ar = app.config.active_record
9
+
9
10
  if ar.observers
10
11
  ar.observers += [ Heliosphere::Observer ]
11
12
  else
12
13
  ar.observers = [ Heliosphere::Observer ]
13
14
  end
15
+
16
+ ActiveRecord::Base.observers = ar.observers
17
+ ActiveRecord::Base.instantiate_observers
14
18
  end
15
19
 
16
20
  rake_tasks do
@@ -29,9 +29,21 @@ module Heliosphere
29
29
  end
30
30
 
31
31
  def reindex
32
+ wait(&:up?)
33
+ clear_index
34
+
35
+ Heliosphere.indexer.models.each do |model|
36
+ model.all.each do |record|
37
+ Sunspot.index(record)
38
+ end
39
+ end
40
+
41
+ Sunspot.commit
42
+ end
43
+
44
+ def clear_index
32
45
  Heliosphere.indexer.models.each do |model|
33
46
  model.remove_all_from_index
34
- model.index
35
47
  end
36
48
  end
37
49
 
@@ -1,3 +1,3 @@
1
1
  module Heliosphere
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heliosphere
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-26 00:00:00.000000000Z
12
+ date: 2012-02-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Saving a record is usually more important than indexing it in Solr. Heliosphere
15
15
  will only index when Solr is running. It also includes some convenient rake tasks
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  version: '0'
59
59
  requirements: []
60
60
  rubyforge_project: heliosphere
61
- rubygems_version: 1.8.10
61
+ rubygems_version: 1.8.15
62
62
  signing_key:
63
63
  specification_version: 3
64
64
  summary: Heliosphere makes Sunspot less dangerous by allowing updates to occur when