sidestate 0.1.0 → 0.2.0
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/sidestate/hook.rb +19 -3
- data/spec/sidestate/hook_spec.rb +21 -2
- data/spec/support/models.rb +4 -4
- metadata +3 -3
data/lib/sidestate/hook.rb
CHANGED
@@ -8,20 +8,36 @@ module Sidestate::Hook
|
|
8
8
|
|
9
9
|
define_method(aasm_state_object) do
|
10
10
|
aasm_state_collection.last
|
11
|
-
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
base.aasm_state_class.instance_eval do
|
15
|
+
belongs_to base.name.underscore.to_sym
|
12
16
|
end
|
13
17
|
end
|
14
18
|
|
15
19
|
module ClassMethods
|
20
|
+
def aasm_state_class
|
21
|
+
"#{name}State".constantize
|
22
|
+
end
|
23
|
+
|
16
24
|
def aasm_state_collection
|
17
|
-
|
25
|
+
aasm_state_class.name.pluralize.underscore.to_sym
|
18
26
|
end
|
19
27
|
|
20
28
|
def aasm_state_object
|
21
|
-
|
29
|
+
aasm_state_class.name.underscore.to_sym
|
22
30
|
end
|
23
31
|
end
|
24
32
|
|
33
|
+
def next_states
|
34
|
+
self.class.aasm_events.collect { |name, event|
|
35
|
+
event.transitions_from_state(aasm_current_state)
|
36
|
+
}.flatten.collect { |transition|
|
37
|
+
Array(transition.to)
|
38
|
+
}.flatten
|
39
|
+
end
|
40
|
+
|
25
41
|
private
|
26
42
|
|
27
43
|
def save_aasm_state_object
|
data/spec/sidestate/hook_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec/spec_helper'
|
2
2
|
|
3
3
|
describe Sidestate::Hook do
|
4
|
-
|
4
|
+
describe '#article_state' do
|
5
5
|
it "should create a new state when a model object is created" do
|
6
6
|
article = Article.create
|
7
7
|
article.article_state.should_not be_nil
|
@@ -21,7 +21,7 @@ describe Sidestate::Hook do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
describe '#article_states' do
|
25
25
|
it "should store all states for a given object" do
|
26
26
|
Timecop.travel 2.days.ago
|
27
27
|
article = Article.create
|
@@ -36,4 +36,23 @@ describe Sidestate::Hook do
|
|
36
36
|
}.should == ['draft', 'published', 'flagged']
|
37
37
|
end
|
38
38
|
end
|
39
|
+
|
40
|
+
describe '#next_states' do
|
41
|
+
it "should return the available states the object can move to" do
|
42
|
+
Timecop.travel 1.day.ago
|
43
|
+
article = Article.create
|
44
|
+
Timecop.return
|
45
|
+
|
46
|
+
article.next_states.should =~ [:published, :deleted]
|
47
|
+
|
48
|
+
article.publish!
|
49
|
+
article.next_states.should =~ [:flagged, :deleted]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#article' do
|
54
|
+
it "should add the association on the state object" do
|
55
|
+
ArticleState.reflect_on_association(:article).should_not be_nil
|
56
|
+
end
|
57
|
+
end
|
39
58
|
end
|
data/spec/support/models.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
class ArticleState < ActiveRecord::Base
|
2
|
+
#
|
3
|
+
end
|
4
|
+
|
1
5
|
class Article < ActiveRecord::Base
|
2
6
|
include AASM
|
3
7
|
include Sidestate
|
@@ -25,7 +29,3 @@ class Article < ActiveRecord::Base
|
|
25
29
|
transitions :from => [:draft, :published, :flagged], :to => :deleted
|
26
30
|
end
|
27
31
|
end
|
28
|
-
|
29
|
-
class ArticleState < ActiveRecord::Base
|
30
|
-
#
|
31
|
-
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Pat Allan
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-16 00:00:00 +11:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|