state_machine-audit_trail 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ require 'state_machine'
2
2
 
3
3
  module StateMachine::AuditTrail
4
4
 
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.3"
6
6
 
7
7
  def self.setup
8
8
  StateMachine::Machine.send(:include, StateMachine::AuditTrail::TransitionAuditing)
@@ -5,8 +5,8 @@ class StateMachine::AuditTrailGenerator < ::Rails::Generators::Base
5
5
  source_root File.join(File.dirname(__FILE__), 'templates')
6
6
 
7
7
  argument :source_model
8
- argument :state_attribute, :default => 'state'
9
- argument :transition_model, :default => nil
8
+ argument :state_attribute, :default => 'state'
9
+ argument :transition_model, :default => ''
10
10
 
11
11
 
12
12
  def create_model
@@ -16,6 +16,6 @@ class StateMachine::AuditTrailGenerator < ::Rails::Generators::Base
16
16
  protected
17
17
 
18
18
  def transition_class_name
19
- transition_model || "#{source_model.camelize}#{state_attribute.camelize}Transition"
19
+ transition_model.blank? ? "#{source_model.camelize}#{state_attribute.camelize}Transition" : transition_model
20
20
  end
21
21
  end
@@ -1,75 +1,82 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe StateMachine::AuditTrail do
4
+
4
5
  it "should have a VERSION constant" do
5
6
  StateMachine::AuditTrail.const_defined?('VERSION').should be_true
6
7
  end
7
8
 
8
- it "should log an event with all fields set correctly" do
9
- m = TestModel.create!
10
- m.start!
11
- last_transition = TestModelStateTransition.where(:test_model_id => m.id).last
9
+ context 'on an object with a single state machine' do
10
+ let!(:state_machine) { TestModel.create! }
11
+
12
+ it "should log an event with all fields set correctly" do
13
+ state_machine.start!
14
+ last_transition = TestModelStateTransition.where(:test_model_id => state_machine.id).last
12
15
 
13
- last_transition.event.should == 'start'
14
- last_transition.from.should == 'waiting'
15
- last_transition.to.should == 'started'
16
- last_transition.created_at.should be_within(10.seconds).of(Time.now.utc)
16
+ last_transition.event.should == 'start'
17
+ last_transition.from.should == 'waiting'
18
+ last_transition.to.should == 'started'
19
+ last_transition.created_at.should be_within(10.seconds).of(Time.now.utc)
20
+ end
21
+
22
+ it "should log multiple events" do
23
+ lambda { state_machine.start && state_machine.stop && state_machine.start }.should change(TestModelStateTransition, :count).by(3)
24
+ end
25
+
26
+ it "should do nothing when the transition is not exectuted successfully" do
27
+ lambda { state_machine.stop }.should_not change(TestModelStateTransition, :count)
28
+ end
17
29
  end
18
30
 
19
- it "should log multiple events" do
20
- m = TestModel.create!
31
+ context 'on an object with multiple state machines' do
32
+ let!(:state_machine) { TestModelWithMultipleStateMachines.create! }
33
+
34
+ it "should log a state transition for the affected state machine" do
35
+ lambda { state_machine.begin_first! }.should change(TestModelWithMultipleStateMachinesFirstTransition, :count).by(1)
36
+ end
21
37
 
22
- lambda do
23
- m.start
24
- m.stop
25
- m.start
26
- end.should change(TestModelStateTransition, :count).by(3)
38
+ it "should not log a state transition for the unaffected state machine" do
39
+ lambda { state_machine.begin_first! }.should_not change(TestModelWithMultipleStateMachinesSecondTransition, :count)
40
+ end
27
41
  end
28
42
 
29
- it "should do nothing when the transition is not exectuted successfully" do
30
- m = TestModel.create!
31
- lambda { m.stop }.should_not change(TestModelStateTransition, :count)
43
+ context 'on an object with a state machine having an initial state' do
44
+ let(:state_machine_class) { TestModelWithMultipleStateMachines }
45
+ let(:state_transition_class) { TestModelWithMultipleStateMachinesFirstTransition }
46
+
47
+ it "should log a state transition for the inital state" do
48
+ lambda { state_machine_class.create! }.should change(state_transition_class, :count).by(1)
49
+ end
50
+
51
+ it "should only set the :to state for the initial transition" do
52
+ state_machine_class.create!
53
+ initial_transition = state_transition_class.last
54
+ initial_transition.event.should be_nil
55
+ initial_transition.from.should be_nil
56
+ initial_transition.to.should == 'beginning'
57
+ initial_transition.created_at.should be_within(10.seconds).of(Time.now.utc)
58
+ end
32
59
  end
33
60
 
34
- it "should log a state_machine specific event for the affected state machine" do
35
- m = TestModelWithMultipleStateMachines.create!
36
- lambda { m.begin_first! }.should change(TestModelWithMultipleStateMachinesFirstTransition, :count).by(1)
37
- end
61
+ context 'on an object with a state machine not having an initial state' do
62
+ let(:state_machine_class) { TestModelWithMultipleStateMachines }
63
+ let(:state_transition_class) { TestModelWithMultipleStateMachinesSecondTransition }
64
+
65
+ it "should not log a transition when the object is created" do
66
+ lambda { state_machine_class.create! }.should_not change(state_transition_class, :count)
67
+ end
38
68
 
39
- it "should not log a state_machine specific event for the unaffected state machine" do
40
- m = TestModelWithMultipleStateMachines.create!
41
- lambda { m.begin_first! }.should_not change(TestModelWithMultipleStateMachinesSecondTransition, :count)
42
- end
43
-
44
- it "should log a transition for the inital state" do
45
- lambda { TestModelWithMultipleStateMachines.create! }.should change(TestModelWithMultipleStateMachinesFirstTransition, :count).by(1)
46
- end
47
-
48
- it "should only log the :to state for an initial state" do
49
- TestModelWithMultipleStateMachines.create!
50
- initial_transition = TestModelWithMultipleStateMachinesFirstTransition.last
51
- initial_transition.event.should be_nil
52
- initial_transition.from.should be_nil
53
- initial_transition.to.should == 'beginning'
54
- initial_transition.created_at.should be_within(10.seconds).of(Time.now.utc)
55
- end
56
-
57
- it "should not log a transition when the state machine does not have an initial state" do
58
- lambda { TestModelWithMultipleStateMachines.create! }.should_not change(TestModelWithMultipleStateMachinesSecondTransition, :count)
59
- end
60
-
61
- it "should create a transiction for the first record when the state machine does not have an initial state" do
62
- m = TestModelWithMultipleStateMachines.create!
63
- lambda { m.begin_second! }.should change(TestModelWithMultipleStateMachinesSecondTransition, :count).by(1)
64
- end
65
-
66
- it "should not have a value for the from state when the state machine does not have an initial state" do
67
- m = TestModelWithMultipleStateMachines.create!
68
- m.begin_second!
69
- first_transition = TestModelWithMultipleStateMachinesSecondTransition.last
70
- first_transition.event.should == 'begin_second'
71
- first_transition.from.should be_nil
72
- first_transition.to.should == 'beginning'
73
- first_transition.created_at.should be_within(10.seconds).of(Time.now.utc)
69
+ it "should log a transition for the first event" do
70
+ lambda { state_machine_class.create.begin_second! }.should change(state_transition_class, :count).by(1)
71
+ end
72
+
73
+ it "should not set a value for the :from state on the first transition" do
74
+ state_machine_class.create.begin_second!
75
+ first_transition = state_transition_class.last
76
+ first_transition.event.should == 'begin_second'
77
+ first_transition.from.should be_nil
78
+ first_transition.to.should == 'beginning'
79
+ first_transition.created_at.should be_within(10.seconds).of(Time.now.utc)
80
+ end
74
81
  end
75
82
  end
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = "state_machine-audit_trail"
4
- s.version = "0.0.2"
4
+ s.version = "0.0.3"
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ["Willem van Bergen", "Jesse Storimer"]
7
7
  s.email = ["willem@shopify.com", "jesse@shopify.com"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Willem van Bergen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-15 00:00:00 -04:00
18
+ date: 2011-04-04 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency