progressive 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/progressive/state.rb +12 -1
- data/lib/progressive/subject.rb +13 -6
- data/lib/progressive/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 950a9e588f18cdf8b6c8e3096c2786f0ef1d8f86
|
4
|
+
data.tar.gz: 0d382df9f963fcf7bd29cfee79361a922c132865
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cb78e9cd695073da8674e8fa3c3c51eabd891ea96c4bedcc39e51b858501452dabbd90ea940a3b2b96110e91a456623035b13ee1797a699207930ea9860d7dd
|
7
|
+
data.tar.gz: 8178a0b45cb87205cb94374cdeb8afcda2ce6fa7945e7316b03a2831798d690dc1f96c9c0c2dfe073c3611109d8bf93b7d4a80ce21c7d4815b7289774379c90c
|
data/lib/progressive/state.rb
CHANGED
@@ -51,6 +51,7 @@ module Progressive
|
|
51
51
|
# Returns nothing.
|
52
52
|
def to(event, options = {})
|
53
53
|
return false unless current_state.event?(event)
|
54
|
+
new_record = !!options.delete(:new_record) || subject.new_state_record_on_change?
|
54
55
|
|
55
56
|
current_event = current_state.events[event]
|
56
57
|
|
@@ -62,7 +63,17 @@ module Progressive
|
|
62
63
|
|
63
64
|
subject.run_callbacks(:progress) do
|
64
65
|
subject.run_callbacks(current_event.name) do
|
65
|
-
|
66
|
+
if new_record
|
67
|
+
record = dup
|
68
|
+
record.state = current_event.to
|
69
|
+
if record.save
|
70
|
+
record
|
71
|
+
else
|
72
|
+
false
|
73
|
+
end
|
74
|
+
else
|
75
|
+
update_attribute(:state, current_event.to)
|
76
|
+
end
|
66
77
|
end
|
67
78
|
end
|
68
79
|
ensure
|
data/lib/progressive/subject.rb
CHANGED
@@ -6,12 +6,6 @@ module Progressive
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
include ActiveModel::Callbacks
|
8
8
|
|
9
|
-
included do
|
10
|
-
define_model_callbacks :progress, only: [:before, :after]
|
11
|
-
|
12
|
-
attr_accessor :event_context
|
13
|
-
end
|
14
|
-
|
15
9
|
module ClassMethods
|
16
10
|
# Public: Define the different states and events the subject can go through.
|
17
11
|
#
|
@@ -35,8 +29,21 @@ module Progressive
|
|
35
29
|
end
|
36
30
|
end
|
37
31
|
|
32
|
+
included do
|
33
|
+
define_model_callbacks :progress, only: [:before, :after]
|
34
|
+
|
35
|
+
attr_accessor :event_context
|
36
|
+
|
37
|
+
class_attribute :new_state_record_on_change
|
38
|
+
self.new_state_record_on_change = false
|
39
|
+
end
|
40
|
+
|
38
41
|
def specification
|
39
42
|
self.class.specification
|
40
43
|
end
|
44
|
+
|
45
|
+
def new_state_record_on_change?
|
46
|
+
!!self.class.new_state_record_on_change
|
47
|
+
end
|
41
48
|
end
|
42
49
|
end
|
data/lib/progressive/version.rb
CHANGED