tphases 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -73,6 +73,9 @@ If a transactional violation occurs in a TPhase, the code will continue to run,
73
73
  #### `ensure_no_transactions_on`
74
74
  Call `ensure_no_transactions_on` on controllers to prevent DB transactions within the views of controller actions. e.g.:
75
75
 
76
+ #### `ignore_phases`
77
+ Call `ignore_phases` to disable TPhases within the block passed
78
+
76
79
  ```ruby
77
80
  class BarsController < ApplicationController
78
81
 
@@ -23,17 +23,19 @@ module TPhases
23
23
  def define_phase_methods!
24
24
  %w{read write no_transactions}.each do |phase_type|
25
25
  define_singleton_method(:"#{phase_type}_phase") do |&block|
26
+
27
+ if @phase_stack.last.try(:ignored?)
28
+ return block.call
29
+ end
30
+
26
31
  phase = Phase.new
27
32
  @phase_stack << phase
28
33
  begin
29
34
  subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |name, date, date2, sha, args|
30
35
  next unless @phase_stack.last == phase
31
- if send(:"#{phase_type}_violation?", args[:sql])
32
- send(:"#{phase_type}_violation_action", args[:sql], caller)
33
- end
34
-
36
+ send(:"#{phase_type}_violation_action", args[:sql], caller) if send(:"#{phase_type}_violation?", args[:sql])
35
37
  end
36
- block.call
38
+ return block.call
37
39
  ensure
38
40
  ActiveSupport::Notifications.unsubscribe(subscriber)
39
41
  @phase_stack.pop
@@ -42,6 +44,13 @@ module TPhases
42
44
  end
43
45
  end
44
46
 
47
+ def ignore_phases
48
+ @phase_stack << Phase.new(ignore: true)
49
+ yield
50
+ ensure
51
+ @phase_stack.pop
52
+ end
53
+
45
54
  private
46
55
  READ_QUERIES = %w{show select describe}
47
56
  WRITE_QUERIES = %w{update commit insert delete}
@@ -79,6 +88,13 @@ module TPhases
79
88
 
80
89
  # simple class to represent a phase on the stack of phases. Used to determine which phase is active
81
90
  class Phase;
91
+ def initialize(opts={ ignore: false })
92
+ @ignore = opts[:ignore]
93
+ end
94
+
95
+ def ignored?
96
+ @ignore
97
+ end
82
98
  end
83
99
 
84
100
  end
@@ -17,6 +17,10 @@ module TPhases
17
17
  yield
18
18
  end
19
19
 
20
+ def ignore_phases
21
+ yield
22
+ end
23
+
20
24
  private
21
25
  def add_rails_methods!
22
26
  require 'tphases/rails/no_transactions_in_controller_pass_through'
@@ -1,3 +1,3 @@
1
1
  module Tphases
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -98,5 +98,29 @@ describe TPhases::Modes::ExceptionsMode do
98
98
  end
99
99
  subject.instance_variable_get("@phase_stack").should be_empty
100
100
  end
101
+
102
+ context "ignore_phases inside of a no_transactions_phase" do
103
+ it "should allow all transactions" do
104
+ subject.no_transactions_phase do
105
+ expect { ActiveRecord::Base.connection.select_all(read_sql) }.to raise_error
106
+ subject.ignore_phases do
107
+ expect { ActiveRecord::Base.connection.select_all(read_sql) }.to_not raise_error
108
+ end
109
+ expect { ActiveRecord::Base.connection.select_all(read_sql) }.to raise_error
110
+ end
111
+ end
112
+ end
113
+
114
+ context "no_transactions_phase inside of a ignore_phases" do
115
+ it "should allow all transactions" do
116
+ subject.ignore_phases do
117
+ expect { ActiveRecord::Base.connection.select_all(read_sql) }.to_not raise_error
118
+ subject.no_transactions_phase do
119
+ expect { ActiveRecord::Base.connection.select_all(read_sql) }.to_not raise_error
120
+ end
121
+ expect { ActiveRecord::Base.connection.select_all(read_sql) }.to_not raise_error
122
+ end
123
+ end
124
+ end
101
125
  end
102
126
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tphases
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
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: 2012-11-01 00:00:00.000000000 Z
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport