workflow 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,71 +0,0 @@
1
- module Workflow
2
- module Adapter
3
- module ActiveRecord
4
- def self.included(klass)
5
- klass.send :include, Adapter::ActiveRecord::InstanceMethods
6
- klass.send :extend, Adapter::ActiveRecord::Scopes
7
- klass.before_validation :write_initial_state
8
- end
9
-
10
- module InstanceMethods
11
- def load_workflow_state
12
- read_attribute(self.class.workflow_column)
13
- end
14
-
15
- # On transition the new workflow state is immediately saved in the
16
- # database.
17
- def persist_workflow_state(new_value)
18
- # Rails 3.1 or newer
19
- update_column self.class.workflow_column, new_value
20
- end
21
-
22
- private
23
-
24
- # Motivation: even if NULL is stored in the workflow_state database column,
25
- # the current_state is correctly recognized in the Ruby code. The problem
26
- # arises when you want to SELECT records filtering by the value of initial
27
- # state. That's why it is important to save the string with the name of the
28
- # initial state in all the new records.
29
- def write_initial_state
30
- write_attribute self.class.workflow_column, current_state.to_s
31
- end
32
- end
33
-
34
- # This module will automatically generate ActiveRecord scopes based on workflow states.
35
- # The name of each generated scope will be something like `with_<state_name>_state`
36
- #
37
- # Examples:
38
- #
39
- # Article.with_pending_state # => ActiveRecord::Relation
40
- # Payment.without_refunded_state # => ActiveRecord::Relation
41
- #`
42
- # Example above just adds `where(:state_column_name => 'pending')` or
43
- # `where.not(:state_column_name => 'pending')` to AR query and returns
44
- # ActiveRecord::Relation.
45
- module Scopes
46
- def self.extended(object)
47
- class << object
48
- alias_method :workflow_without_scopes, :workflow unless method_defined?(:workflow_without_scopes)
49
- alias_method :workflow, :workflow_with_scopes
50
- end
51
- end
52
-
53
- def workflow_with_scopes(&specification)
54
- workflow_without_scopes(&specification)
55
- states = workflow_spec.states.values
56
-
57
- states.each do |state|
58
- define_singleton_method("with_#{state}_state") do
59
- where("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
60
- end
61
-
62
- define_singleton_method("without_#{state}_state") do
63
- where.not("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
64
- end
65
- end
66
- end
67
-
68
- end
69
- end
70
- end
71
- end
@@ -1,15 +0,0 @@
1
- module Workflow
2
- module Adapter
3
- module Remodel
4
- module InstanceMethods
5
- def load_workflow_state
6
- send(self.class.workflow_column)
7
- end
8
-
9
- def persist_workflow_state(new_value)
10
- update(self.class.workflow_column => new_value)
11
- end
12
- end
13
- end
14
- end
15
- end