mikowitz-aasm 2.0.5 → 2.0.6

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.
@@ -57,6 +57,45 @@ Here's a quick example highlighting some of the features.
57
57
  end
58
58
  end
59
59
 
60
+
61
+ == Less Simple Example
62
+
63
+ Another example showing how to use the :role options
64
+
65
+ class Report
66
+ include AASM
67
+
68
+ aasm_initial_state :submitted
69
+
70
+ aasm_state :submitted
71
+ aasm_state :public
72
+ aasm_state :hidden
73
+
74
+ aasm_default_role :reporter
75
+
76
+ aasm_role :reporter
77
+ aasm_role :admin
78
+
79
+ aasm_event :edit do
80
+ transitions :from => :public, :to => :submitted, :for => :reporter
81
+ transitions :from => :public, :to => :public, :for => :admin
82
+ end
83
+
84
+ aasm_event :hide do
85
+ transitions :from => :public, :to => :submitted, :for => :reporter
86
+ transitions :from => :public, :to => :hidden, :for => :admin
87
+ end
88
+
89
+ aasm_event :approve do
90
+ transitions :from => :submitted, :for => :admin, :on_transition => { approve_proposed_changes }
91
+ end
92
+
93
+ aasm_event :reject do
94
+ transitions :from => :submitted, :for => :admin, :on_transition => { reject_proposed_changes }
95
+ end
96
+ end
97
+
98
+
60
99
  = Other Stuff
61
100
 
62
101
  Author:: Scott Barron <scott at elitists dot net>
data/Rakefile CHANGED
@@ -82,13 +82,4 @@ else
82
82
  end
83
83
  end
84
84
 
85
- if !defined?(Gem)
86
- puts "Package target requires RubyGEMs"
87
- else
88
- desc "sudo gem uninstall aasm && rake gem && sudo gem install pkg/aasm-2.0.3.gem"
89
- task :reinstall do
90
- puts `sudo gem uninstall aasm && rake gem && sudo gem install pkg/aasm-2.0.3.gem`
91
- end
92
- end
93
-
94
85
  task :default => [:spec]
@@ -0,0 +1,69 @@
1
+ require File.join(File.dirname(__FILE__), 'state_transition')
2
+
3
+ module AASM
4
+ module SupportingClasses
5
+ class StagedEvent
6
+ attr_reader :name, :success, :admin_roles, :staging_roles, :admin_only
7
+
8
+ def initialize(name, options = {}, &block)
9
+ @name = name
10
+ @success = options[:success]
11
+ @admin_roles = options[:admin_roles]
12
+ @staging_roles = options[:staging_roles]
13
+ @transitions = []
14
+ @admin_only = options[:admin_only]
15
+ instance_eval(&block) if block
16
+ end
17
+
18
+ def fire(obj, to_state=nil, *args)
19
+ role = !AASM::StateMachine[obj.class].nil? ? (obj.class.aasm_roles & args.flatten).first || obj.class.aasm_default_role : nil
20
+
21
+ second_arg = obj.class.respond_to?(:aasm_roles) ? obj.class.aasm_roles : nil
22
+ transitions = @transitions.select { |t| t.from == obj.aasm_current_state and role_truth(t.for || second_arg, role) }
23
+ raise AASM::InvalidTransition, "Event '#{name}' cannot transition from '#{obj.aasm_current_state}' as #{role.to_s}" if transitions.size == 0
24
+
25
+ next_state = nil
26
+ transitions.each do |transition|
27
+ next if to_state and !Array(transition.to).include?(to_state)
28
+ if transition.perform(obj)
29
+ next_state = to_state || Array(transition.to).first
30
+ transition.execute(obj, *args)
31
+ break
32
+ end
33
+ end
34
+ next_state
35
+ end
36
+
37
+ def transitions_from_state?(state)
38
+ @transitions.any? { |t| t.from == state }
39
+ end
40
+
41
+ def execute_success_callback(obj)
42
+ case success
43
+ when String, Symbol:
44
+ obj.send(success)
45
+ when Array:
46
+ success.each { |meth| obj.send(meth) }
47
+ when Proc:
48
+ success.call(obj)
49
+ end
50
+ end
51
+
52
+ private
53
+ def transitions(trans_opts)
54
+ Array(trans_opts[:from]).each do |s|
55
+ # Make admin version
56
+ @transitions << SupportingClasses::StateTransition.new(trans_opts.merge({:from => s.to_sym, :for => @admin_roles}))
57
+ # Make staging version if it should be there
58
+ unless @admin_only == true
59
+ @transitions << SupportingClasses::StateTransition.new(trans_opts.merge({:from => s.to_sym, :to => (trans_opts[:staged_to] || nil), :for => @staging_roles, :on_transition => trans_opts[:on_staged_transition]}))
60
+ end
61
+ end
62
+ end
63
+
64
+ def role_truth(trans_for, role)
65
+ role.nil? ? true : trans_for.is_a?(Symbol) ? trans_for == role : trans_for.include?(role)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -11,7 +11,7 @@ module AASM
11
11
  (@machines ||= {})[args] = val
12
12
  end
13
13
 
14
- attr_accessor :states, :roles, :admin_roles, :staging_roles, :events, :initial_state, :default_role, :config
14
+ attr_accessor :states, :roles, :admin_roles, :staging_roles, :events, :initial_state, :default_role, :staging_state, :config
15
15
  attr_reader :name
16
16
 
17
17
  def initialize(name)
@@ -22,6 +22,7 @@ module AASM
22
22
  @roles = []
23
23
  @admin_roles = []
24
24
  @staging_roles = []
25
+ @staging_state = []
25
26
  @events = {}
26
27
  @config = OpenStruct.new
27
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mikowitz-aasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Barron
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-22 00:00:00 -08:00
12
+ date: 2009-02-05 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -30,15 +30,16 @@ files:
30
30
  - Rakefile
31
31
  - README.rdoc
32
32
  - TODO
33
+ - doc/jamis.rb
33
34
  - lib/aasm.rb
34
35
  - lib/event.rb
35
36
  - lib/persistence/active_record_persistence.rb
36
37
  - lib/persistence.rb
38
+ - lib/staged_event.rb
37
39
  - lib/state.rb
38
40
  - lib/state_machine.rb
39
41
  - lib/state_transition.rb
40
42
  - lib/version.rb
41
- - doc/jamis.rb
42
43
  has_rdoc: true
43
44
  homepage: http://github.com/rubyist/aasm
44
45
  post_install_message: