Sha256: 47f419c3a23dddb6ffb993d50659c6962be33235c8aff5b2b56063a383366943
Contents?: true
Size: 1.98 KB
Versions: 7
Compression:
Stored size: 1.98 KB
Contents
module Hobo module Model module Lifecycles 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] raise ArgumentError, "No such state '#{from}' in #{name} transition (#{lifecycle.model.name})" unless state state.transitions_out << self end lifecycle.transitions << self end 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 def change_state(record) record.lifecycle.clear_key unless options[:new_key] || options[:keep_key] record.lifecycle.become(get_state(record, end_state)) end 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 def parameters options[:params] || [] end end end end end
Version data entries
7 entries across 7 versions & 1 rubygems