sunspot_rails 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -188,6 +188,29 @@ remove those documents from the index, use +clean_index_orphans+. Note that
188
188
  neither of these operations should be needed if Sunspot and Sunspot::Rails are
189
189
  used as intended.
190
190
 
191
+ === Configuration
192
+
193
+ ==== ActiveRecord index hooks
194
+
195
+ By default, `sunspot_rails` uses `after_save` and `after_destroy` hooks to
196
+ automatically index or remove ActiveRecord models. When you're using any sort
197
+ of asynchronous indexing like
198
+ [sunspot_index_queue](https://github.com/bdurand/sunspot_index_queue) or
199
+ [sunspot-queue](https://github.com/gaffneyc/sunspot-queue) you may want
200
+ these to be after_commit hooks or you may have timing issues.
201
+
202
+ To do this, add the following to your `sunspot.yml` for each environment:
203
+
204
+ production:
205
+ # ...
206
+ auto_index_callback: after_commit
207
+ auto_remove_callback: after_commit
208
+
209
+ Note that if you set these to `after_commit` in the `test` environment you may
210
+ need https://github.com/grosser/test_after_commit if you use
211
+ `transactionnal_fixtures = true`.
212
+
213
+
191
214
  == Testing Solr integration using RSpec
192
215
 
193
216
  To disable the sunspot-solr integration for your active record models, require
@@ -6,6 +6,8 @@ production:
6
6
  path: /solr/production
7
7
  read_timeout: 20
8
8
  open_timeout: 1
9
+ auto_index_callback: after_commit
10
+ auto_remove_callback: after_commit
9
11
 
10
12
  development:
11
13
  solr:
@@ -13,6 +15,8 @@ development:
13
15
  port: 8983
14
16
  log_level: INFO
15
17
  path: /solr/development
18
+ auto_index_callback: after_commit
19
+ auto_remove_callback: after_commit
16
20
 
17
21
  test:
18
22
  solr:
@@ -20,4 +24,5 @@ test:
20
24
  port: 8983
21
25
  log_level: WARNING
22
26
  path: /solr/test
23
-
27
+ auto_index_callback: after_commit
28
+ auto_remove_callback: after_commit
@@ -1,4 +1,5 @@
1
1
  require 'erb'
2
+ require 'yaml'
2
3
 
3
4
  module Sunspot #:nodoc:
4
5
  module Rails #:nodoc:
@@ -39,6 +40,8 @@ module Sunspot #:nodoc:
39
40
  # hostname: localhost
40
41
  # port: 8982
41
42
  # path: /solr
43
+ # auto_index_callback: after_commit
44
+ # auto_remove_callback: after_commit
42
45
  # auto_commit_after_request: true
43
46
  #
44
47
  # Sunspot::Rails uses the configuration to set up the Solr connection, as
@@ -315,6 +318,24 @@ module Sunspot #:nodoc:
315
318
  @disabled ||= (user_configuration_from_key('disabled') || false)
316
319
  end
317
320
 
321
+ #
322
+ # The callback to use when automatically indexing records.
323
+ # Defaults to after_save.
324
+ #
325
+ def auto_index_callback
326
+ @auto_index_callback ||=
327
+ (user_configuration_from_key('auto_index_callback') || 'after_save')
328
+ end
329
+
330
+ #
331
+ # The callback to use when automatically removing records after deletation.
332
+ # Defaults to after_destroy.
333
+ #
334
+ def auto_remove_callback
335
+ @auto_remove_callback ||=
336
+ (user_configuration_from_key('auto_remove_callback') || 'after_destroy')
337
+ end
338
+
318
339
  private
319
340
 
320
341
  #
@@ -90,16 +90,21 @@ module Sunspot #:nodoc:
90
90
 
91
91
  unless options[:auto_index] == false
92
92
  before_save :mark_for_auto_indexing_or_removal
93
- after_save :perform_index_tasks
93
+
94
+ # after_commit :perform_index_tasks, :if => :persisted?
95
+ __send__ Sunspot::Rails.configuration.auto_index_callback,
96
+ :perform_index_tasks,
97
+ :if => :persisted?
94
98
  end
95
99
 
96
100
  unless options[:auto_remove] == false
97
- after_destroy do |searchable|
98
- searchable.remove_from_index
99
- end
101
+ # after_commit { |searchable| searchable.remove_from_index }, :on => :destroy
102
+ __send__ Sunspot::Rails.configuration.auto_remove_callback,
103
+ proc { |searchable| searchable.remove_from_index },
104
+ :on => :destroy
100
105
  end
101
106
  options[:include] = Util::Array(options[:include])
102
-
107
+
103
108
  self.sunspot_options = options
104
109
  end
105
110
  end
@@ -46,13 +46,13 @@ module Sunspot
46
46
  false
47
47
  end
48
48
 
49
- def commit_if_dirty
49
+ def commit_if_dirty(soft_commit = false)
50
50
  end
51
51
 
52
- def commit_if_delete_dirty
52
+ def commit_if_delete_dirty(soft_commit = false)
53
53
  end
54
54
 
55
- def commit
55
+ def commit(soft_commit = false)
56
56
  end
57
57
 
58
58
  def search(*types)
@@ -71,8 +71,12 @@ module Sunspot
71
71
  Search.new
72
72
  end
73
73
 
74
+ class DataAccessorStub
75
+ attr_accessor :include, :select
76
+ end
77
+
74
78
  class Search
75
-
79
+
76
80
  def build
77
81
  self
78
82
  end
@@ -102,28 +106,32 @@ module Sunspot
102
106
  FacetStub.new
103
107
  end
104
108
 
109
+ def data_accessor_for(klass)
110
+ DataAccessorStub.new
111
+ end
112
+
105
113
  def execute
106
114
  self
107
115
  end
108
116
  end
109
-
110
-
117
+
118
+
111
119
  class PaginatedCollection < Array
112
-
120
+
113
121
  def total_count
114
122
  0
115
123
  end
116
124
  alias :total_entries :total_count
117
-
125
+
118
126
  def current_page
119
127
  1
120
128
  end
121
-
129
+
122
130
  def per_page
123
131
  30
124
132
  end
125
133
  alias :limit_value :per_page
126
-
134
+
127
135
  def total_pages
128
136
  1
129
137
  end
@@ -152,7 +160,7 @@ module Sunspot
152
160
  def offset
153
161
  0
154
162
  end
155
-
163
+
156
164
  end
157
165
 
158
166
  class FacetStub
@@ -162,7 +170,7 @@ module Sunspot
162
170
  end
163
171
 
164
172
  end
165
-
173
+
166
174
  end
167
175
  end
168
176
  end
@@ -31,6 +31,7 @@ namespace :sunspot do
31
31
  # Load all the application's models. Models which invoke 'searchable' will register themselves
32
32
  # in Sunspot.searchable.
33
33
  Rails.application.eager_load!
34
+ Rails::Engine.subclasses.each{|engine| engine.instance.eager_load!}
34
35
 
35
36
  if args[:models].present?
36
37
  # Choose a specific subset of models, if requested
@@ -92,6 +92,14 @@ describe Sunspot::Rails::Configuration, "default values without a sunspot.yml" d
92
92
  it "should handle the 'disabled' property when not set" do
93
93
  @config.disabled?.should be_false
94
94
  end
95
+
96
+ it "should handle the 'auto_index_callback' property when not set" do
97
+ @config.auto_index_callback.should == "after_save"
98
+ end
99
+
100
+ it "should handle the 'auto_remove_callback' property when not set" do
101
+ @config.auto_remove_callback.should == "after_destroy"
102
+ end
95
103
  end
96
104
 
97
105
  describe Sunspot::Rails::Configuration, "user provided sunspot.yml" do
@@ -161,6 +169,21 @@ describe Sunspot::Rails::Configuration, "user provided sunspot.yml" do
161
169
  end
162
170
  end
163
171
 
172
+ describe Sunspot::Rails::Configuration, "with auto_index_callback and auto_remove_callback set" do
173
+ before do
174
+ ::Rails.stub!(:env => 'config_commit_test')
175
+ @config = Sunspot::Rails::Configuration.new
176
+ end
177
+
178
+ it "should handle the 'auto_index_callback' property when set" do
179
+ @config.auto_index_callback.should == "after_commit"
180
+ end
181
+
182
+ it "should handle the 'auto_remove_callback' property when set" do
183
+ @config.auto_remove_callback.should == "after_commit"
184
+ end
185
+ end
186
+
164
187
  describe Sunspot::Rails::Configuration, "with disabled: true in sunspot.yml" do
165
188
  before(:each) do
166
189
  ::Rails.stub(:env => 'config_disabled_test')
@@ -2,6 +2,8 @@ test:
2
2
  solr:
3
3
  hostname: localhost
4
4
  port: 8983
5
+ auto_index_callback: after_save
6
+ auto_remove_callback: after_destroy
5
7
  development:
6
8
  solr:
7
9
  hostname: localhost
@@ -25,3 +27,6 @@ config_test:
25
27
  auto_commit_after_delete_request: true
26
28
  config_disabled_test:
27
29
  disabled: true
30
+ config_commit_test:
31
+ auto_index_callback: after_commit
32
+ auto_remove_callback: after_commit
@@ -140,5 +140,19 @@ describe 'specs with Sunspot stubbed' do
140
140
  it 'should return empty array if listing facets' do
141
141
  @search.facets.should == []
142
142
  end
143
+
144
+ describe '#data_accessor_for' do
145
+ before do
146
+ @accessor = @search.data_accessor_for(Post)
147
+ end
148
+
149
+ it 'should provide accessor for select' do
150
+ @accessor.should respond_to(:select, :select=)
151
+ end
152
+
153
+ it 'should provide accessor for include' do
154
+ @accessor.should respond_to(:include, :include=)
155
+ end
156
+ end
143
157
  end
144
158
  end
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunspot_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Mat Brown
@@ -26,11 +27,12 @@ authors:
26
27
  autorequire:
27
28
  bindir: bin
28
29
  cert_chain: []
29
- date: 2014-05-07 00:00:00.000000000 Z
30
+ date: 2015-04-09 00:00:00.000000000 Z
30
31
  dependencies:
31
32
  - !ruby/object:Gem::Dependency
32
33
  name: rails
33
34
  requirement: !ruby/object:Gem::Requirement
35
+ none: false
34
36
  requirements:
35
37
  - - ">="
36
38
  - !ruby/object:Gem::Version
@@ -38,6 +40,7 @@ dependencies:
38
40
  type: :runtime
39
41
  prerelease: false
40
42
  version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
41
44
  requirements:
42
45
  - - ">="
43
46
  - !ruby/object:Gem::Version
@@ -45,20 +48,23 @@ dependencies:
45
48
  - !ruby/object:Gem::Dependency
46
49
  name: sunspot
47
50
  requirement: !ruby/object:Gem::Requirement
51
+ none: false
48
52
  requirements:
49
53
  - - '='
50
54
  - !ruby/object:Gem::Version
51
- version: 2.1.1
55
+ version: 2.2.0
52
56
  type: :runtime
53
57
  prerelease: false
54
58
  version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
55
60
  requirements:
56
61
  - - '='
57
62
  - !ruby/object:Gem::Version
58
- version: 2.1.1
63
+ version: 2.2.0
59
64
  - !ruby/object:Gem::Dependency
60
65
  name: nokogiri
61
66
  requirement: !ruby/object:Gem::Requirement
67
+ none: false
62
68
  requirements:
63
69
  - - ">="
64
70
  - !ruby/object:Gem::Version
@@ -66,6 +72,7 @@ dependencies:
66
72
  type: :runtime
67
73
  prerelease: false
68
74
  version_requirements: !ruby/object:Gem::Requirement
75
+ none: false
69
76
  requirements:
70
77
  - - ">="
71
78
  - !ruby/object:Gem::Version
@@ -73,6 +80,7 @@ dependencies:
73
80
  - !ruby/object:Gem::Dependency
74
81
  name: rspec
75
82
  requirement: !ruby/object:Gem::Requirement
83
+ none: false
76
84
  requirements:
77
85
  - - "~>"
78
86
  - !ruby/object:Gem::Version
@@ -80,6 +88,7 @@ dependencies:
80
88
  type: :development
81
89
  prerelease: false
82
90
  version_requirements: !ruby/object:Gem::Requirement
91
+ none: false
83
92
  requirements:
84
93
  - - "~>"
85
94
  - !ruby/object:Gem::Version
@@ -87,6 +96,7 @@ dependencies:
87
96
  - !ruby/object:Gem::Dependency
88
97
  name: rspec-rails
89
98
  requirement: !ruby/object:Gem::Requirement
99
+ none: false
90
100
  requirements:
91
101
  - - "~>"
92
102
  - !ruby/object:Gem::Version
@@ -94,6 +104,7 @@ dependencies:
94
104
  type: :development
95
105
  prerelease: false
96
106
  version_requirements: !ruby/object:Gem::Requirement
107
+ none: false
97
108
  requirements:
98
109
  - - "~>"
99
110
  - !ruby/object:Gem::Version
@@ -182,7 +193,6 @@ files:
182
193
  homepage: http://github.com/outoftime/sunspot/tree/master/sunspot_rails
183
194
  licenses:
184
195
  - MIT
185
- metadata: {}
186
196
  post_install_message:
187
197
  rdoc_options:
188
198
  - "--webcvs=http://github.com/outoftime/sunspot/tree/master/%s"
@@ -193,20 +203,22 @@ rdoc_options:
193
203
  require_paths:
194
204
  - lib
195
205
  required_ruby_version: !ruby/object:Gem::Requirement
206
+ none: false
196
207
  requirements:
197
208
  - - ">="
198
209
  - !ruby/object:Gem::Version
199
210
  version: '0'
200
211
  required_rubygems_version: !ruby/object:Gem::Requirement
212
+ none: false
201
213
  requirements:
202
214
  - - ">="
203
215
  - !ruby/object:Gem::Version
204
216
  version: '0'
205
217
  requirements: []
206
218
  rubyforge_project: sunspot
207
- rubygems_version: 2.2.2
219
+ rubygems_version: 1.8.25
208
220
  signing_key:
209
- specification_version: 4
221
+ specification_version: 3
210
222
  summary: Rails integration for the Sunspot Solr search library
211
223
  test_files:
212
224
  - spec/configuration_spec.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: e4265b8bc7f396dcf434243ff31d073722ce37e4
4
- data.tar.gz: 6a31e3de9ff96687f791fbca0ff71b29644a3b7e
5
- SHA512:
6
- metadata.gz: 476e1c959c213af1972a51abbd779a535d6b698544dc3a93348c4825433819b441cc082736ccbc73f44043ddfe5493f90cb0e963b0669e35b03d050322930516
7
- data.tar.gz: e0afd66ab79b43f53df67f4434dface8feaed30185b2f3b21e4d150c6ffdb60ef63ba44f52c0ba6e3f35cdfb7aec927775c23ef19bd3c42494eb87e727ed954c