Class: Hobo::Model::Lifecycles::Transition

Inherits:
Struct
  • Object
show all
Includes:
Actions
Defined in:
lib/hobo/model/lifecycles/transition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_stateObject

Returns the value of attribute end_state

Returns:

  • (Object)

    the current value of end_state



5
6
7
# File 'lib/hobo/model/lifecycles/transition.rb', line 5

def end_state
  @end_state
end

#lifecycleObject

Returns the value of attribute lifecycle

Returns:

  • (Object)

    the current value of lifecycle



5
6
7
# File 'lib/hobo/model/lifecycles/transition.rb', line 5

def lifecycle
  @lifecycle
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



5
6
7
# File 'lib/hobo/model/lifecycles/transition.rb', line 5

def name
  @name
end

#on_transitionObject

Returns the value of attribute on_transition

Returns:

  • (Object)

    the current value of on_transition



5
6
7
# File 'lib/hobo/model/lifecycles/transition.rb', line 5

def on_transition
  @on_transition
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



5
6
7
# File 'lib/hobo/model/lifecycles/transition.rb', line 5

def options
  @options
end

#start_statesObject

Returns the value of attribute start_states

Returns:

  • (Object)

    the current value of 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 options[:new_key] || options[: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 = 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

#parametersObject



61
62
63
# File 'lib/hobo/model/lifecycles/transition.rb', line 61

def parameters
  options[: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