Module: Estate::Logic::CommonLogic
- Defined in:
- lib/estate/logic/common_logic.rb
Instance Method Summary collapse
- #config_for(klass) ⇒ Object
- #transition_allowed?(state_machine_name, from_state, to_state) ⇒ Boolean
- #validate_state_changes(instance, from_state, to_state) ⇒ Object
Instance Method Details
#config_for(klass) ⇒ Object
27 28 29 30 |
# File 'lib/estate/logic/common_logic.rb', line 27 def config_for(klass) state_machine_name = klass.is_a?(Class) ? klass.name : klass.class.name Estate::StateMachine.state_machines[state_machine_name][:config] end |
#transition_allowed?(state_machine_name, from_state, to_state) ⇒ Boolean
23 24 25 |
# File 'lib/estate/logic/common_logic.rb', line 23 def transition_allowed?(state_machine_name, from_state, to_state) from_state.nil? || Estate::StateMachine.transition_exists?(state_machine_name, from_state, to_state) end |
#validate_state_changes(instance, from_state, to_state) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/estate/logic/common_logic.rb', line 6 def validate_state_changes(instance, from_state, to_state) state_machine_name = instance.class.name if from_state == to_state if from_state.nil? && !config_for(instance)[:empty_initial_state] add_error(instance, "empty `#{config_for(instance)[:column_name]}` is not allowed") end elsif to_state.nil? add_error(instance, 'transition to empty state is not allowed') elsif !Estate::StateMachine.state_exists?(state_machine_name, to_state) add_error(instance, "state `#{to_state}` is not defined") elsif !transition_allowed?(state_machine_name, from_state, to_state) add_error(instance, "transition from `#{from_state}` to `#{to_state}` is not allowed", attribute: config_for(instance)[:column_name]) end end |