Sha256: 1abe826dbf3001d12e6f8686b9c4e889f6bdbfafab678cc0b4544fa8193d6bdf
Contents?: true
Size: 1.89 KB
Versions: 9
Compression:
Stored size: 1.89 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]) && refl.macro == :belongs_to allowed << refl.primary_key_name.to_s allowed << refl.options[:foreign_type] if refl.options[:polymorphic] end end attributes & allowed end def change_state(record) 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
9 entries across 9 versions & 1 rubygems