branston 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. data/README.rdoc +1 -1
  2. data/lib/branston/Gemfile +25 -0
  3. data/lib/branston/Gemfile.lock +76 -0
  4. data/lib/branston/app/controllers/application_controller.rb +1 -1
  5. data/lib/branston/app/controllers/outcomes_controller.rb +2 -0
  6. data/lib/branston/app/controllers/stories_controller.rb +82 -86
  7. data/lib/branston/app/controllers/users_controller.rb +69 -11
  8. data/lib/branston/app/helpers/iterations_helper.rb +13 -13
  9. data/lib/branston/app/models/iteration.rb +3 -1
  10. data/lib/branston/app/models/release.rb +0 -1
  11. data/lib/branston/app/models/story.rb +30 -28
  12. data/lib/branston/app/models/user.rb +46 -1
  13. data/lib/branston/app/views/layouts/_header.html.erb +8 -3
  14. data/lib/branston/app/views/layouts/user_roles.html.erb +5 -5
  15. data/lib/branston/app/views/sessions/new.html.erb +8 -14
  16. data/lib/branston/app/views/users/_admin_controls.html.erb +14 -0
  17. data/lib/branston/app/views/users/_form.html.erb +27 -0
  18. data/lib/branston/app/views/users/edit.html.erb +9 -0
  19. data/lib/branston/app/views/users/index.html.erb +14 -0
  20. data/lib/branston/app/views/users/new.html.erb +3 -22
  21. data/lib/branston/config/boot.rb +20 -0
  22. data/lib/branston/config/environment.rb +2 -7
  23. data/lib/branston/config/environments/test.rb +0 -8
  24. data/lib/branston/config/preinitializer.rb +21 -0
  25. data/lib/branston/config/routes.rb +15 -10
  26. data/lib/branston/db/development.sqlite3 +0 -0
  27. data/lib/branston/db/development_structure.sql +21 -8
  28. data/lib/branston/db/migrate/20100723161424_add_state_to_user.rb +12 -0
  29. data/lib/branston/db/migrate/20100726150322_add_activation_fields_to_user.rb +12 -0
  30. data/lib/branston/db/migrate/20100729125551_set_default_user_state_to_pending.rb +10 -0
  31. data/lib/branston/db/migrate/20100812133837_add_is_admin_property_to_user.rb +10 -0
  32. data/lib/branston/db/migrate/20100812140532_set_default_user_state_to_active.rb +10 -0
  33. data/lib/branston/db/migrate/20100812143455_add_default_admin_user.rb +17 -0
  34. data/lib/branston/db/migrate/20110408162438_remove_is_admin_property_and_add_role_instead.rb +12 -0
  35. data/lib/branston/db/pristine.sqlite3 +0 -0
  36. data/lib/branston/db/schema.rb +6 -8
  37. data/lib/branston/db/test.sqlite3 +0 -0
  38. data/lib/branston/log/development.log +1181 -433
  39. data/lib/branston/log/test.log +145306 -52026
  40. data/lib/branston/test/blueprints.rb +22 -28
  41. data/lib/branston/test/functional/iterations_controller_test.rb +149 -113
  42. data/lib/branston/test/functional/outcomes_controller_test.rb +94 -60
  43. data/lib/branston/test/functional/preconditions_controller_test.rb +101 -67
  44. data/lib/branston/test/functional/releases_controller_test.rb +85 -49
  45. data/lib/branston/test/functional/scenarios_controller_test.rb +104 -70
  46. data/lib/branston/test/functional/stories_controller_test.rb +41 -12
  47. data/lib/branston/test/functional/users_controller_test.rb +364 -43
  48. data/lib/branston/test/unit/iteration_test.rb +37 -6
  49. data/lib/branston/test/unit/outcome_test.rb +2 -2
  50. data/lib/branston/test/unit/participation_test.rb +2 -2
  51. data/lib/branston/test/unit/precondition_test.rb +3 -3
  52. data/lib/branston/test/unit/release_test.rb +4 -0
  53. data/lib/branston/test/unit/scenario_test.rb +4 -4
  54. data/lib/branston/test/unit/story_test.rb +62 -40
  55. data/lib/branston/test/unit/user_test.rb +195 -5
  56. metadata +136 -156
  57. data/lib/branston/app/controllers/user_roles_controller.rb +0 -105
  58. data/lib/branston/app/helpers/user_roles_helper.rb +0 -2
  59. data/lib/branston/app/models/user_role.rb +0 -21
  60. data/lib/branston/app/views/layouts/outcomes.html.erb +0 -17
  61. data/lib/branston/app/views/layouts/preconditions.html.erb +0 -17
  62. data/lib/branston/app/views/layouts/releases.html.erb +0 -17
  63. data/lib/branston/app/views/user_roles/edit.html.erb +0 -16
  64. data/lib/branston/app/views/user_roles/index.html.erb +0 -20
  65. data/lib/branston/app/views/user_roles/new.html.erb +0 -15
  66. data/lib/branston/app/views/user_roles/show.html.erb +0 -8
  67. data/lib/branston/coverage/app-controllers-application_controller_rb.html +0 -231
  68. data/lib/branston/coverage/app-controllers-iterations_controller_rb.html +0 -801
  69. data/lib/branston/coverage/app-controllers-outcomes_controller_rb.html +0 -759
  70. data/lib/branston/coverage/app-controllers-preconditions_controller_rb.html +0 -783
  71. data/lib/branston/coverage/app-controllers-releases_controller_rb.html +0 -705
  72. data/lib/branston/coverage/app-controllers-scenarios_controller_rb.html +0 -777
  73. data/lib/branston/coverage/app-controllers-sessions_controller_rb.html +0 -411
  74. data/lib/branston/coverage/app-controllers-stories_controller_rb.html +0 -1071
  75. data/lib/branston/coverage/app-controllers-user_roles_controller_rb.html +0 -693
  76. data/lib/branston/coverage/app-controllers-users_controller_rb.html +0 -315
  77. data/lib/branston/coverage/app-helpers-application_helper_rb.html +0 -327
  78. data/lib/branston/coverage/app-helpers-iterations_helper_rb.html +0 -363
  79. data/lib/branston/coverage/app-helpers-outcomes_helper_rb.html +0 -75
  80. data/lib/branston/coverage/app-helpers-preconditions_helper_rb.html +0 -75
  81. data/lib/branston/coverage/app-helpers-releases_helper_rb.html +0 -75
  82. data/lib/branston/coverage/app-helpers-sessions_helper_rb.html +0 -75
  83. data/lib/branston/coverage/app-helpers-stories_helper_rb.html +0 -75
  84. data/lib/branston/coverage/app-helpers-user_roles_helper_rb.html +0 -75
  85. data/lib/branston/coverage/app-models-iteration_rb.html +0 -321
  86. data/lib/branston/coverage/app-models-outcome_rb.html +0 -243
  87. data/lib/branston/coverage/app-models-participation_rb.html +0 -189
  88. data/lib/branston/coverage/app-models-precondition_rb.html +0 -243
  89. data/lib/branston/coverage/app-models-release_rb.html +0 -195
  90. data/lib/branston/coverage/app-models-scenario_rb.html +0 -231
  91. data/lib/branston/coverage/app-models-story_rb.html +0 -621
  92. data/lib/branston/coverage/app-models-user_rb.html +0 -513
  93. data/lib/branston/coverage/app-models-user_role_rb.html +0 -189
  94. data/lib/branston/coverage/index.html +0 -570
  95. data/lib/branston/coverage/jquery-1.3.2.min.js +0 -19
  96. data/lib/branston/coverage/jquery.tablesorter.min.js +0 -15
  97. data/lib/branston/coverage/lib-client_rb.html +0 -537
  98. data/lib/branston/coverage/lib-faker_extras_rb.html +0 -207
  99. data/lib/branston/coverage/lib-story_generator_rb.html +0 -873
  100. data/lib/branston/coverage/print.css +0 -12
  101. data/lib/branston/coverage/rcov.js +0 -42
  102. data/lib/branston/coverage/screen.css +0 -270
  103. data/lib/branston/db/migrate/20091127131037_create_user_roles.rb +0 -13
  104. data/lib/branston/db/migrate/20091127172950_add_story_id_to_user_role.rb +0 -10
  105. data/lib/branston/test/functional/user_roles_controller_test.rb +0 -71
  106. data/lib/branston/test/unit/helpers/user_roles_helper_test.rb +0 -4
  107. data/lib/branston/test/unit/user_role_test.rb +0 -9
  108. data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_flat.txt +0 -8
  109. data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_graph.html +0 -6718
  110. data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_tree.txt +0 -9942
  111. data/lib/branston/vendor/plugins/state_machine/CHANGELOG.rdoc +0 -298
  112. data/lib/branston/vendor/plugins/state_machine/LICENSE +0 -20
  113. data/lib/branston/vendor/plugins/state_machine/README.rdoc +0 -466
  114. data/lib/branston/vendor/plugins/state_machine/Rakefile +0 -98
  115. data/lib/branston/vendor/plugins/state_machine/examples/AutoShop_state.png +0 -0
  116. data/lib/branston/vendor/plugins/state_machine/examples/Car_state.png +0 -0
  117. data/lib/branston/vendor/plugins/state_machine/examples/TrafficLight_state.png +0 -0
  118. data/lib/branston/vendor/plugins/state_machine/examples/Vehicle_state.png +0 -0
  119. data/lib/branston/vendor/plugins/state_machine/examples/auto_shop.rb +0 -11
  120. data/lib/branston/vendor/plugins/state_machine/examples/car.rb +0 -19
  121. data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/controller.rb +0 -51
  122. data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/model.rb +0 -28
  123. data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_edit.html.erb +0 -24
  124. data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_index.html.erb +0 -23
  125. data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_new.html.erb +0 -13
  126. data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_show.html.erb +0 -17
  127. data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/controller.rb +0 -43
  128. data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/migration.rb +0 -11
  129. data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/model.rb +0 -23
  130. data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_edit.html.erb +0 -25
  131. data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_index.html.erb +0 -23
  132. data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_new.html.erb +0 -14
  133. data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_show.html.erb +0 -17
  134. data/lib/branston/vendor/plugins/state_machine/examples/traffic_light.rb +0 -7
  135. data/lib/branston/vendor/plugins/state_machine/examples/vehicle.rb +0 -31
  136. data/lib/branston/vendor/plugins/state_machine/init.rb +0 -1
  137. data/lib/branston/vendor/plugins/state_machine/lib/state_machine.rb +0 -388
  138. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/assertions.rb +0 -36
  139. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/callback.rb +0 -189
  140. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/condition_proxy.rb +0 -94
  141. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/eval_helpers.rb +0 -67
  142. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event.rb +0 -252
  143. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event_collection.rb +0 -122
  144. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/extensions.rb +0 -149
  145. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/guard.rb +0 -230
  146. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations.rb +0 -68
  147. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record.rb +0 -492
  148. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/locale.rb +0 -11
  149. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb +0 -41
  150. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper.rb +0 -351
  151. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper/observer.rb +0 -139
  152. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/sequel.rb +0 -322
  153. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine.rb +0 -1467
  154. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine_collection.rb +0 -155
  155. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher.rb +0 -123
  156. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher_helpers.rb +0 -54
  157. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/node_collection.rb +0 -152
  158. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state.rb +0 -249
  159. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state_collection.rb +0 -112
  160. data/lib/branston/vendor/plugins/state_machine/lib/state_machine/transition.rb +0 -394
  161. data/lib/branston/vendor/plugins/state_machine/state_machine.gemspec +0 -30
  162. data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rake +0 -1
  163. data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rb +0 -30
  164. data/lib/branston/vendor/plugins/state_machine/test/classes/switch.rb +0 -11
  165. data/lib/branston/vendor/plugins/state_machine/test/functional/state_machine_test.rb +0 -941
  166. data/lib/branston/vendor/plugins/state_machine/test/test_helper.rb +0 -4
  167. data/lib/branston/vendor/plugins/state_machine/test/unit/assertions_test.rb +0 -40
  168. data/lib/branston/vendor/plugins/state_machine/test/unit/callback_test.rb +0 -455
  169. data/lib/branston/vendor/plugins/state_machine/test/unit/condition_proxy_test.rb +0 -328
  170. data/lib/branston/vendor/plugins/state_machine/test/unit/eval_helpers_test.rb +0 -120
  171. data/lib/branston/vendor/plugins/state_machine/test/unit/event_collection_test.rb +0 -326
  172. data/lib/branston/vendor/plugins/state_machine/test/unit/event_test.rb +0 -743
  173. data/lib/branston/vendor/plugins/state_machine/test/unit/guard_test.rb +0 -908
  174. data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/active_record_test.rb +0 -1367
  175. data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/data_mapper_test.rb +0 -962
  176. data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/sequel_test.rb +0 -859
  177. data/lib/branston/vendor/plugins/state_machine/test/unit/integrations_test.rb +0 -42
  178. data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_event_test.rb +0 -7
  179. data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_transition_test.rb +0 -7
  180. data/lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb +0 -938
  181. data/lib/branston/vendor/plugins/state_machine/test/unit/machine_test.rb +0 -2004
  182. data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_helpers_test.rb +0 -37
  183. data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_test.rb +0 -155
  184. data/lib/branston/vendor/plugins/state_machine/test/unit/node_collection_test.rb +0 -207
  185. data/lib/branston/vendor/plugins/state_machine/test/unit/state_collection_test.rb +0 -280
  186. data/lib/branston/vendor/plugins/state_machine/test/unit/state_machine_test.rb +0 -31
  187. data/lib/branston/vendor/plugins/state_machine/test/unit/state_test.rb +0 -795
  188. data/lib/branston/vendor/plugins/state_machine/test/unit/transition_test.rb +0 -1212
@@ -1,17 +0,0 @@
1
- <p>
2
- <b>Name:</b>
3
- <%=h @user.name %>
4
- </p>
5
-
6
- <p>
7
- <b>State:</b>
8
- <%=h @user.state %>
9
- </p>
10
-
11
- <p>
12
- <b>Access State:</b>
13
- <%=h @user.access_state %>
14
- </p>
15
-
16
- <%= link_to 'Edit', edit_user_path(@user) %> |
17
- <%= link_to 'Back', users_path %>
@@ -1,7 +0,0 @@
1
- class TrafficLight
2
- state_machine :initial => :stop do
3
- event :cycle do
4
- transition :stop => :proceed, :proceed => :caution, :caution => :stop
5
- end
6
- end
7
- end
@@ -1,31 +0,0 @@
1
- class Vehicle
2
- state_machine :initial => :parked do
3
- event :park do
4
- transition [:idling, :first_gear] => :parked
5
- end
6
-
7
- event :ignite do
8
- transition :stalled => same, :parked => :idling
9
- end
10
-
11
- event :idle do
12
- transition :first_gear => :idling
13
- end
14
-
15
- event :shift_up do
16
- transition :idling => :first_gear, :first_gear => :second_gear, :second_gear => :third_gear
17
- end
18
-
19
- event :shift_down do
20
- transition :third_gear => :second_gear, :second_gear => :first_gear
21
- end
22
-
23
- event :crash do
24
- transition [:first_gear, :second_gear, :third_gear] => :stalled
25
- end
26
-
27
- event :repair do
28
- transition :stalled => :parked
29
- end
30
- end
31
- end
@@ -1 +0,0 @@
1
- require 'state_machine'
@@ -1,388 +0,0 @@
1
- require 'state_machine/machine'
2
-
3
- # A state machine is a model of behavior composed of states, events, and
4
- # transitions. This helper adds support for defining this type of
5
- # functionality on any Ruby class.
6
- module StateMachine
7
- module MacroMethods
8
- # Creates a new state machine with the given name. The default name, if not
9
- # specified, is <tt>:state</tt>.
10
- #
11
- # Configuration options:
12
- # * <tt>:attribute</tt> - The name of the attribute to store the state value
13
- # in. By default, this is the same as the name of the machine.
14
- # * <tt>:initial</tt> - The initial state of the attribute. This can be a
15
- # static state or a lambda block which will be evaluated at runtime
16
- # (e.g. lambda {|vehicle| vehicle.speed == 0 ? :parked : :idling}).
17
- # Default is nil.
18
- # * <tt>:action</tt> - The instance method to invoke when an object
19
- # transitions. Default is nil unless otherwise specified by the
20
- # configured integration.
21
- # * <tt>:namespace</tt> - The name to use for namespacing all generated
22
- # state / event instance methods (e.g. "heater" would generate
23
- # :turn_on_heater and :turn_off_heater for the :turn_on/:turn_off events).
24
- # Default is nil.
25
- # * <tt>:integration</tt> - The name of the integration to use for adding
26
- # library-specific behavior to the machine. Built-in integrations
27
- # include :data_mapper, :active_record, and :sequel. By default, this
28
- # is determined automatically.
29
- #
30
- # Configuration options relevant to ORM integrations:
31
- # * <tt>:plural</tt> - The pluralized name of the attribute. By default,
32
- # this will attempt to call +pluralize+ on the attribute. If this
33
- # method is not available, an "s" is appended. This is used for
34
- # generating scopes.
35
- # * <tt>:messages</tt> - The error messages to use when invalidating
36
- # objects due to failed transitions. Messages include:
37
- # * <tt>:invalid</tt>
38
- # * <tt>:invalid_event</tt>
39
- # * <tt>:invalid_transition</tt>
40
- # * <tt>:use_transactions</tt> - Whether transactions should be used when
41
- # firing events. Default is true unless otherwise specified by the
42
- # configured integration.
43
- #
44
- # This also expects a block which will be used to actually configure the
45
- # states, events and transitions for the state machine. *Note* that this
46
- # block will be executed within the context of the state machine. As a
47
- # result, you will not be able to access any class methods unless you refer
48
- # to them directly (i.e. specifying the class name).
49
- #
50
- # For examples on the types of state machine configurations and blocks, see
51
- # the section below.
52
- #
53
- # == Examples
54
- #
55
- # With the default name/attribute and no configuration:
56
- #
57
- # class Vehicle
58
- # state_machine do
59
- # event :park do
60
- # ...
61
- # end
62
- # end
63
- # end
64
- #
65
- # The above example will define a state machine named "state" that will
66
- # store the value in the +state+ attribute. Every vehicle will start
67
- # without an initial state.
68
- #
69
- # With a custom name / attribute:
70
- #
71
- # class Vehicle
72
- # state_machine :status, :attribute => :status_value do
73
- # ...
74
- # end
75
- # end
76
- #
77
- # With a static initial state:
78
- #
79
- # class Vehicle
80
- # state_machine :status, :initial => :parked do
81
- # ...
82
- # end
83
- # end
84
- #
85
- # With a dynamic initial state:
86
- #
87
- # class Vehicle
88
- # state_machine :status, :initial => lambda {|vehicle| vehicle.speed == 0 ? :parked : :idling} do
89
- # ...
90
- # end
91
- # end
92
- #
93
- # == Instance Methods
94
- #
95
- # The following instance methods will be automatically generated by the
96
- # state machine based on the *name* of the machine. Any existing methods
97
- # will not be overwritten.
98
- # * <tt>state</tt> - Gets the current value for the attribute
99
- # * <tt>state=(value)</tt> - Sets the current value for the attribute
100
- # * <tt>state?(name)</tt> - Checks the given state name against the current
101
- # state. If the name is not a known state, then an ArgumentError is raised.
102
- # * <tt>state_name</tt> - Gets the name of the state for the current value
103
- # * <tt>state_events</tt> - Gets the list of events that can be fired on
104
- # the current object's state (uses the *unqualified* event names)
105
- # * <tt>state_transitions(requirements = {})</tt> - Gets the list of possible
106
- # transitions that can be made on the current object's state. Additional
107
- # requirements, such as the :from / :to state and :on event can be specified
108
- # to restrict the transitions to select. By default, the current state
109
- # will be used for the :from state.
110
- #
111
- # For example,
112
- #
113
- # class Vehicle
114
- # state_machine :state, :initial => :parked do
115
- # event :ignite do
116
- # transition :parked => :idling
117
- # end
118
- #
119
- # event :park do
120
- # transition :idling => :parked
121
- # end
122
- # end
123
- # end
124
- #
125
- # vehicle = Vehicle.new
126
- # vehicle.state # => "parked"
127
- # vehicle.state_name # => :parked
128
- # vehicle.state?(:parked) # => true
129
- #
130
- # # Changing state
131
- # vehicle.state = 'idling'
132
- # vehicle.state # => "idling"
133
- # vehicle.state_name # => :idling
134
- # vehicle.state?(:parked) # => false
135
- #
136
- # # Getting current event / transition availability
137
- # vehicle.state_events # => [:park]
138
- # vehicle.park # => true
139
- # vehicle.state_events # => [:ignite]
140
- #
141
- # vehicle.state_transitions # => [#<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>]
142
- # vehicle.ignite
143
- # vehicle.state_transitions # => [#<StateMachine::Transition attribute=:state event=:park from="idling" from_name=:idling to="parked" to_name=:parked>]
144
- #
145
- # == Attribute initialization
146
- #
147
- # For most classes, the initial values for state machine attributes are
148
- # automatically assigned when a new object is created. However, this
149
- # behavior will *not* work if the class defines an +initialize+ method
150
- # without properly calling +super+.
151
- #
152
- # For example,
153
- #
154
- # class Vehicle
155
- # state_machine :state, :initial => :parked do
156
- # ...
157
- # end
158
- # end
159
- #
160
- # vehicle = Vehicle.new # => #<Vehicle:0xb7c8dbf8 @state="parked">
161
- # vehicle.state # => "parked"
162
- #
163
- # In the above example, no +initialize+ method is defined. As a result,
164
- # the default behavior of initializing the state machine attributes is used.
165
- #
166
- # In the following example, a custom +initialize+ method is defined:
167
- #
168
- # class Vehicle
169
- # state_machine :state, :initial => :parked do
170
- # ...
171
- # end
172
- #
173
- # def initialize
174
- # end
175
- # end
176
- #
177
- # vehicle = Vehicle.new # => #<Vehicle:0xb7c77678>
178
- # vehicle.state # => nil
179
- #
180
- # Since the +initialize+ method is defined, the state machine attributes
181
- # never get initialized. In order to ensure that all initialization hooks
182
- # are called, the custom method *must* call +super+ without any arguments
183
- # like so:
184
- #
185
- # class Vehicle
186
- # state_machine :state, :initial => :parked do
187
- # ...
188
- # end
189
- #
190
- # def initialize(attributes = {})
191
- # ...
192
- # super()
193
- # end
194
- # end
195
- #
196
- # vehicle = Vehicle.new # => #<Vehicle:0xb7c8dbf8 @state="parked">
197
- # vehicle.state # => "parked"
198
- #
199
- # Because of the way the inclusion of modules works in Ruby, calling
200
- # <tt>super()</tt> will not only call the superclass's +initialize+, but
201
- # also +initialize+ on all included modules. This allows the original state
202
- # machine hook to get called properly.
203
- #
204
- # If you want to avoid calling the superclass's constructor, but still want
205
- # to initialize the state machine attributes:
206
- #
207
- # class Vehicle
208
- # state_machine :state, :initial => :parked do
209
- # ...
210
- # end
211
- #
212
- # def initialize(attributes = {})
213
- # ...
214
- # initialize_state_machines
215
- # end
216
- # end
217
- #
218
- # vehicle = Vehicle.new # => #<Vehicle:0xb7c8dbf8 @state="parked">
219
- # vehicle.state # => "parked"
220
- #
221
- # == States
222
- #
223
- # All of the valid states for the machine are automatically tracked based
224
- # on the events, transitions, and callbacks defined for the machine. If
225
- # there are additional states that are never referenced, these should be
226
- # explicitly added using the StateMachine::Machine#state or
227
- # StateMachine::Machine#other_states helpers.
228
- #
229
- # When a new state is defined, a predicate method for that state is
230
- # generated on the class. For example,
231
- #
232
- # class Vehicle
233
- # state_machine :initial => :parked do
234
- # event :ignite do
235
- # transition all => :idling
236
- # end
237
- # end
238
- # end
239
- #
240
- # ...will generate the following instance methods (assuming they're not
241
- # already defined in the class):
242
- # * <tt>parked?</tt>
243
- # * <tt>idling?</tt>
244
- #
245
- # Each predicate method will return true if it matches the object's
246
- # current state. Otherwise, it will return false.
247
- #
248
- # == Events and Transitions
249
- #
250
- # Events defined on the machine are the interface to transitioning states
251
- # for an object. Events can be fired either directly (through the method
252
- # generated for the event) or indirectly (through attributes defined on
253
- # the machine).
254
- #
255
- # For example,
256
- #
257
- # class Vehicle
258
- # include DataMapper::Resource
259
- # property :id, Serial
260
- #
261
- # state_machine :initial => :parked do
262
- # event :ignite do
263
- # transition :parked => :idling
264
- # end
265
- # end
266
- #
267
- # state_machine :alarm_state, :initial => :active do
268
- # event :disable do
269
- # transition all => :off
270
- # end
271
- # end
272
- # end
273
- #
274
- # # Fire +ignite+ event directly
275
- # vehicle = Vehicle.create # => #<Vehicle id=1 state="parked" alarm_state="active">
276
- # vehicle.ignite # => true
277
- # vehicle.state # => "idling"
278
- # vehicle.alarm_state # => "active"
279
- #
280
- # # Fire +disable+ event automatically
281
- # vehicle.alarm_state_event = 'disable'
282
- # vehicle.save # => true
283
- # vehicle.alarm_state # => "off"
284
- #
285
- # In the above example, the +state+ attribute is transitioned using the
286
- # +ignite+ action that's generated from the state machine. On the other
287
- # hand, the +alarm_state+ attribute is transitioned using the +alarm_state_event+
288
- # attribute that automatically gets fired when the machine's action (+save+)
289
- # is invoked.
290
- #
291
- # For more information about how to configure an event and its associated
292
- # transitions, see StateMachine::Machine#event.
293
- #
294
- # == Defining callbacks
295
- #
296
- # Within the +state_machine+ block, you can also define callbacks for
297
- # transitions. For more information about defining these callbacks,
298
- # see StateMachine::Machine#before_transition and
299
- # StateMachine::Machine#after_transition.
300
- #
301
- # == Namespaces
302
- #
303
- # When a namespace is configured for a state machine, the name provided
304
- # will be used in generating the instance methods for interacting with
305
- # states/events in the machine. This is particularly useful when a class
306
- # has multiple state machines and it would be difficult to differentiate
307
- # between the various states / events.
308
- #
309
- # For example,
310
- #
311
- # class Vehicle
312
- # state_machine :heater_state, :initial => :off, :namespace => 'heater' do
313
- # event :turn_on do
314
- # transition all => :on
315
- # end
316
- #
317
- # event :turn_off do
318
- # transition all => :off
319
- # end
320
- # end
321
- #
322
- # state_machine :alarm_state, :initial => :active, :namespace => 'alarm' do
323
- # event :turn_on do
324
- # transition all => :active
325
- # end
326
- #
327
- # event :turn_off do
328
- # transition all => :off
329
- # end
330
- # end
331
- # end
332
- #
333
- # The above class defines two state machines: +heater_state+ and +alarm_state+.
334
- # For the +heater_state+ machine, the following methods are generated since
335
- # it's namespaced by "heater":
336
- # * <tt>can_turn_on_heater?</tt>
337
- # * <tt>turn_on_heater</tt>
338
- # * ...
339
- # * <tt>can_turn_off_heater?</tt>
340
- # * <tt>turn_off_heater</tt>
341
- # * ..
342
- # * <tt>heater_off?</tt>
343
- # * <tt>heater_on?</tt>
344
- #
345
- # As shown, each method is unique to the state machine so that the states
346
- # and events don't conflict. The same goes for the +alarm_state+ machine:
347
- # * <tt>can_turn_on_alarm?</tt>
348
- # * <tt>turn_on_alarm</tt>
349
- # * ...
350
- # * <tt>can_turn_off_alarm?</tt>
351
- # * <tt>turn_off_alarm</tt>
352
- # * ..
353
- # * <tt>alarm_active?</tt>
354
- # * <tt>alarm_off?</tt>
355
- #
356
- # == Scopes
357
- #
358
- # For integrations that support it, a group of default scope filters will
359
- # be automatically created for assisting in finding objects that have the
360
- # attribute set to one of a given set of states.
361
- #
362
- # For example,
363
- #
364
- # Vehicle.with_state(:parked) # => All vehicles where the state is parked
365
- # Vehicle.with_states(:parked, :idling) # => All vehicles where the state is either parked or idling
366
- #
367
- # Vehicle.without_state(:parked) # => All vehicles where the state is *not* parked
368
- # Vehicle.without_states(:parked, :idling) # => All vehicles where the state is *not* parked or idling
369
- #
370
- # *Note* that if class methods already exist with those names (i.e.
371
- # :with_state, :with_states, :without_state, or :without_states), then a
372
- # scope will not be defined for that name.
373
- #
374
- # See StateMachine::Machine for more information about using integrations
375
- # and the individual integration docs for information about the actual
376
- # scopes that are generated.
377
- def state_machine(*args, &block)
378
- StateMachine::Machine.find_or_create(self, *args, &block)
379
- end
380
- end
381
- end
382
-
383
- Class.class_eval do
384
- include StateMachine::MacroMethods
385
- end
386
-
387
- # Register rake tasks for supported libraries
388
- Merb::Plugins.add_rakefiles("#{File.dirname(__FILE__)}/../tasks/state_machine") if defined?(Merb::Plugins)