dm-is-persistent_state_machine 0.1.3 → 0.1.4

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.3
1
+ 0.1.4
@@ -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.3"
8
+ s.version = "0.1.4"
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-02-03}
12
+ s.date = %q{2010-02-04}
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 = [
@@ -57,22 +57,6 @@ class StateEvent
57
57
  property :type, Discriminator
58
58
  end
59
59
 
60
- class StateChange
61
- include DataMapper::Resource
62
-
63
- property :id, Serial
64
-
65
- property :from_id, Integer, :required => true, :min => 1
66
- property :to_id, Integer, :required => true, :min => 1
67
- property :user_id, Integer
68
- property :created_at, DateTime
69
-
70
- # associations
71
- belongs_to :user
72
- belongs_to :from, "State"
73
- belongs_to :to, "State"
74
- end
75
-
76
60
  module DataMapper
77
61
  module Is
78
62
  module PersistentStateMachine
@@ -83,7 +67,7 @@ module DataMapper
83
67
  # fired when plugin gets included into Resource
84
68
  #
85
69
  def self.included(base)
86
-
70
+
87
71
  end
88
72
 
89
73
  ##
@@ -102,18 +86,44 @@ module DataMapper
102
86
  # Add instance-methods
103
87
  include DataMapper::Is::PersistentStateMachine::InstanceMethods
104
88
 
89
+ target_model_name = self.name.snake_case
90
+
105
91
  # target object must have a status associated
106
92
  property :state_id, Integer, :required => true, :min => 1
107
93
  belongs_to :state
108
94
 
95
+ has n, Extlib::Inflection.pluralize(target_model_name+"StateChange").to_sym
96
+
109
97
  # generate a FooState class that is derived from State
110
98
  state_model = Object.full_const_set(self.to_s+"State", Class.new(State))
111
99
  # generate a FooStateEvent class that is derived from StateEvent
112
100
  event_model = Object.full_const_set(self.to_s+"StateEvent", Class.new(StateEvent))
101
+
102
+ state_change_model = Class.new do
103
+ include DataMapper::Resource
104
+
105
+ property :id, ::DataMapper::Types::Serial
106
+
107
+ property :from_id, Integer, :required => true, :min => 1
108
+ property :to_id, Integer, :required => true, :min => 1
109
+ property :user_id, Integer
110
+ property Extlib::Inflection.foreign_key(target_model_name).to_sym, Integer, :required => true, :min => 1
111
+ property :created_at, DateTime
112
+
113
+ # associations
114
+ belongs_to :user
115
+ belongs_to :from, "State"
116
+ belongs_to :to, "State"
117
+ belongs_to target_model_name.to_sym
118
+ end
119
+
120
+ state_change_model = Object.full_const_set(self.to_s+"StateChange",state_change_model)
121
+
122
+ self_cached = self
113
123
 
114
124
  after :save do
115
125
  if (@prev_state && @prev_state != state)
116
- @state_change = StateChange.create(:from => @prev_state, :to => state, :created_at => DateTime.now, :user => @user)
126
+ @state_change = state_change_model.create(:from => @prev_state, :to => state, :created_at => DateTime.now, :user => @user, Extlib::Inflection.foreign_key(target_model_name).to_sym => self.id)
117
127
  @prev_state = nil # clean up cache
118
128
  @user = nil
119
129
  end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DataMapper::Is::PersistentStateMachine do
4
-
5
4
  before(:all) do
6
5
  DataMapper.auto_migrate!
7
6
 
@@ -25,7 +24,7 @@ describe DataMapper::Is::PersistentStateMachine do
25
24
  describe "at least" do
26
25
  before :each do
27
26
  Project.auto_migrate!
28
- StateChange.auto_migrate!
27
+ ProjectStateChange.auto_migrate!
29
28
  end
30
29
 
31
30
  it "should log state changes" do
@@ -33,12 +32,13 @@ describe DataMapper::Is::PersistentStateMachine do
33
32
  @project.state.code.should == "open"
34
33
  @project.trigger_event!('review', User.first)
35
34
  Project.first(:name => "Operation X").state.code.should == "open" # not persistent yet
36
-
37
- StateChange.count.should == 0
35
+
36
+ ProjectStateChange.count.should == 0
38
37
  @project.state.code.should == "reviewed"
39
38
  @project.save
40
- StateChange.count.should == 1
41
- StateChange.first.user.name.should == "John Doe"
39
+ ProjectStateChange.count.should == 1
40
+ ProjectStateChange.first.user.name.should == "John Doe"
41
+ ProjectStateChange.first.project.should == @project
42
42
  end
43
43
  end
44
44
  end
data/spec/spec_helper.rb CHANGED
@@ -10,6 +10,8 @@ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
10
10
 
11
11
  require 'dm-is-persistent_state_machine'
12
12
 
13
+ # DataMapper::Logger.new($stdout, :debug)
14
+
13
15
  DataMapper.setup(:default, 'sqlite3::memory:')
14
16
 
15
17
  # classes/vars for tests
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.3
4
+ version: 0.1.4
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-02-03 00:00:00 +01:00
12
+ date: 2010-02-04 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency