forked from thumblemonks/riot-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supporting transactions through context middleware now
- Loading branch information
Showing
3 changed files
with
43 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require 'riot/rails' | ||
require 'riot/active_record/assertion_macros' | ||
require 'riot/active_record/context_middleware' | ||
require 'riot/active_record/transactional_middleware' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module RiotRails | ||
class TransactionalMiddleware < Riot::ContextMiddleware | ||
register | ||
|
||
def handle?(context) | ||
context.option(:transactional) == true | ||
end | ||
|
||
def call(context) | ||
context.class.class_eval do | ||
alias_method :transactionless_local_run, :local_run | ||
def local_run(*args) | ||
::ActiveRecord::Base.transaction do | ||
transactionless_local_run(*args) | ||
raise ::ActiveRecord::Rollback | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end # RiotRails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
require 'teststrap' | ||
|
||
context "Transactional middleware" do | ||
setup { Riot::Context.new("Room") { set :transactional, true } } | ||
# hookup { topic.transaction { |&block| raise Exception, "Hello" } } | ||
hookup do | ||
ActiveRecord::Base.instance_eval do | ||
# Hijacking transaction and just letting stuff fall through | ||
def transaction(&block) yield; end | ||
end | ||
end | ||
|
||
asserts("context execution is wrapped in a transaction to be rolled back") | ||
|
||
# asserts(:transactional?) | ||
# | ||
# asserts("calling local run") do | ||
# topic.local_run(Riot::SilentReporter.new, Riot::Situation.new) | ||
# end.raises(Exception, "Hello") | ||
# | ||
# context "with a child context" do | ||
# setup do | ||
# topic.context("call me a submarine") {} | ||
# end | ||
# asserts(:transactional?) | ||
# end | ||
context "when :transactional is not set" do | ||
setup { Riot::Context.new("Room") {} } | ||
|
||
asserts("executing context does not raise errors") do | ||
topic.local_run(Riot::SilentReporter.new, Riot::Situation.new) | ||
end | ||
end # when :transactional is not set | ||
|
||
context "when :transactional is set to true" do | ||
setup { Riot::Context.new("Room") { set(:transactional, true) } } | ||
|
||
asserts("executing context") do | ||
topic.local_run(Riot::SilentReporter.new, Riot::Situation.new) | ||
end.raises(ActiveRecord::Rollback) | ||
end # when :transactional is set to true | ||
end # Transactional middleware |