Class: Hobo::Model::Lifecycles::Transition
- Inherits:
-
Struct
- Object
- Struct
- Hobo::Model::Lifecycles::Transition
- Includes:
- Actions
- Defined in:
- lib/hobo/model/lifecycles/transition.rb
Instance Attribute Summary collapse
-
#end_state ⇒ Object
Returns the value of attribute end_state.
-
#lifecycle ⇒ Object
Returns the value of attribute lifecycle.
-
#name ⇒ Object
Returns the value of attribute name.
-
#on_transition ⇒ Object
Returns the value of attribute on_transition.
-
#options ⇒ Object
Returns the value of attribute options.
-
#start_states ⇒ Object
Returns the value of attribute start_states.
Instance Method Summary collapse
- #change_state(record) ⇒ Object
- #extract_attributes(attributes) ⇒ Object
-
#initialize(*args) ⇒ Transition
constructor
A new instance of Transition.
- #parameters ⇒ Object
- #run!(record, user, attributes) ⇒ Object
Methods included from Actions
#acting_user_is?, #apply_user_becomes!, #available_to, #available_to_acting_user?, #can_run?, #fire_event, #get_state, #guard_ok?, #prepare!, #publishable?, #publishable_by, #routable_for?, #run_hook
Constructor Details
#initialize(*args) ⇒ Transition
Returns a new instance of Transition.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 10 def initialize(*args) super self.name = name.to_sym start_states.each do |from| state = lifecycle.states[from] raise ArgumentError, "No such state '#{from}' in #{name} transition (#{lifecycle.model.name})" unless state state.transitions_out << self end lifecycle.transitions << self end |
Instance Attribute Details
#end_state ⇒ Object
Returns the value of attribute end_state
5 6 7 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 5 def end_state @end_state end |
#lifecycle ⇒ Object
Returns the value of attribute lifecycle
5 6 7 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 5 def lifecycle @lifecycle end |
#name ⇒ Object
Returns the value of attribute name
5 6 7 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 5 def name @name end |
#on_transition ⇒ Object
Returns the value of attribute on_transition
5 6 7 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 5 def on_transition @on_transition end |
#options ⇒ Object
Returns the value of attribute options
5 6 7 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 5 def @options end |
#start_states ⇒ Object
Returns the value of attribute start_states
5 6 7 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 5 def start_states @start_states end |
Instance Method Details
#change_state(record) ⇒ Object
36 37 38 39 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 36 def change_state(record) record.lifecycle.clear_key unless [:new_key] || [:keep_key] record.lifecycle.become(get_state(record, end_state)) end |
#extract_attributes(attributes) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 22 def extract_attributes(attributes) model = lifecycle.model params = .fetch(:params, []) allowed = params.dup params.each do |p| if (refl = model.reflections[p.to_s]) && refl.macro == :belongs_to allowed << refl.foreign_key.to_s allowed << refl.[:foreign_type] if refl.[:polymorphic] end end attributes & allowed end |
#parameters ⇒ Object
61 62 63 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 61 def parameters [:params] || [] end |
#run!(record, user, attributes) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/hobo/model/lifecycles/transition.rb', line 42 def run!(record, user, attributes) current_state = record.lifecycle.state_name unless start_states.include?(current_state) raise Hobo::Model::Lifecycles::LifecycleError, "Transition #{record.class}##{name} cannot be run from the '#{current_state}' state" end record.lifecycle.active_step = self record.with_acting_user(user) do prepare!(record, attributes) if can_run?(record) if change_state(record) fire_event(record, on_transition) end else raise Hobo::PermissionDeniedError end end end |