lib/hobo/lifecycles/transition.rb in hobo-0.8.3 vs lib/hobo/lifecycles/transition.rb in hobo-0.8.4
- old
+ new
@@ -1,59 +1,53 @@
module Hobo
module Lifecycles
- class Transition < Struct.new(:lifecycle, :name, :who, :start_states, :end_state, :on_transition, :options)
+ class Transition < Struct.new(:lifecycle, :name, :start_states, :end_state, :on_transition, :options)
include Actions
def initialize(*args)
super
+ self.name = name.to_sym
start_states.each do |from|
- state = lifecycle.states[from.to_s]
- raise ArgumentError, "No such state '#{from}' in #{'name'} transition (#{lifecycle.model.name})" unless state
+ state = lifecycle.states[from]
+ raise ArgumentError, "No such state '#{from}' in #{name} transition (#{lifecycle.model.name})" unless state
state.transitions_out << self
end
- unless end_state.to_s == "destroy"
- state = lifecycle.states[end_state.to_s]
- raise ArgumentError, "No such state '#{end_state}' in '#{name}' transition (#{lifecycle.model.name})" unless state
- state.transitions_in << self
- end
lifecycle.transitions << self
end
- def allowed?(record, user, attributes=nil)
- prepare_and_check!(record, user, attributes) && true
- end
-
-
def extract_attributes(attributes)
update_attributes = options.fetch(:update, [])
attributes & update_attributes
end
+
+
+ def change_state(record)
+ record.lifecycle.become(get_state(record, end_state))
+ end
def run!(record, user, attributes)
- if prepare_and_check!(record, user, attributes)
- if record.lifecycle.become end_state
- fire_event(record, on_transition)
+ current_state = record.lifecycle.state_name
+ unless start_states.include?(current_state)
+ raise Hobo::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
- else
- raise Hobo::Model::PermissionDeniedError
end
end
-
-
- def set_or_check_who_with_key!(record, user)
- if who == :with_key
- record.lifecycle.valid_key? or raise LifecycleKeyError
- else
- set_or_check_who_without_key!(record, user)
- end
- end
- alias_method_chain :set_or_check_who!, :key
def parameters
options[:update] || []
end