dpickett-thinking-sphinx 1.1.4

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 (79) hide show
  1. data/LICENCE +20 -0
  2. data/README +107 -0
  3. data/lib/thinking_sphinx/active_record/delta.rb +74 -0
  4. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  5. data/lib/thinking_sphinx/active_record/search.rb +57 -0
  6. data/lib/thinking_sphinx/active_record.rb +245 -0
  7. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +34 -0
  8. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +53 -0
  9. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +129 -0
  10. data/lib/thinking_sphinx/association.rb +144 -0
  11. data/lib/thinking_sphinx/attribute.rb +254 -0
  12. data/lib/thinking_sphinx/class_facet.rb +20 -0
  13. data/lib/thinking_sphinx/collection.rb +142 -0
  14. data/lib/thinking_sphinx/configuration.rb +236 -0
  15. data/lib/thinking_sphinx/core/string.rb +22 -0
  16. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  17. data/lib/thinking_sphinx/deltas/default_delta.rb +65 -0
  18. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  19. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  20. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  21. data/lib/thinking_sphinx/deltas/delayed_delta.rb +25 -0
  22. data/lib/thinking_sphinx/deltas.rb +22 -0
  23. data/lib/thinking_sphinx/facet.rb +58 -0
  24. data/lib/thinking_sphinx/facet_collection.rb +45 -0
  25. data/lib/thinking_sphinx/field.rb +172 -0
  26. data/lib/thinking_sphinx/index/builder.rb +233 -0
  27. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  28. data/lib/thinking_sphinx/index.rb +432 -0
  29. data/lib/thinking_sphinx/rails_additions.rb +133 -0
  30. data/lib/thinking_sphinx/search.rb +654 -0
  31. data/lib/thinking_sphinx/tasks.rb +128 -0
  32. data/lib/thinking_sphinx.rb +145 -0
  33. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
  34. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  35. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  36. data/spec/unit/thinking_sphinx/active_record_spec.rb +256 -0
  37. data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
  38. data/spec/unit/thinking_sphinx/attribute_spec.rb +212 -0
  39. data/spec/unit/thinking_sphinx/collection_spec.rb +14 -0
  40. data/spec/unit/thinking_sphinx/configuration_spec.rb +136 -0
  41. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  42. data/spec/unit/thinking_sphinx/field_spec.rb +145 -0
  43. data/spec/unit/thinking_sphinx/index/builder_spec.rb +5 -0
  44. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
  45. data/spec/unit/thinking_sphinx/index_spec.rb +54 -0
  46. data/spec/unit/thinking_sphinx/search_spec.rb +59 -0
  47. data/spec/unit/thinking_sphinx_spec.rb +129 -0
  48. data/tasks/distribution.rb +48 -0
  49. data/tasks/rails.rake +1 -0
  50. data/tasks/testing.rb +86 -0
  51. data/vendor/after_commit/LICENSE +20 -0
  52. data/vendor/after_commit/README +16 -0
  53. data/vendor/after_commit/Rakefile +22 -0
  54. data/vendor/after_commit/init.rb +5 -0
  55. data/vendor/after_commit/lib/after_commit/active_record.rb +91 -0
  56. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  57. data/vendor/after_commit/lib/after_commit.rb +42 -0
  58. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  59. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  60. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  61. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  62. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  63. data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
  64. data/vendor/riddle/lib/riddle/client/message.rb +65 -0
  65. data/vendor/riddle/lib/riddle/client/response.rb +84 -0
  66. data/vendor/riddle/lib/riddle/client.rb +619 -0
  67. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  68. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  69. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  70. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  71. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  72. data/vendor/riddle/lib/riddle/configuration/section.rb +37 -0
  73. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  74. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  75. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  76. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  77. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  78. data/vendor/riddle/lib/riddle.rb +30 -0
  79. metadata +158 -0
@@ -0,0 +1,129 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx do
4
+ it "should define indexes by default" do
5
+ ThinkingSphinx.define_indexes?.should be_true
6
+ end
7
+
8
+ it "should disable index definition" do
9
+ ThinkingSphinx.define_indexes = false
10
+ ThinkingSphinx.define_indexes?.should be_false
11
+ end
12
+
13
+ it "should enable index definition" do
14
+ ThinkingSphinx.define_indexes = false
15
+ ThinkingSphinx.define_indexes?.should be_false
16
+ ThinkingSphinx.define_indexes = true
17
+ ThinkingSphinx.define_indexes?.should be_true
18
+ end
19
+
20
+ it "should index deltas by default" do
21
+ ThinkingSphinx.deltas_enabled = nil
22
+ ThinkingSphinx.deltas_enabled?.should be_true
23
+ end
24
+
25
+ it "should disable delta indexing" do
26
+ ThinkingSphinx.deltas_enabled = false
27
+ ThinkingSphinx.deltas_enabled?.should be_false
28
+ end
29
+
30
+ it "should enable delta indexing" do
31
+ ThinkingSphinx.deltas_enabled = false
32
+ ThinkingSphinx.deltas_enabled?.should be_false
33
+ ThinkingSphinx.deltas_enabled = true
34
+ ThinkingSphinx.deltas_enabled?.should be_true
35
+ end
36
+
37
+ it "should update indexes by default" do
38
+ ThinkingSphinx.updates_enabled = nil
39
+ ThinkingSphinx.updates_enabled?.should be_true
40
+ end
41
+
42
+ it "should disable index updating" do
43
+ ThinkingSphinx.updates_enabled = false
44
+ ThinkingSphinx.updates_enabled?.should be_false
45
+ end
46
+
47
+ it "should enable index updating" do
48
+ ThinkingSphinx.updates_enabled = false
49
+ ThinkingSphinx.updates_enabled?.should be_false
50
+ ThinkingSphinx.updates_enabled = true
51
+ ThinkingSphinx.updates_enabled?.should be_true
52
+ end
53
+
54
+ describe "use_group_by_shortcut? method" do
55
+ before :each do
56
+ unless ::ActiveRecord::ConnectionAdapters.const_defined?(:MysqlAdapter)
57
+ pending "No MySQL"
58
+ return
59
+ end
60
+
61
+ @connection = ::ActiveRecord::ConnectionAdapters::MysqlAdapter.stub_instance(
62
+ :select_all => true
63
+ )
64
+ ::ActiveRecord::Base.stub_method(
65
+ :connection => @connection
66
+ )
67
+ end
68
+
69
+ it "should return true if no ONLY_FULL_GROUP_BY" do
70
+ @connection.stub_method(
71
+ :select_all => {:a => "OTHER SETTINGS"}
72
+ )
73
+
74
+ ThinkingSphinx.use_group_by_shortcut?.should be_true
75
+ end
76
+
77
+ it "should return true if NULL value" do
78
+ @connection.stub_method(
79
+ :select_all => {:a => nil}
80
+ )
81
+
82
+ ThinkingSphinx.use_group_by_shortcut?.should be_true
83
+ end
84
+
85
+ it "should return false if ONLY_FULL_GROUP_BY is set" do
86
+ @connection.stub_method(
87
+ :select_all => {:a => "OTHER SETTINGS,ONLY_FULL_GROUP_BY,blah"}
88
+ )
89
+
90
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
91
+ end
92
+
93
+ it "should return false if ONLY_FULL_GROUP_BY is set in any of the values" do
94
+ @connection.stub_method(
95
+ :select_all => {
96
+ :a => "OTHER SETTINGS",
97
+ :b => "ONLY_FULL_GROUP_BY"
98
+ }
99
+ )
100
+
101
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
102
+ end
103
+
104
+ describe "if not using MySQL" do
105
+ before :each do
106
+ unless ::ActiveRecord::ConnectionAdapters.const_defined?(:PostgreSQLAdapter)
107
+ pending "No PostgreSQL"
108
+ return
109
+ end
110
+ @connection = ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.stub_instance(
111
+ :select_all => true
112
+ )
113
+ ::ActiveRecord::Base.stub_method(
114
+ :connection => @connection
115
+ )
116
+ end
117
+
118
+ it "should return false" do
119
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
120
+ end
121
+
122
+ it "should not call select_all" do
123
+ ThinkingSphinx.use_group_by_shortcut?
124
+
125
+ @connection.should_not have_received(:select_all)
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,48 @@
1
+ require 'rake/rdoctask'
2
+ require 'rake/gempackagetask'
3
+
4
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
5
+ require 'thinking_sphinx'
6
+
7
+ desc 'Generate documentation'
8
+ Rake::RDocTask.new(:rdoc) do |rdoc|
9
+ rdoc.rdoc_dir = 'rdoc'
10
+ rdoc.title = 'Thinking Sphinx - ActiveRecord Sphinx Plugin'
11
+ rdoc.options << '--line-numbers' << '--inline-source'
12
+ rdoc.rdoc_files.include('README')
13
+ rdoc.rdoc_files.include('lib/**/*.rb')
14
+ end
15
+
16
+ spec = Gem::Specification.new do |s|
17
+ s.name = "thinking-sphinx"
18
+ s.version = ThinkingSphinx::Version::String
19
+ s.summary = "A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching."
20
+ s.description = "A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching."
21
+ s.author = "Pat Allan"
22
+ s.email = "pat@freelancing-gods.com"
23
+ s.homepage = "http://ts.freelancing-gods.com"
24
+ s.has_rdoc = true
25
+ s.rdoc_options << "--title" << "Thinking Sphinx -- Rails/Merb Sphinx Plugin" <<
26
+ "--line-numbers"
27
+ s.rubyforge_project = "thinking-sphinx"
28
+ s.test_files = FileList["spec/**/*_spec.rb"]
29
+ s.files = FileList[
30
+ "lib/**/*.rb",
31
+ "LICENCE",
32
+ "README",
33
+ "tasks/**/*.rb",
34
+ "tasks/**/*.rake",
35
+ "vendor/**/*"
36
+ ]
37
+ end
38
+
39
+ Rake::GemPackageTask.new(spec) do |p|
40
+ p.gem_spec = spec
41
+ p.need_tar = true
42
+ p.need_zip = true
43
+ end
44
+
45
+ desc "Build gemspec file"
46
+ task :build do
47
+ File.open('thinking-sphinx.gemspec', 'w') { |f| f.write spec.to_ruby }
48
+ end
data/tasks/rails.rake ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), '/../lib/thinking_sphinx/tasks')
data/tasks/testing.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'rubygems'
2
+ require 'spec/rake/spectask'
3
+ require 'cucumber/rake/task'
4
+
5
+ desc "Run the specs under spec"
6
+ Spec::Rake::SpecTask.new do |t|
7
+ t.spec_files = FileList['spec/**/*_spec.rb']
8
+ t.spec_opts << "-c"
9
+ end
10
+
11
+ desc "Run all feature-set configurations"
12
+ task :features do |t|
13
+ puts "rake features:mysql"
14
+ system "rake features:mysql"
15
+ puts "rake features:postgresql"
16
+ system "rake features:postgresql"
17
+ end
18
+
19
+ namespace :features do
20
+ def add_task(name, description)
21
+ Cucumber::Rake::Task.new(name, description) do |t|
22
+ t.cucumber_opts = "--format pretty"
23
+ t.step_pattern = [
24
+ "features/support/env",
25
+ "features/support/db/#{name}",
26
+ "features/support/db/active_record",
27
+ "features/support/post_database",
28
+ "features/step_definitions/**.rb"
29
+ ]
30
+ end
31
+ end
32
+
33
+ add_task :mysql, "Run feature-set against MySQL"
34
+ add_task :postgresql, "Run feature-set against PostgreSQL"
35
+ end
36
+
37
+ desc "Generate RCov reports"
38
+ Spec::Rake::SpecTask.new(:rcov) do |t|
39
+ t.libs << 'lib'
40
+ t.spec_files = FileList['spec/**/*_spec.rb']
41
+ t.rcov = true
42
+ t.rcov_opts = ['--exclude', 'spec', '--exclude', 'gems', '--exclude', 'riddle']
43
+ end
44
+
45
+ namespace :rcov do
46
+ def add_task(name, description)
47
+ Cucumber::Rake::Task.new(name, description) do |t|
48
+ t.cucumber_opts = "--format pretty"
49
+ t.step_pattern = [
50
+ "features/support/env",
51
+ "features/support/db/#{name}",
52
+ "features/support/db/active_record",
53
+ "features/support/post_database",
54
+ "features/step_definitions/**.rb"
55
+ ]
56
+ t.rcov = true
57
+ t.rcov_opts = [
58
+ '--exclude', 'spec',
59
+ '--exclude', 'gems',
60
+ '--exclude', 'riddle',
61
+ '--exclude', 'features'
62
+ ]
63
+ end
64
+ end
65
+
66
+ add_task :mysql, "Run feature-set against MySQL with rcov"
67
+ add_task :postgresql, "Run feature-set against PostgreSQL with rcov"
68
+ end
69
+
70
+ desc "Build cucumber.yml file"
71
+ task :cucumber_defaults do
72
+ default_requires = %w(
73
+ --require features/support/env.rb
74
+ --require features/support/db/mysql.rb
75
+ --require features/support/db/active_record.rb
76
+ --require features/support/post_database.rb
77
+ ).join(" ")
78
+
79
+ step_definitions = FileList["features/step_definitions/**.rb"].collect { |path|
80
+ "--require #{path}"
81
+ }.join(" ")
82
+
83
+ File.open('cucumber.yml', 'w') { |f|
84
+ f.write "default: \"#{default_requires} #{step_definitions}\""
85
+ }
86
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Nick Muerdter
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ after_commit
2
+ ===========
3
+
4
+ A Ruby on Rails plugin to add after_commit callbacks. The callbacks that are provided can be used
5
+ to trigger events that run only after the entire transaction is complete. This is beneficial
6
+ in situations where you are doing asynchronous processing and need committed objects.
7
+
8
+ The following callbacks are provided:
9
+
10
+ * (1) after_commit
11
+ * (2) after_commit_on_create
12
+ * (3) after_commit_on_update
13
+ * (4) after_commit_on_destroy
14
+
15
+ The after_commit callback is run for any object that has just been committed. You can obtain finer
16
+ callback control by using the additional <tt>after_commit_on_*</tt> callbacks.
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the after_commit plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the after_commit plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'AfterCommit'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Base.send(:include, AfterCommit::ActiveRecord)
2
+
3
+ Object.subclasses_of(ActiveRecord::ConnectionAdapters::AbstractAdapter).each do |klass|
4
+ klass.send(:include, AfterCommit::ConnectionAdapters)
5
+ end
@@ -0,0 +1,91 @@
1
+ module AfterCommit
2
+ module ActiveRecord
3
+ # Based on the code found in Thinking Sphinx:
4
+ # http://ts.freelancing-gods.com/ which was based on code written by Eli
5
+ # Miller:
6
+ # http://elimiller.blogspot.com/2007/06/proper-cache-expiry-with-aftercommit.html
7
+ # with slight modification from Joost Hietbrink. And now me! Whew.
8
+ def self.included(base)
9
+ base.class_eval do
10
+ # The define_callbacks method was added post Rails 2.0.2 - if it
11
+ # doesn't exist, we define the callback manually
12
+ if respond_to?(:define_callbacks)
13
+ define_callbacks :after_commit,
14
+ :after_commit_on_create,
15
+ :after_commit_on_update,
16
+ :after_commit_on_destroy
17
+ else
18
+ class << self
19
+ # Handle after_commit callbacks - call all the registered callbacks.
20
+ def after_commit(*callbacks, &block)
21
+ callbacks << block if block_given?
22
+ write_inheritable_array(:after_commit, callbacks)
23
+ end
24
+
25
+ def after_commit_on_create(*callbacks, &block)
26
+ callbacks << block if block_given?
27
+ write_inheritable_array(:after_commit_on_create, callbacks)
28
+ end
29
+
30
+ def after_commit_on_update(*callbacks, &block)
31
+ callbacks << block if block_given?
32
+ write_inheritable_array(:after_commit_on_update, callbacks)
33
+ end
34
+
35
+ def after_commit_on_destroy(*callbacks, &block)
36
+ callbacks << block if block_given?
37
+ write_inheritable_array(:after_commit_on_destroy, callbacks)
38
+ end
39
+ end
40
+ end
41
+
42
+ after_save :add_committed_record
43
+ after_create :add_committed_record_on_create
44
+ after_update :add_committed_record_on_update
45
+ after_destroy :add_committed_record_on_destroy
46
+
47
+ # We need to keep track of records that have been saved or destroyed
48
+ # within this transaction.
49
+ def add_committed_record
50
+ AfterCommit.committed_records << self
51
+ end
52
+
53
+ def add_committed_record_on_create
54
+ AfterCommit.committed_records_on_create << self
55
+ end
56
+
57
+ def add_committed_record_on_update
58
+ AfterCommit.committed_records_on_update << self
59
+ end
60
+
61
+ def add_committed_record_on_destroy
62
+ AfterCommit.committed_records << self
63
+ AfterCommit.committed_records_on_destroy << self
64
+ end
65
+
66
+ def after_commit
67
+ # Deliberately blank.
68
+ end
69
+
70
+ # Wraps a call to the private callback method so that the the
71
+ # after_commit callback can be made from the ConnectionAdapters when
72
+ # the commit for the transaction has finally succeeded.
73
+ def after_commit_callback
74
+ callback(:after_commit)
75
+ end
76
+
77
+ def after_commit_on_create_callback
78
+ callback(:after_commit_on_create)
79
+ end
80
+
81
+ def after_commit_on_update_callback
82
+ callback(:after_commit_on_update)
83
+ end
84
+
85
+ def after_commit_on_destroy_callback
86
+ callback(:after_commit_on_destroy)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,103 @@
1
+ module AfterCommit
2
+ module ConnectionAdapters
3
+ def self.included(base)
4
+ base.class_eval do
5
+ # The commit_db_transaction method gets called when the outermost
6
+ # transaction finishes and everything inside commits. We want to
7
+ # override it so that after this happens, any records that were saved
8
+ # or destroyed within this transaction now get their after_commit
9
+ # callback fired.
10
+ def commit_db_transaction_with_callback
11
+ commit_db_transaction_without_callback
12
+ trigger_after_commit_callbacks
13
+ trigger_after_commit_on_create_callbacks
14
+ trigger_after_commit_on_update_callbacks
15
+ trigger_after_commit_on_destroy_callbacks
16
+ end
17
+ alias_method_chain :commit_db_transaction, :callback
18
+
19
+ # In the event the transaction fails and rolls back, nothing inside
20
+ # should recieve the after_commit callback.
21
+ def rollback_db_transaction_with_callback
22
+ rollback_db_transaction_without_callback
23
+
24
+ AfterCommit.committed_records = []
25
+ AfterCommit.committed_records_on_create = []
26
+ AfterCommit.committed_records_on_update = []
27
+ AfterCommit.committed_records_on_destroy = []
28
+ end
29
+ alias_method_chain :rollback_db_transaction, :callback
30
+
31
+ protected
32
+ def trigger_after_commit_callbacks
33
+ # Trigger the after_commit callback for each of the committed
34
+ # records.
35
+ if AfterCommit.committed_records.any?
36
+ AfterCommit.committed_records.each do |record|
37
+ begin
38
+ record.after_commit_callback
39
+ rescue
40
+ end
41
+ end
42
+ end
43
+
44
+ # Make sure we clear out our list of committed records now that we've
45
+ # triggered the callbacks for each one.
46
+ AfterCommit.committed_records = []
47
+ end
48
+
49
+ def trigger_after_commit_on_create_callbacks
50
+ # Trigger the after_commit_on_create callback for each of the committed
51
+ # records.
52
+ if AfterCommit.committed_records_on_create.any?
53
+ AfterCommit.committed_records_on_create.each do |record|
54
+ begin
55
+ record.after_commit_on_create_callback
56
+ rescue
57
+ end
58
+ end
59
+ end
60
+
61
+ # Make sure we clear out our list of committed records now that we've
62
+ # triggered the callbacks for each one.
63
+ AfterCommit.committed_records_on_create = []
64
+ end
65
+
66
+ def trigger_after_commit_on_update_callbacks
67
+ # Trigger the after_commit_on_update callback for each of the committed
68
+ # records.
69
+ if AfterCommit.committed_records_on_update.any?
70
+ AfterCommit.committed_records_on_update.each do |record|
71
+ begin
72
+ record.after_commit_on_update_callback
73
+ rescue
74
+ end
75
+ end
76
+ end
77
+
78
+ # Make sure we clear out our list of committed records now that we've
79
+ # triggered the callbacks for each one.
80
+ AfterCommit.committed_records_on_update = []
81
+ end
82
+
83
+ def trigger_after_commit_on_destroy_callbacks
84
+ # Trigger the after_commit_on_destroy callback for each of the committed
85
+ # records.
86
+ if AfterCommit.committed_records_on_destroy.any?
87
+ AfterCommit.committed_records_on_destroy.each do |record|
88
+ begin
89
+ record.after_commit_on_destroy_callback
90
+ rescue
91
+ end
92
+ end
93
+ end
94
+
95
+ # Make sure we clear out our list of committed records now that we've
96
+ # triggered the callbacks for each one.
97
+ AfterCommit.committed_records_on_destroy = []
98
+ end
99
+ #end protected
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,42 @@
1
+ require 'after_commit/active_record'
2
+ require 'after_commit/connection_adapters'
3
+
4
+ module AfterCommit
5
+ def self.committed_records
6
+ @@committed_records ||= []
7
+ end
8
+
9
+ def self.committed_records=(committed_records)
10
+ @@committed_records = committed_records
11
+ end
12
+
13
+ def self.committed_records_on_create
14
+ @@committed_records_on_create ||= []
15
+ end
16
+
17
+ def self.committed_records_on_create=(committed_records)
18
+ @@committed_records_on_create = committed_records
19
+ end
20
+
21
+ def self.committed_records_on_update
22
+ @@committed_records_on_update ||= []
23
+ end
24
+
25
+ def self.committed_records_on_update=(committed_records)
26
+ @@committed_records_on_update = committed_records
27
+ end
28
+
29
+ def self.committed_records_on_destroy
30
+ @@committed_records_on_destroy ||= []
31
+ end
32
+
33
+ def self.committed_records_on_destroy=(committed_records)
34
+ @@committed_records_on_destroy = committed_records
35
+ end
36
+ end
37
+
38
+ ActiveRecord::Base.send(:include, AfterCommit::ActiveRecord)
39
+
40
+ Object.subclasses_of(ActiveRecord::ConnectionAdapters::AbstractAdapter).each do |klass|
41
+ klass.send(:include, AfterCommit::ConnectionAdapters)
42
+ end
@@ -0,0 +1,53 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'test/unit'
3
+ require 'rubygems'
4
+ require 'activerecord'
5
+ require 'after_commit'
6
+ require 'after_commit/active_record'
7
+ require 'after_commit/connection_adapters'
8
+
9
+ ActiveRecord::Base.establish_connection({"adapter" => "sqlite3", "database" => 'test.sqlite3'})
10
+ begin
11
+ ActiveRecord::Base.connection.execute("drop table mock_records");
12
+ rescue
13
+ end
14
+ ActiveRecord::Base.connection.execute("create table mock_records(id int)");
15
+
16
+ require File.dirname(__FILE__) + '/../init.rb'
17
+
18
+ class MockRecord < ActiveRecord::Base
19
+ attr_accessor :after_commit_on_create_called
20
+ attr_accessor :after_commit_on_update_called
21
+ attr_accessor :after_commit_on_destroy_called
22
+
23
+ after_commit_on_create :do_create
24
+ def do_create
25
+ self.after_commit_on_create_called = true
26
+ end
27
+
28
+ after_commit_on_update :do_update
29
+ def do_update
30
+ self.after_commit_on_update_called = true
31
+ end
32
+
33
+ after_commit_on_create :do_destroy
34
+ def do_destroy
35
+ self.after_commit_on_destroy_called = true
36
+ end
37
+ end
38
+
39
+ class AfterCommitTest < Test::Unit::TestCase
40
+ def test_after_commit_on_create_is_called
41
+ assert_equal true, MockRecord.create!.after_commit_on_create_called
42
+ end
43
+
44
+ def test_after_commit_on_update_is_called
45
+ record = MockRecord.create!
46
+ record.save
47
+ assert_equal true, record.after_commit_on_update_called
48
+ end
49
+
50
+ def test_after_commit_on_destroy_is_called
51
+ assert_equal true, MockRecord.create!.destroy.after_commit_on_destroy_called
52
+ end
53
+ end