nxa-sunspot_rails 0.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. data/History.txt +15 -0
  2. data/LICENSE +18 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +260 -0
  5. data/Rakefile +27 -0
  6. data/TODO +8 -0
  7. data/VERSION.yml +4 -0
  8. data/dev_tasks/gemspec.rake +55 -0
  9. data/dev_tasks/rdoc.rake +7 -0
  10. data/dev_tasks/release.rake +4 -0
  11. data/dev_tasks/todo.rake +4 -0
  12. data/generators/sunspot/sunspot_generator.rb +9 -0
  13. data/generators/sunspot/templates/sunspot.yml +18 -0
  14. data/install.rb +1 -0
  15. data/lib/sunspot/rails/adapters.rb +79 -0
  16. data/lib/sunspot/rails/configuration.rb +248 -0
  17. data/lib/sunspot/rails/request_lifecycle.rb +22 -0
  18. data/lib/sunspot/rails/searchable.rb +309 -0
  19. data/lib/sunspot/rails/server.rb +229 -0
  20. data/lib/sunspot/rails/session_proxy.rb +62 -0
  21. data/lib/sunspot/rails/tasks.rb +35 -0
  22. data/lib/sunspot/rails/util.rb +20 -0
  23. data/lib/sunspot/rails.rb +22 -0
  24. data/lib/sunspot/spec/extension.rb +45 -0
  25. data/rails/init.rb +7 -0
  26. data/spec/configuration_spec.rb +102 -0
  27. data/spec/mock_app/app/controllers/application.rb +10 -0
  28. data/spec/mock_app/app/controllers/application_controller.rb +10 -0
  29. data/spec/mock_app/app/controllers/posts_controller.rb +6 -0
  30. data/spec/mock_app/app/models/author.rb +8 -0
  31. data/spec/mock_app/app/models/blog.rb +2 -0
  32. data/spec/mock_app/app/models/post.rb +5 -0
  33. data/spec/mock_app/app/models/post_with_auto.rb +9 -0
  34. data/spec/mock_app/config/boot.rb +110 -0
  35. data/spec/mock_app/config/database.yml +4 -0
  36. data/spec/mock_app/config/environment.rb +42 -0
  37. data/spec/mock_app/config/environments/development.rb +27 -0
  38. data/spec/mock_app/config/environments/test.rb +27 -0
  39. data/spec/mock_app/config/initializers/new_rails_defaults.rb +19 -0
  40. data/spec/mock_app/config/initializers/session_store.rb +15 -0
  41. data/spec/mock_app/config/routes.rb +43 -0
  42. data/spec/mock_app/config/sunspot.yml +19 -0
  43. data/spec/mock_app/db/schema.rb +20 -0
  44. data/spec/model_lifecycle_spec.rb +53 -0
  45. data/spec/model_spec.rb +314 -0
  46. data/spec/request_lifecycle_spec.rb +52 -0
  47. data/spec/schema.rb +20 -0
  48. data/spec/server_spec.rb +124 -0
  49. data/spec/session_spec.rb +24 -0
  50. data/spec/spec_helper.rb +51 -0
  51. data/spec/sunspot_mocking_spec.rb +22 -0
  52. data/spec/util_spec.rb +15 -0
  53. metadata +203 -0
@@ -0,0 +1,4 @@
1
+ test: &test
2
+ :adapter: sqlite3
3
+ :database: db/test.db
4
+ development: *test
@@ -0,0 +1,42 @@
1
+ # Be sure to restart your server when you modify this file
2
+
3
+ # Specifies gem version of Rails to use when vendor/rails is not present
4
+ # RAILS_GEM_VERSION = ENV['RAILS_GEM_VERSION'] || '2.3.3' unless defined? RAILS_GEM_VERSION
5
+
6
+ # Bootstrap the Rails environment, frameworks, and default configuration
7
+ require File.join(File.dirname(__FILE__), 'boot')
8
+
9
+ Rails::Initializer.run do |config|
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Add additional load paths for your own custom dirs
15
+ # config.load_paths += %W( #{RAILS_ROOT}/extras )
16
+
17
+ # Specify gems that this application depends on and have them installed with rake gems:install
18
+ # config.gem "bj"
19
+ # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
20
+ # config.gem "sqlite3-ruby", :lib => "sqlite3"
21
+ # config.gem "aws-s3", :lib => "aws/s3"
22
+ config.gem "qoobaa-sqlite3-ruby", :lib => 'sqlite3', :source => "http://gems.github.com" if RUBY_VERSION > '1.9'
23
+
24
+ # Only load the plugins named here, in the order given (default is alphabetical).
25
+ # :all can be used as a placeholder for all plugins not explicitly named
26
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
27
+
28
+ # Skip frameworks you're not going to use. To use Rails without a database,
29
+ # you must remove the Active Record framework.
30
+ # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
31
+
32
+ # Activate observers that should always be running
33
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
34
+
35
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
36
+ # Run "rake -D time" for a list of tasks for finding time zone names.
37
+ config.time_zone = 'UTC'
38
+
39
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
40
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
41
+ # config.i18n.default_locale = :de
42
+ end
@@ -0,0 +1,27 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # The test environment is used exclusively to run your application's
4
+ # test suite. You never need to work with it otherwise. Remember that
5
+ # your test database is "scratch space" for the test suite and is wiped
6
+ # and recreated between test runs. Don't rely on the data there!
7
+ config.cache_classes = true
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.action_controller.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Disable request forgery protection in test environment
17
+ config.action_controller.allow_forgery_protection = false
18
+
19
+ # Tell Action Mailer not to deliver emails to the real world.
20
+ # The :test delivery method accumulates sent emails in the
21
+ # ActionMailer::Base.deliveries array.
22
+ config.action_mailer.delivery_method = :test
23
+
24
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
25
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
26
+ # like if you have constraints or database-specific column types
27
+ # config.active_record.schema_format = :sql
@@ -0,0 +1,27 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # The test environment is used exclusively to run your application's
4
+ # test suite. You never need to work with it otherwise. Remember that
5
+ # your test database is "scratch space" for the test suite and is wiped
6
+ # and recreated between test runs. Don't rely on the data there!
7
+ config.cache_classes = true
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.action_controller.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Disable request forgery protection in test environment
17
+ config.action_controller.allow_forgery_protection = false
18
+
19
+ # Tell Action Mailer not to deliver emails to the real world.
20
+ # The :test delivery method accumulates sent emails in the
21
+ # ActionMailer::Base.deliveries array.
22
+ config.action_mailer.delivery_method = :test
23
+
24
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
25
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
26
+ # like if you have constraints or database-specific column types
27
+ # config.active_record.schema_format = :sql
@@ -0,0 +1,19 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # These settings change the behavior of Rails 2 apps and will be defaults
4
+ # for Rails 3. You can remove this initializer when Rails 3 is released.
5
+
6
+ if defined?(ActiveRecord)
7
+ # Include Active Record class name as root for JSON serialized output.
8
+ ActiveRecord::Base.include_root_in_json = true
9
+
10
+ # Store the full class name (including module namespace) in STI type column.
11
+ ActiveRecord::Base.store_full_sti_class = true
12
+ end
13
+
14
+ # Use ISO 8601 format for JSON serialized times and dates.
15
+ ActiveSupport.use_standard_json_time_format = true
16
+
17
+ # Don't escape HTML entities in JSON, leave that for the #json_escape helper.
18
+ # if you're including raw json in an HTML page.
19
+ ActiveSupport.escape_html_entities_in_json = false
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying cookie session data integrity.
4
+ # If you change this key, all old sessions will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ ActionController::Base.session = {
8
+ :key => '_mock_app_session',
9
+ :secret => 'a2ddf56c7d642f2075a5d466eff55556c20d13415aff5156740ac82848c3c586711fde0e52f4834727816d08b2d13f3da7b6c7587d789cebe6081ee1a7fec943'
10
+ }
11
+
12
+ # Use the database for sessions instead of the cookie-based default,
13
+ # which shouldn't be used to store highly confidential information
14
+ # (create the session table with "rake db:sessions:create")
15
+ # ActionController::Base.session_store = :active_record_store
@@ -0,0 +1,43 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ # The priority is based upon order of creation: first created -> highest priority.
3
+
4
+ # Sample of regular route:
5
+ # map.connect 'products/:id', :controller => 'catalog', :action => 'view'
6
+ # Keep in mind you can assign values other than :controller and :action
7
+
8
+ # Sample of named route:
9
+ # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
10
+ # This route can be invoked with purchase_url(:id => product.id)
11
+
12
+ # Sample resource route (maps HTTP verbs to controller actions automatically):
13
+ # map.resources :products
14
+
15
+ # Sample resource route with options:
16
+ # map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
17
+
18
+ # Sample resource route with sub-resources:
19
+ # map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
20
+
21
+ # Sample resource route with more complex sub-resources
22
+ # map.resources :products do |products|
23
+ # products.resources :comments
24
+ # products.resources :sales, :collection => { :recent => :get }
25
+ # end
26
+
27
+ # Sample resource route within a namespace:
28
+ # map.namespace :admin do |admin|
29
+ # # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
30
+ # admin.resources :products
31
+ # end
32
+
33
+ # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
34
+ # map.root :controller => "welcome"
35
+
36
+ # See how all your routes lay out with "rake routes"
37
+
38
+ # Install the default routes as the lowest priority.
39
+ # Note: These default routes make all actions in every controller accessible via GET requests. You should
40
+ # consider removing the them or commenting them out if you're using named routes and resources.
41
+
42
+ map.resources :posts, :only => :create
43
+ end
@@ -0,0 +1,19 @@
1
+ test:
2
+ solr:
3
+ hostname: localhost
4
+ port: 8980
5
+ development:
6
+ solr:
7
+ hostname: localhost
8
+ port: 8981
9
+ config_test:
10
+ solr:
11
+ hostname: some.host
12
+ port: 1234
13
+ path: /solr/idx
14
+ log_level: WARNING
15
+ data_path: /my_superior_path/data
16
+ pid_path: /my_superior_path/pids
17
+ solr_home: /my_superior_path
18
+ auto_commit_after_request: false
19
+ auto_commit_after_delete_request: true
@@ -0,0 +1,20 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :posts, :force => true do |t|
3
+ t.string :title
4
+ t.text :body
5
+ t.references :blog
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :blogs, :force => true do |t|
10
+ t.string :name
11
+ t.string :subdomain
12
+ t.timestamps
13
+ end
14
+
15
+ create_table :writers, :force => true, :primary_key => :writer_id do |t|
16
+ t.string :name
17
+ t.timestamps
18
+ end
19
+
20
+ end
@@ -0,0 +1,53 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe 'searchable with lifecycle' do
4
+ integrate_sunspot
5
+
6
+ describe 'on create' do
7
+ before :each do
8
+ @post = PostWithAuto.create
9
+ Sunspot.commit
10
+ end
11
+
12
+ it 'should automatically index' do
13
+ PostWithAuto.search.results.should == [@post]
14
+ end
15
+ end
16
+
17
+ describe 'on update' do
18
+ before :each do
19
+ @post = PostWithAuto.create
20
+ @post.update_attributes(:title => 'Test 1')
21
+ Sunspot.commit
22
+ end
23
+
24
+ it 'should automatically update index' do
25
+ PostWithAuto.search { with :title, 'Test 1' }.results.should == [@post]
26
+ end
27
+ end
28
+
29
+ describe 'on destroy' do
30
+ before :each do
31
+ @post = PostWithAuto.create
32
+ @post.destroy
33
+ Sunspot.commit
34
+ end
35
+
36
+ it 'should automatically remove it from the index' do
37
+ PostWithAuto.search_ids.should be_empty
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'searchable with lifecycle - ignoring specific attributes' do
43
+ integrate_sunspot
44
+
45
+ before(:each) do
46
+ @post = PostWithAuto.create
47
+ end
48
+
49
+ it "should not reindex the object on an update_at change, because it is marked as to-ignore" do
50
+ Sunspot.should_not_receive(:index).with(@post)
51
+ @post.update_attribute :updated_at, 123.seconds.from_now
52
+ end
53
+ end
@@ -0,0 +1,314 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe 'ActiveRecord mixin' do
4
+ integrate_sunspot
5
+
6
+ describe 'index()' do
7
+ before :each do
8
+ @post = Post.create!
9
+ @post.index
10
+ end
11
+
12
+ it 'should not commit the model' do
13
+ Post.search.results.should be_empty
14
+ end
15
+
16
+ it 'should index the model' do
17
+ Sunspot.commit
18
+ Post.search.results.should == [@post]
19
+ end
20
+ end
21
+
22
+ describe 'index!()' do
23
+ before :each do
24
+ @post = Post.create!
25
+ @post.index!
26
+ end
27
+
28
+ it 'should immediately index and commit' do
29
+ Post.search.results.should == [@post]
30
+ end
31
+ end
32
+
33
+ describe 'remove_from_index()' do
34
+ before :each do
35
+ @post = Post.create!
36
+ @post.index!
37
+ @post.remove_from_index
38
+ end
39
+
40
+ it 'should not commit immediately' do
41
+ Post.search.results.should == [@post]
42
+ end
43
+
44
+ it 'should remove the model from the index' do
45
+ Sunspot.commit
46
+ Post.search.results.should be_empty
47
+ end
48
+ end
49
+
50
+ describe 'remove_from_index!()' do
51
+ before :each do
52
+ @post = Post.create!
53
+ @post.index!
54
+ @post.remove_from_index!
55
+ end
56
+
57
+ it 'should immediately remove the model and commit' do
58
+ Post.search.results.should be_empty
59
+ end
60
+ end
61
+
62
+ describe 'remove_all_from_index' do
63
+ before :each do
64
+ @posts = Array.new(10) { Post.create! }.each { |post| Sunspot.index(post) }
65
+ Sunspot.commit
66
+ Post.remove_all_from_index
67
+ end
68
+
69
+ it 'should not commit immediately' do
70
+ Post.search.results.to_set.should == @posts.to_set
71
+ end
72
+
73
+ it 'should remove all instances from the index' do
74
+ Sunspot.commit
75
+ Post.search.results.should be_empty
76
+ end
77
+ end
78
+
79
+ describe 'remove_all_from_index!' do
80
+ before :each do
81
+ Array.new(10) { Post.create! }.each { |post| Sunspot.index(post) }
82
+ Sunspot.commit
83
+ Post.remove_all_from_index!
84
+ end
85
+
86
+ it 'should remove all instances from the index and commit immediately' do
87
+ Post.search.results.should be_empty
88
+ end
89
+ end
90
+
91
+ describe 'search()' do
92
+ before :each do
93
+ @post = Post.create!(:title => 'Test Post')
94
+ @post.index!
95
+ end
96
+
97
+ it 'should return results specified by search' do
98
+ Post.search do
99
+ with :title, 'Test Post'
100
+ end.results.should == [@post]
101
+ end
102
+
103
+ it 'should not return results excluded by search' do
104
+ Post.search do
105
+ with :title, 'Bogus Post'
106
+ end.results.should be_empty
107
+ end
108
+
109
+ it 'should find ActiveRecord objects with an integer, not a string' do
110
+ Post.should_receive(:find).with([@post.id.to_i], anything()).and_return([@post])
111
+ Post.search do
112
+ with :title, 'Test Post'
113
+ end.results.should == [@post]
114
+ end
115
+
116
+ it 'should use the include option on the data accessor when specified' do
117
+ Post.should_receive(:find).with(anything(), hash_including(:include => [:blog])).and_return([@post])
118
+ Post.search do
119
+ with :title, 'Test Post'
120
+ data_accessor_for(Post).include = [:blog]
121
+ end.results.should == [@post]
122
+ end
123
+
124
+ it 'should use the select option on the data accessor when specified' do
125
+ Post.should_receive(:find).with(anything(), hash_including(:select => 'title, published_at')).and_return([@post])
126
+ Post.search do
127
+ with :title, 'Test Post'
128
+ data_accessor_for(Post).select = [:title, :published_at]
129
+ end.results.should == [@post]
130
+ end
131
+
132
+ it 'should not use the select option on the data accessor when not specified' do
133
+ Post.should_receive(:find).with(anything(), hash_not_including(:select)).and_return([@post])
134
+ Post.search do
135
+ with :title, 'Test Post'
136
+ end.results.should == [@post]
137
+ end
138
+ end
139
+
140
+ describe 'search_ids()' do
141
+ before :each do
142
+ @posts = Array.new(2) { Post.create! }.each { |post| post.index }
143
+ Sunspot.commit
144
+ end
145
+
146
+ it 'should return IDs' do
147
+ Post.search_ids.to_set.should == @posts.map { |post| post.id }.to_set
148
+ end
149
+ end
150
+
151
+ describe 'searchable?()' do
152
+ it 'should not be true for models that have not been configured for search' do
153
+ Blog.should_not be_searchable
154
+ end
155
+
156
+ it 'should be true for models that have been configured for search' do
157
+ Post.should be_searchable
158
+ end
159
+ end
160
+
161
+ describe 'index_orphans()' do
162
+ before :each do
163
+ @posts = Array.new(2) { Post.create }.each { |post| post.index }
164
+ Sunspot.commit
165
+ @posts.first.destroy
166
+ end
167
+
168
+ it 'should return IDs of objects that are in the index but not the database' do
169
+ Post.index_orphans.should == [@posts.first.id]
170
+ end
171
+ end
172
+
173
+ describe 'clean_index_orphans()' do
174
+ before :each do
175
+ @posts = Array.new(2) { Post.create }.each { |post| post.index }
176
+ Sunspot.commit
177
+ @posts.first.destroy
178
+ end
179
+
180
+ it 'should remove orphans from the index' do
181
+ Post.clean_index_orphans
182
+ Sunspot.commit
183
+ Post.search.results.should == [@posts.last]
184
+ end
185
+ end
186
+
187
+ describe 'reindex()' do
188
+ before :each do
189
+ @posts = Array.new(2) { Post.create }
190
+ end
191
+
192
+ it 'should index all instances' do
193
+ Post.reindex(:batch_size => nil)
194
+ Sunspot.commit
195
+ Post.search.results.to_set.should == @posts.to_set
196
+ end
197
+
198
+ it 'should remove all currently indexed instances' do
199
+ old_post = Post.create!
200
+ old_post.index!
201
+ old_post.destroy
202
+ Post.reindex
203
+ Sunspot.commit
204
+ Post.search.results.to_set.should == @posts.to_set
205
+ end
206
+
207
+ end
208
+
209
+ describe 'reindex() with real data' do
210
+ before :each do
211
+ @posts = Array.new(2) { Post.create }
212
+ end
213
+
214
+ it 'should index all instances' do
215
+ Post.reindex(:batch_size => nil)
216
+ Sunspot.commit
217
+ Post.search.results.to_set.should == @posts.to_set
218
+ end
219
+
220
+ it 'should remove all currently indexed instances' do
221
+ old_post = Post.create!
222
+ old_post.index!
223
+ old_post.destroy
224
+ Post.reindex
225
+ Sunspot.commit
226
+ Post.search.results.to_set.should == @posts.to_set
227
+ end
228
+
229
+ describe "using batch sizes" do
230
+ it 'should index with a specified batch size' do
231
+ Post.reindex(:batch_size => 1)
232
+ Sunspot.commit
233
+ Post.search.results.to_set.should == @posts.to_set
234
+ end
235
+ end
236
+
237
+ end
238
+
239
+ describe "reindex()" do
240
+
241
+ before(:each) do
242
+ @posts = Array.new(10) { Post.create }
243
+ end
244
+
245
+ describe "when not using batches" do
246
+
247
+ it "should select all if the batch_size is nil" do
248
+ Post.should_receive(:all).with(:include => []).and_return([])
249
+ Post.reindex(:batch_size => nil)
250
+ end
251
+
252
+ it "should search for models with includes" do
253
+ Post.should_receive(:all).with(:include => :author).and_return([])
254
+ Post.reindex(:batch_size => nil, :include => :author)
255
+ end
256
+
257
+ end
258
+
259
+ describe "when using batches" do
260
+
261
+ it "should use the default options" do
262
+ Post.should_receive(:all).with do |params|
263
+ params[:limit].should == 500
264
+ params[:include].should == []
265
+ params[:conditions].should == ['posts.id > ?', 0]
266
+ params[:order].should == 'id'
267
+ end.and_return(@posts)
268
+ Post.reindex
269
+ end
270
+
271
+ it "should set the conditions using the overridden table attributes" do
272
+ @posts = Array.new(10) { Author.create }
273
+ Author.should_receive(:all).with do |params|
274
+ params[:conditions].should == ['writers.writer_id > ?', 0]
275
+ params[:order].should == 'writer_id'
276
+ end.and_return(@posts)
277
+ Author.reindex
278
+ end
279
+
280
+ it "should count the number of records to index" do
281
+ Post.should_receive(:count).and_return(10)
282
+ Post.reindex
283
+ end
284
+
285
+ it "should override the batch_size" do
286
+ Post.should_receive(:all).with do |params|
287
+ params[:limit].should == 20
288
+ @posts
289
+ end.and_return(@posts)
290
+ Post.reindex(:batch_size => 20)
291
+ end
292
+
293
+ it "should set the include option" do
294
+ Post.should_receive(:all).with do |params|
295
+ params[:include].should == [{:author => :address}]
296
+ @posts
297
+ end.and_return(@posts)
298
+ Post.reindex(:include => [{:author => :address}])
299
+ end
300
+
301
+ it "should commit after indexing each batch" do
302
+ Sunspot.should_receive(:commit).twice
303
+ Post.reindex(:batch_size => 5)
304
+ end
305
+
306
+ it "should commit after indexing everything" do
307
+ Sunspot.should_receive(:commit).once
308
+ Post.reindex(:batch_commit => false)
309
+ end
310
+
311
+ end
312
+ end
313
+
314
+ end
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe 'request lifecycle', :type => :controller do
4
+ before(:each) do
5
+ Sunspot::Rails.configuration = @configuration = Sunspot::Rails::Configuration.new
6
+ end
7
+
8
+ after(:each) do
9
+ Sunspot::Rails.configuration = nil
10
+ end
11
+ controller_name :posts
12
+
13
+ it 'should automatically commit after each action if specified' do
14
+ @configuration.user_configuration = { 'auto_commit_after_request' => true }
15
+ Sunspot.should_receive(:commit_if_dirty)
16
+ post :create, :post => { :title => 'Test 1' }
17
+ end
18
+
19
+ it 'should not commit, if configuration is set to false' do
20
+ @configuration.user_configuration = { 'auto_commit_after_request' => false }
21
+ Sunspot.should_not_receive(:commit_if_dirty)
22
+ post :create, :post => { :title => 'Test 1' }
23
+ end
24
+
25
+ it 'should commit if configuration is not specified' do
26
+ @configuration.user_configuration = {}
27
+ Sunspot.should_receive(:commit_if_dirty)
28
+ post :create, :post => { :title => 'Test 1' }
29
+ end
30
+
31
+ ### auto_commit_if_delete_dirty
32
+
33
+ it 'should automatically commit after each delete if specified' do
34
+ @configuration.user_configuration = { 'auto_commit_after_request' => false,
35
+ 'auto_commit_after_delete_request' => true }
36
+ Sunspot.should_receive(:commit_if_delete_dirty)
37
+ post :create, :post => { :title => 'Test 1' }
38
+ end
39
+
40
+ it 'should not automatically commit on delete if configuration is set to false' do
41
+ @configuration.user_configuration = { 'auto_commit_after_request' => false,
42
+ 'auto_commit_after_delete_request' => false }
43
+ Sunspot.should_not_receive(:commit_if_delete_dirty)
44
+ post :create, :post => { :title => 'Test 1' }
45
+ end
46
+
47
+ it 'should not automatically commit on delete if configuration is not specified' do
48
+ @configuration.user_configuration = { 'auto_commit_after_request' => false }
49
+ Sunspot.should_not_receive(:commit_if_delete_dirty)
50
+ post :create, :post => { :title => 'Test 1' }
51
+ end
52
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,20 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :posts, :force => true do |t|
3
+ t.string :title
4
+ t.text :body
5
+ t.references :blog
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :blogs, :force => true do |t|
10
+ t.string :name
11
+ t.string :subdomain
12
+ t.timestamps
13
+ end
14
+
15
+ create_table :writers, :force => true, :primary_key => :writer_id do |t|
16
+ t.string :name
17
+ t.timestamps
18
+ end
19
+
20
+ end