big_machine 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/big_machine/active_record.rb +32 -32
- data/lib/big_machine/lock.rb +32 -26
- data/lib/big_machine/state.rb +8 -0
- data/lib/big_machine/version.rb +1 -1
- data/test/big_machine_test.rb +2 -2
- metadata +1 -1
@@ -1,32 +1,32 @@
|
|
1
|
-
module BigMachine
|
2
|
-
module ActiveRecord
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
class_attribute :state_attribute
|
7
|
-
after_initialize :set_current_state_from_db
|
8
|
-
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
def big_machine(options = {})
|
12
|
-
super options
|
13
|
-
self.state_attribute = options[:state_attribute] || 'state'
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def set_current_state_from_db
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
def set_current_state(new_state_class)
|
27
|
-
super(new_state_class)
|
28
|
-
send "#{state_attribute}=", new_state_class.name
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
end
|
1
|
+
module BigMachine
|
2
|
+
module ActiveRecord
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :state_attribute
|
7
|
+
after_initialize :set_current_state_from_db
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def big_machine(options = {})
|
12
|
+
super options
|
13
|
+
self.state_attribute = options[:state_attribute] || 'state'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def set_current_state_from_db
|
18
|
+
return unless self.class.initial_state_class
|
19
|
+
|
20
|
+
attribute = send state_attribute
|
21
|
+
|
22
|
+
state_class = attribute ? attribute.constantize : self.class.initial_state_class
|
23
|
+
set_current_state(state_class)
|
24
|
+
end
|
25
|
+
|
26
|
+
def set_current_state(new_state_class)
|
27
|
+
super(new_state_class)
|
28
|
+
send "#{state_attribute}=", new_state_class.name
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/lib/big_machine/lock.rb
CHANGED
@@ -1,26 +1,32 @@
|
|
1
|
-
module BigMachine
|
2
|
-
module Lock
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
end
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
|
1
|
+
module BigMachine
|
2
|
+
module Lock
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def transition_methods
|
10
|
+
public_instance_methods - State.public_instance_methods - [:unlock, :locked?]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def locked?
|
15
|
+
@locked
|
16
|
+
end
|
17
|
+
|
18
|
+
def enter(*args)
|
19
|
+
@locked = true
|
20
|
+
end
|
21
|
+
|
22
|
+
def unlock(*args)
|
23
|
+
@locked = false
|
24
|
+
end
|
25
|
+
|
26
|
+
def transition_to(state_class, *args, &block)
|
27
|
+
return if @locked
|
28
|
+
|
29
|
+
super(state_class, *args, &block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/big_machine/state.rb
CHANGED
@@ -11,6 +11,14 @@ module BigMachine
|
|
11
11
|
public_instance_methods - State.public_instance_methods
|
12
12
|
end
|
13
13
|
|
14
|
+
def self.human_name
|
15
|
+
self.to_s.split('::').last.underscore
|
16
|
+
end
|
17
|
+
|
18
|
+
def human_name
|
19
|
+
self.class.human_name
|
20
|
+
end
|
21
|
+
|
14
22
|
def transition_to(state_class, *args, &block)
|
15
23
|
@stateful.transition_to(state_class, *args, &block)
|
16
24
|
end
|
data/lib/big_machine/version.rb
CHANGED
data/test/big_machine_test.rb
CHANGED
@@ -130,11 +130,11 @@ class BigMachineTest < ActiveSupport::TestCase
|
|
130
130
|
|
131
131
|
test "big machine can lock state" do
|
132
132
|
@dummy.lock
|
133
|
-
assert @dummy.locked?
|
133
|
+
assert @dummy.current_state.locked?
|
134
134
|
@dummy.back_to_draft
|
135
135
|
assert_equal 'LockState', @dummy.current_state.class.name
|
136
136
|
assert @dummy.current_state.locked?
|
137
|
-
@dummy.unlock
|
137
|
+
@dummy.current_state.unlock
|
138
138
|
assert !@dummy.current_state.locked?
|
139
139
|
assert_equal 'LockState', @dummy.current_state.class.name
|
140
140
|
@dummy.back_to_draft
|