dm-is-persistent_state_machine 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dm-is-persistent_state_machine}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["michael"]
12
- s.date = %q{2010-01-14}
12
+ s.date = %q{2010-02-03}
13
13
  s.description = %q{A persistent State Machine for DataMapper}
14
14
  s.email = %q{ma@zive.at}
15
15
  s.extra_rdoc_files = [
@@ -64,10 +64,11 @@ class StateChange
64
64
 
65
65
  property :from_id, Integer, :required => true, :min => 1
66
66
  property :to_id, Integer, :required => true, :min => 1
67
-
67
+ property :user_id, Integer
68
68
  property :created_at, DateTime
69
69
 
70
70
  # associations
71
+ belongs_to :user
71
72
  belongs_to :from, "State"
72
73
  belongs_to :to, "State"
73
74
  end
@@ -112,8 +113,9 @@ module DataMapper
112
113
 
113
114
  after :save do
114
115
  if (@prev_state && @prev_state != state)
115
- StateChange.create(:from => @prev_state, :to => state, :created_at => DateTime.now)
116
+ @state_change = StateChange.create(:from => @prev_state, :to => state, :created_at => DateTime.now, :user => @user)
116
117
  @prev_state = nil # clean up cache
118
+ @user = nil
117
119
  end
118
120
  end
119
121
 
@@ -129,9 +131,10 @@ module DataMapper
129
131
  end # ClassMethods
130
132
 
131
133
  module InstanceMethods
132
- def trigger_event!(event_code)
133
- # cache the old value
134
+ def trigger_event!(event_code, user)
135
+ # cache prev_state and the user that is triggering the event
134
136
  @prev_state = self.state
137
+ @user = user
135
138
 
136
139
  # delegate to State#trigger!
137
140
  self.state.trigger_event!(self, event_code)
@@ -5,6 +5,9 @@ describe DataMapper::Is::PersistentStateMachine do
5
5
  before(:all) do
6
6
  DataMapper.auto_migrate!
7
7
 
8
+ # create a user
9
+ john_doe = User.create(:name => "John Doe")
10
+
8
11
  # add some states
9
12
  open = ProjectState.create(:name => "Open", :code => "open")
10
13
  reviewed = ProjectState.create(:name => "Reviewed", :code => "reviewed")
@@ -19,8 +22,7 @@ describe DataMapper::Is::PersistentStateMachine do
19
22
  reviewed_confirmed = StateTransition.create(:state => reviewed, :target => confirmed, :state_event => confirm_evt)
20
23
  end
21
24
 
22
- describe "with active DM Identity Map" do
23
-
25
+ describe "at least" do
24
26
  before :each do
25
27
  Project.auto_migrate!
26
28
  StateChange.auto_migrate!
@@ -29,13 +31,14 @@ describe DataMapper::Is::PersistentStateMachine do
29
31
  it "should log state changes" do
30
32
  @project = Project.create(:name => "Operation X", :state => ProjectState.first(:code => "open"))
31
33
  @project.state.code.should == "open"
32
- @project.trigger_event!('review')
34
+ @project.trigger_event!('review', User.first)
33
35
  Project.first(:name => "Operation X").state.code.should == "open" # not persistent yet
34
36
 
35
37
  StateChange.count.should == 0
36
38
  @project.state.code.should == "reviewed"
37
39
  @project.save
38
40
  StateChange.count.should == 1
41
+ StateChange.first.user.name.should == "John Doe"
39
42
  end
40
43
  end
41
44
  end
data/spec/spec_helper.rb CHANGED
@@ -14,6 +14,13 @@ DataMapper.setup(:default, 'sqlite3::memory:')
14
14
 
15
15
  # classes/vars for tests
16
16
 
17
+ class User
18
+ include DataMapper::Resource
19
+
20
+ property :id, Serial
21
+ property :name, String
22
+ end
23
+
17
24
  class Project
18
25
  include DataMapper::Resource
19
26
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-is-persistent_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - michael
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-14 00:00:00 +01:00
12
+ date: 2010-02-03 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency