class Hobo::Model::Lifecycles::Transition

Public Instance Methods

change_state(record) click to toggle source
# File lib/hobo/model/lifecycles/transition.rb, line 36
def change_state(record)
  record.lifecycle.clear_key unless options[:new_key] || options[:keep_key]
  record.lifecycle.become(get_state(record, end_state))
end
extract_attributes(attributes) click to toggle source
# File lib/hobo/model/lifecycles/transition.rb, line 22
def extract_attributes(attributes)
  model = lifecycle.model
  params = options.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.options[:foreign_type] if refl.options[:polymorphic]
    end
  end
  attributes & allowed
end
parameters() click to toggle source
# File lib/hobo/model/lifecycles/transition.rb, line 61
def parameters
  options[:params] || []
end
run!(record, user, attributes) click to toggle source
# 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

Public Class Methods

new(*args) click to toggle source
# 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